New to Arduino - HELP!

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

1 Like

Doesn't that set come with a book of projects?

change your delay to 2000

Yes, and they are cool. But this is my students' second exposure to syntax-based coding, so I wanted something very pared down.

I did and it only displays the final color still. With a longer display, of course =)

Yeah, you should increase the delay. And you said it only shows the last color. This could also be happening because you used an if statement, I believe. I think that it keeps it yellow because it keeps the if statement true and that is the last action there. It is yellow because it doesn't know what to do else. I don't know for sure, but perhaps a while statement instead would work? --> it keeps repeating while it is true so it will go back to red and orange and yellow indefinetly...

I'm sorry if it doens't work like that, I'm quite the newbie myself.

does stripshow do what you think

strip.show(); 

You have to show after each color change, before the delay.

2 Likes

Amazing! Thank you!! IT WORKED!!!

2 Likes

Thanks for marking your question solved. Not enough new users do this.
Glad it's all working

1 Like

14 posts were split to a new topic: Buttons low. LEDs high. Or tis it other way round?

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.