Hi all! I am a middle school computer science teacher and I am using Microduino's Itty Bitty City's in my class this semester. I am trying to create a really basic project to get started, so I wrote a program (with a lot of internet help) that creates a simple button-controlled LED. You'll see a lot of my comments in there that we'll add as a class so the kids can understand what the different lines of code do.
#include <Microduino_ColorLED.h> //Import Microduino's library for the ColorLED output.
#define buttonPin 2 // Define pin 2 as the button input pin
#define ledPin 12 //Define pin 12 as the LED pin
ColorLED strip = ColorLED(1, ledPin);
/*
ColorLED is a class.
When we type = COloreLED(1, ledPin) we are creating a variable which is an instance of that class.
There is only 1 LED and ledPin is the pin it lives on.
*/
void setup() {
pinMode(ledPin, OUTPUT); //Set the LED pin as an output.
pinMode(buttonPin, INPUT_PULLUP); //Set button pin as input.
/*
On the board there is a resistor attached on the board.
Pullup enables the resistor, which routes the power through the resistor, and keeps the whole thing from shorting.
*/
}
void loop(){
int buttonState = digitalRead(buttonPin); //Read the value from the buttonPin
if (buttonState == LOW) { //Why do we say "LOW" not "HIGH"? Resistors invert things!
strip.setPixelColor(0, COLOR_RED); //If the button input signal is "HIGH" (line 24), then the LED will light up (line 25)
}
else {
strip.setPixelColor(0, COLOR_NONE); //
}
strip.show(); //Set all pixels in the LED to "off". This resets the LED.
}
/*
Options for the LED:
COLOR_NONE,
COLOR_WARM,
COLOR_COLD,
COLOR_RED,
COLOR_ORANGE,
COLOR_YELLOW,
COLOR_GREEN,
COLOR_CYAN,
COLOR_BLUE,
COLOR_PURPLE,
COLOR_WHITE
*/
My current dilemma is that I want to now have my LED cycle through a few colors when I push the button. I tried adding a few lines into my if statement (see below) but it only showed the last LED color.
void loop(){
int buttonState = digitalRead(buttonPin); //Read the value from the buttonPin
if (buttonState == LOW) { //Why do we say "LOW" not "HIGH"? Resistors invert things!
strip.setPixelColor(0, COLOR_RED); //If the button input signal is "HIGH" (line 24), then the LED will light up (line 25)
delay(20);
strip.setPixelColor(0, COLOR_ORANGE);
delay(20);
strip.setPixelColor(0, COLOR_YELLOW);
}
else {
strip.setPixelColor(0, COLOR_NONE); //
}
strip.show(); //Set all pixels in the LED to "off". This resets the LED.
}
Thanks in advance for your help!
-Val