Random Color LED With Push Button

by paroth520 in Circuits > Arduino

301 Views, 0 Favorites, 0 Comments

Random Color LED With Push Button

Push Button - Random LED Selection

As a middle school teacher, I feel as if I am always trying to find ways to make games fun and engaging for students. Many of my students like competition based games. Others like skill based games. Others prefer luck based games where all have an equal chance of winning. One of the more popular games in my classroom is 4 Corners. 4 Corners is a game of chance, one student stands in the middle of the room while everyone else chooses a corner to stand at. The student calls out a corner and everyone who is in that corner must sit down.

The trouble with this game (and others) is students always try to fight the call. "They heard me moving to that corner!" "They peeked!" So, I decided to leave technology in charge. This project has a motion sensor to detect movement, an LED to provide a countdown, and random LED color selection to call the corner. My students helped me plan the game. A push of a button will trigger a timer. All students will then have 15 seconds to choose a corner. When time is up, a random color will light. All students in that color corner and any students not in a corner must sit down.

No arguing necessary when it's a computer that made the call!

This project was created using what I'd consider to be fairly novice code and circuitry building. I'd say that it is definitely a beginner level build because I am still very much a beginner in using Arduino so this was a good introductory project for me (and others) to practice the wiring and the coding.

Supplies

  • Arduino Uno R3
  • USB Cable to connect the Arduino Uno to your computer
  • 830 Tie Points Breadboard
  • 16x2 LCD
  • Push Button
  • Green LED
  • Blue LED
  • Yellow LED
  • Red LED
  • 6 220 Ohm Resistors
  • 22 Breadboard Jumper Wires

Build the Circuit

Brilliant Habbi.png
Super Hango-Migelo.png
Sizzling Migelo-Jarv (1).png
Sizzling Migelo-Jarv.png
Super Hango-Migelo (3).png

Arduino Uno R3 to Breadboard

For the initial setup, connect the ground row (-) on the breadboard to GND on the Uno R3. Connect the power row (+) to the 5V port on the Uno R3. Because there are so many connections, you'll need to connect the upper ground and power rows to their counterparts on the lower half.

Push Button

The push button is attached to the breadboard and will cross the middle of the board. It will be plugged into E-2 and 4 and F-2 and 4. Connect a jumper wire going from C-4 on the breadboard to pin 11 on the Arduino Uno. This pin number is used in the code so if you do attach the signal pin differently, make sure you adjust the code to the new number. Connect another jumper wire going from A-2 to the positive row on the breadboard. Use a 220 kOhm resistor to connect B-4 to the ground row.

LED Lights

You will connect your 4 LED lights to the breadboard. The order of the colors is unimportant because the code will randomly select a color each time. The LEDs are placed in G-2-9 on the breadboard. For each LED the shorter end is the cathode and will be connected to the ground row using a 220 kOHM resistor. To place the resisters, make sure one side of the resistor is in the same number column as the cathode of each LED. The other end of the resistor will connect to the ground row. The longer side on the LED is the anode and will be connected to pins 13, 12, 10, and 9 on the Arduino Uno using jumper wires.

LCD 16x2 Display

This display contains 16 pins and you will be using 12 of them. Moving from the edge to the center of the LCD, the labels are as follows: Ground (VSS), Power (VDD), Contrast (V0), Register Select (RS), Read/Write (RW), Enable (E), DB0, DB1, DB2, DB3, DB4, DB5, DB6, DB7, LED Anode (A), and LED Cathode (K). Place the display so the end pin (Ground) is connected to H-15 and the LED Cathode pin is in H-30. You will connect using jumper wires in row I, directly above each pin.

  • Connect the ground pin (VSS in column 15) to the ground row above it.
  • Connect the power pin (VDD in column 16) to the power row above it.
  • Connect Contrast (V0 in column 17) to the pin labeled A0 on the Arduino Uno - this pin will be labelled as 14 in the code.
  • Connect Register Select (RS in column 18) to pin 8 on the Arduino Uno.
  • Connect Read/Write (RW in column 19) to the ground row above it.
  • Connect Enable (E in row 20) to pin 7 on the Arduino Uno.
  • The next 4 pins (D0-D3) will be left blank.
  • Connect D4 (in column 25) to pin 6 on the Arduino Uno.
  • Connect D5 to pin 5 on the Arduino Uno.
  • Connect D6 to pin 4 on the Arduino Uno.
  • Connect D7 to pin 3 on the Arduino Uno.
  • Connect LED Anode (A in column 29) to pin 2 on the Arduino Uno.
  • Using a 220 kOHM resistor, connect LED Cathode (K in column 30) to the ground row above it.

The Code

#include <LiquidCrystal.h>
LiquidCrystal lcd(8,7,6,5,4,3);

int buttonState = 0;

#define contra 14
//sets the contrast levels to pin 14 (A0)
  
#define bri 2
//sets LED Anode to pin 2
  int choice;

void setup()
{

    lcd.begin(16,2);
  pinMode(contra,OUTPUT);
pinMode(bri, OUTPUT);
digitalWrite(contra, LOW);
analogWrite(bri,255);
  pinMode(11, INPUT);
}

