From the course: Learning Arduino: Pulse Width Modulation

Unlock the full course today

Join today to access over 22,600 courses taught by industry experts or purchase this course individually.

Solution: Fading through the rainbow with PWM

Solution: Fading through the rainbow with PWM - Arduino Tutorial

From the course: Learning Arduino: Pulse Width Modulation

Start my 1-month free trial

Solution: Fading through the rainbow with PWM

- Did you get your Arduino to output each color? Lets take a quick look at my solution. First I instantiated constant integer variables for each LED. I also declared an array called Color to keep track of the current color that's being output. In the setup function I've initialized the serial monitor so that we can see what's going on. In the loop is where the real magic happens. I start with a four loop. Because of my initial settings in the array it will start with a red LED on. The downer variable will help me select the item in the array that is currently on and slowly bring it down. Next I initialize a variable called upper and assign it a value of zero. Then I check to see if downer is less than two. If downer is zero then upper will be assigned to one. If downer is one then upper will be assigned to two. If downer is two I need to assign upper to zero to bring the color back around to red. Then in my next four loop I slowly bring the current color down and the next color up. On…

Contents