Hardware Reference
In-Depth Information
//OFF (mode = 0)
else
{
digitalWrite(RLED, LOW);
digitalWrite(GLED, LOW);
digitalWrite(BLED, LOW);
}
}
void loop()
{
currentButton = debounce(lastButton); //read deboucned state
if (lastButton == LOW && currentButton == HIGH) //if it was pressed...
{
ledMode++; //increment the LED value
}
lastButton = currentButton; //reset button value
//if you've cycled through the different options,
//reset the counter to 0
if (ledMode == 8) ledMode = 0;
setMode(ledMode); //change the LED state
}
This might look like a lot of code, but it is nothing more than a conglomera-
tion of code snippets that you have already written throughout this chapter.
How else could you modify this code? You could add additional buttons to
independently control one of the three colors. You could also add blink modes,
using code from Chapter 1 that blinked the LED. The possibilities are limitless.
Summary
In this chapter you learned about the following:
■
How a breadboard works
■
How to pick a resistor to current-limit an LED
■
How to wire an external LED to your Arduino
■
How to use PWM to write “analog” values to LEDs
■
How to read a pushbutton
■
How to debounce a pushbutton
■
How to use
for
loops
■
How to utilize pullup and pulldown resistors
Search WWH ::
Custom Search