project 1.09 change rgb color with sw1
In this project, we change the color of the RGB LED each
time SW1 is pressed. Upload the sketch and press SW1 to see it in
action.
Concepts: arrays, digitalRead, debounce
Circuits:
Concepts: arrays, digitalRead, debounce
Circuits:
In Project 1.08, we controlled the colors of the RGB LED using three variables. In this project, we use an array, RGB[], to hold the pin values for the three colors:
int RGB[3] = {9, 10, 11};
In the setup() block, we use a for loop to set each pin of the RGB LED to an output:
for(int k = 0; k<3; k++){
pinMode(RGB[k],OUTPUT);
}
At the top of the loop() block, we use the same approach as in Project 1.06 to blink without using a delay statement:
if(millis() > (lastBlink + blinkSpeed)){
isOn = 1 - isOn;
lastBlink = millis();
We use the variable thisLED to keep track of which LED to switch on or off:
digitalWrite(RGB[thisLED],isOn);
}
Next, we check to see if SW1 is pressed. We use the same approach as in Project 1.06 to debounce SW1 to make sure we record only one event each time SW1 is pressed:
isPress = digitalRead(SW1);
if(isPress == LOW && millis() > (lastPressed + 1000)){
The first thing we do in the if block is turn the current LED color off:
digitalWrite(RGB[thisLED],LOW);
We then increment thisLED and set it back to zero when it’s greater than 2. We use the short-hand thisLED ++ which is equivalent to thisLED = thisLED + 1:
thisLED ++;
if(thisLED > 2) thisLED = 0;
We’re not done yet. We use digitalWrite to turn the new LED to the correct setting. It could be on or off:
digitalWrite(RGB[thisLED],isOn);
Finally, we record the time so that we can wait at least a second before acknowledging the next time SW1 is pressed:
lastPressed = millis();
To finish up, we close the if block and the loop() block:
}
}
int RGB[3] = {9, 10, 11};
In the setup() block, we use a for loop to set each pin of the RGB LED to an output:
for(int k = 0; k<3; k++){
pinMode(RGB[k],OUTPUT);
}
At the top of the loop() block, we use the same approach as in Project 1.06 to blink without using a delay statement:
if(millis() > (lastBlink + blinkSpeed)){
isOn = 1 - isOn;
lastBlink = millis();
We use the variable thisLED to keep track of which LED to switch on or off:
digitalWrite(RGB[thisLED],isOn);
}
Next, we check to see if SW1 is pressed. We use the same approach as in Project 1.06 to debounce SW1 to make sure we record only one event each time SW1 is pressed:
isPress = digitalRead(SW1);
if(isPress == LOW && millis() > (lastPressed + 1000)){
The first thing we do in the if block is turn the current LED color off:
digitalWrite(RGB[thisLED],LOW);
We then increment thisLED and set it back to zero when it’s greater than 2. We use the short-hand thisLED ++ which is equivalent to thisLED = thisLED + 1:
thisLED ++;
if(thisLED > 2) thisLED = 0;
We’re not done yet. We use digitalWrite to turn the new LED to the correct setting. It could be on or off:
digitalWrite(RGB[thisLED],isOn);
Finally, we record the time so that we can wait at least a second before acknowledging the next time SW1 is pressed:
lastPressed = millis();
To finish up, we close the if block and the loop() block:
}
}