void loop()
{
  buttonState = digitalRead(11);

  if(buttonState == HIGH){

      lcd.print("15!"); //this tells what word to display
lcd.setCursor(0,0); //the tells the start point row,column
delay(1000);
lcd.clear(); //clears the screen
delay (100);
lcd.print("14");
lcd.setCursor(0,0);
delay(1000);
lcd.clear();
delay(100);
lcd.print("13");
lcd.setCursor(0,0);
delay(1000);
lcd.clear();
delay(100);
lcd.print("12");
lcd.setCursor(0,0);
delay(1000);
lcd.clear();
delay(100);
lcd.print("11");
lcd.setCursor(0,0);
delay(1000);
lcd.clear();
delay(100);
lcd.print("10!");
lcd.setCursor(0,0);
delay(1000);
lcd.print("9!");
lcd.setCursor(0,0);
delay(1000);
lcd.print("8!");
lcd.setCursor(0,0);
delay(1000);
    lcd.print("7!");
lcd.setCursor(0,0);
delay(1000);
    lcd.print("6!");
lcd.setCursor(0,0);
delay(1000);
    lcd.print("5!");
lcd.setCursor(0,0);
delay(1000);
    lcd.print("4!");
lcd.setCursor(0,0);
delay(1000);
    lcd.print("3!");
lcd.setCursor(0,0);
delay(1000);
    lcd.print("2!");
lcd.setCursor(0,0);
delay(1000);
    lcd.print("1!");
lcd.setCursor(0,0);
delay(1000);
    lcd.print("0!");
lcd.setCursor(0,0);
delay(1000);
lcd.clear();
delay(1000);
lcd.clear(); //clears the screen 
   
choice = random(4);
    
switch(choice){ 
        case 0: digitalWrite(12, HIGH);
          digitalWrite(13, LOW);
          digitalWrite(10, LOW);
          digitalWrite(9, LOW);
        break; 
        case 1:digitalWrite(12, LOW);
          digitalWrite(13, HIGH);
          digitalWrite(10, LOW);
          digitalWrite(9, LOW); 
        break; 
        case 2: digitalWrite(12, LOW);
          digitalWrite(13, LOW);
          digitalWrite(10, HIGH);
          digitalWrite(9, LOW); 
        break; 
        case 3: digitalWrite(12, LOW);
          digitalWrite(13, LOW);
          digitalWrite(10, LOW);
          digitalWrite(9, HIGH); 
 
      } //end of switch 
  delay(3000); // Delay a little bit to allow light to show
  
  // turn off light
  digitalWrite(12, LOW);
          digitalWrite(13, LOW);
          digitalWrite(10, LOW);
          digitalWrite(9, LOW);
  }

    } //close the loop

The Code Explained

The first portion of the code is declaring variables that will be used.

  • #include states that the library to use the LCD display should be included. If you are using the Arduino Web Editor, click on the Library tab and include the LiquidCrystal library. If you are using the Arduino application, choose Sketch->Include Library->Liquid Crystal.
  • LiquidCrystal lcd(8,7,6,5,4,3); declares determines the pin locations for the lcd screen. The order of the lcd pins is register select, enable, db4, db5, db6, db7
  • #define contra 14 sets the contrast levels to pin 14 (A0) .
  • #define bri 2 sets LED Anode to pin 2.
  • int choice; is used to declare the variable "choice" which will be used later in the code to randomize the light color choice.

After that is the setup portion of the code.

  • lcd.begin(16,2); states we are using a 16x2 display.
  • pinMode(contra,OUTPUT); sets the contrast pin we declared earlier as an output.
  • pinMode(bri, OUTPUT); sets the brightness pin earlier as an input.
  • digitalWrite(contra, LOW); and analogWrite(bri,255); allow us to clearly see the words on the display.
  • pinMode(11, INPUT) tells the board that the signal from the push button should be received from pin 11.

The final section is the loop. This will run continuously and includes the actions taken by the code.

  • buttonState = digitalRead(11); is used to specify that pin 11 contains the information about the push button
  • Conditional Statement if (buttonState == HIGH) { is used to tell the board that the following code should only occur if the button is pushed.
  • Start a 15 second countdown on the display.
    • lcd.begin(16,2); initializes the 16X2 LCD
      • screenlcd.print("15!"); tells what phrase to display
      • lcd.setCursor(0,0); tells the start point by row and column
      • delay(1000); leaves the phrase up for 1 second (1000 milliseconds)
      • lcd.clear(); clears the screen delay (100);
      • Repeat this code changing the phrase each time to demonstrate a countdown (15, 14, 13, 12, 11...)
    • Once zero is reached use lcd.clear(); to clear the screen
    • Next is the random color choice of the lights.
      • choice = random(4); is used to say that the board should choose randomly between the 4 options listed.
      • switch(choice){ is used to indicate the choices will follow; they are listed as cases 0-3.
        • case 0: digitalWrite(12, HIGH); digitalWrite(13, LOW); digitalWrite(10, LOW); digitalWrite(9, LOW); break; is written to turn the green light on and leave the rest off.
        • case 1:digitalWrite(12, LOW); digitalWrite(13, HIGH); digitalWrite(10, LOW); digitalWrite(9, LOW); break; is written to turn the red light on and leave the rest off.
        • case 2: digitalWrite(12, LOW); digitalWrite(13, LOW); digitalWrite(10, HIGH); digitalWrite(9, LOW); break; is written to turn the yellow light on and leave the rest off.
        • case 3: digitalWrite(12, LOW); digitalWrite(13, LOW); digitalWrite(10, LOW); digitalWrite(9, HIGH); is written to turn the blue light on and leave the rest off.
        • } //end of switch } shows that all of the possible options have been listed
      • delay(3000); means the light will stay on for 3 seconds
      • digitalWrite(12, LOW); digitalWrite(13, LOW); digitalWrite(10, LOW);digitalWrite(9, LOW); } turns all the lights off and closes the loop.

You can also view the full code in the Arduino Web Editor here.

Upload the Code

Once your circuit is full built and your code is completed and verified, you will connect the Arduino Uno R3 to your computer using a USB cable. Upload the code onto your Uno. Some uploads take a minute or so to be fully in place. Once you have uploaded the code, you will be able to push the button to initiate the countdown on the display. When the countdown is complete, a random LED will light up.