• 1
    Step 1

    Each press of the button cycles the LED colors. It turns off after red.

    Just wire it up and upload the following code.

  • 2
    Step 2
    int redPin = 3;
    int greenPin = 5;
    int bluePin = 6;
    int buttonPin = 2;
    int initialCount = 0;
    
    //uncomment this line if using a Common Anode LED
    #define COMMON_ANODE
    
    void setup()
    {
      pinMode(redPin, OUTPUT);
      pinMode(greenPin, OUTPUT);
      pinMode(bluePin, OUTPUT);
      pinMode(buttonPin, INPUT_PULLUP);
       setColor(0,0,0);
    }
    
    void loop()
    {
      int buttonState = digitalRead(buttonPin);
      if ( buttonState == LOW ){
        delay(500); //delay for debounce
        initialCount++;
        if (initialCount > 4 ){
          initialCount = 0;
        }
        switch (initialCount){
          case 0:
            setColor(0,0,0);
            break;
          case 1:
           setColor(0, 255, 0);  // green
            break;
          case 2:
            setColor(0, 255, 255);  // aqua
            break;
          case 3:
           setColor(0, 0, 255);  // blue
            break;
          case 4:
           setColor(255, 0, 0);  // red
           break;
        }
    
      }
    
    }
    
    void setColor(int red, int green, int blue)
    {
      #ifdef COMMON_ANODE
        red = 255 - red;
        green = 255 - green;
        blue = 255 - blue;
      #endif
      analogWrite(redPin, red);
      analogWrite(greenPin, green);
      analogWrite(bluePin, blue);  
    }
  • 3
    Step 3