Learn Arduino with Olympia Circuits
Learn Arduino
  • Home
    • Get Started
    • How to Use This Site
  • Electronics
    • The Basics
    • Electricity Flows like Water
    • Electronic Components
    • The Arno Board
  • Programming
    • The Basics
    • setup and loop Blocks
    • Variables and Arrays
    • Connecting with the Pins
    • Flow Control >
      • if Statement
      • Loops
      • Delays
    • Functions
    • Serial Communication
    • USB
    • Some Thoughts
  • Projects
    • Projects 1 >
      • 1.01: Blink
      • 1.02 Blink x2
      • 1.03 Blink Faster
      • 1.04 LED Chase!
      • 1.05 Wait To Blink
      • 1.06 Blink a Little Faster Now
      • 1.07 LED Fade
      • 1.08 RGB Blink
      • 1.09 Change RGB Color with SW1
      • 1.10 Fade RGB Colors
      • 1.11 Reaction Time Game
    • Projects 2 >
      • 2.01 Hello World
      • 2.02 Talk Back
      • 2.03 ASCII Values
      • 2.04 Ski Game
      • 2.05 Demonstration of the String Object
    • Projects 3 >
      • 3.01 Read the Potentiometer
      • 3.02 ASCIIbet Soup
      • 3.03 Potentiometer sets LED Brightness
      • 3.04 Potentiometer Sets Blink Rate
      • 3.05 LED Chase, Part II
    • Projects 4 >
      • 4.01 Bringing the Piezo to Life
      • 4.02 Controlling the Piezo with a Function
      • 4.03 Piezo C Major
      • 4.04 Piezo Greensleaves
      • 4.05 Piezo Metronome
      • 4.06 Piezo as an Input
      • 4.07 Piezo as an Input 2
      • 4.08 Metronome II
      • 4.09 Piezo Playback
      • 4.10 Piezo Fireworks
      • 4.11 Piezo Mosquito
    • Projects 5 >
      • 5.01 The Phototransistor
      • 5.02 Light and Sound
      • 5.03 Light and Sound II
    • Projects 6 >
      • 6.01 EEPROM
      • 6.02 I2C Address Scan
      • 6.03 Read the I2C Temperature Sensor
      • 6.04 High Temperature Alarm
    • Projects 7 >
      • 7.01 Arno Phone Home
      • 7.02 Keyboard Alphabet
      • 7.03 Move Mouse
      • 7.04 Draw Squares
    • Special Projects >
      • Bike Light Demo
  • References
    • Arno Pin Key
    • Arno Schematic
    • Project Index

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:
  • Circuit 3


  • Select Sketch

    ///////////////////////////////////////////////////
    //Project 1.09 Change RGB Color when SW1 is pressed
    int RGB[3] = {9, 10, 11};
    int thisLED = 0;
    int SW1 = 1;
    int isOn = 1;
    int isPress = HIGH;
    long lastPressed = 0;
    long lastBlink = 0;
    int blinkSpeed = 1000;
    void setup(){
    for(int k = 0; k<3; k++){
    pinMode(RGB[k],OUTPUT);
    }
    pinMode(SW1,INPUT);
    }
    void loop(){
    if(millis() > (lastBlink + blinkSpeed)){
    isOn = 1 - isOn;
    lastBlink = millis();
    digitalWrite(RGB[thisLED],isOn);
    }
    //now check the button
    isPress = digitalRead(SW1);
    if(isPress == LOW && millis() > (lastPressed + 1000)){
    digitalWrite(RGB[thisLED],LOW); //turn current LED off
    thisLED ++;
    if(thisLED > 2) thisLED = 0;
    digitalWrite(RGB[thisLED],isOn);
    lastPressed = millis();
    }
    }
    ///////////////////////////////////////////////////

    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:

      } 

    }


    Back to Projects 1

    Copyright Olympia Circuits LLC 2014. All Rights Reserved.