COMP 1045 Lab 5
Circuit diagram: For this lab we will be using a button to control LED lights. Please setup
the following circuit and run the code provided in level 1.
Level 1: Run the following code to make sure your button works.
int blueLedPin = 13 ; //The blue LED is connected pin 13 of the Arduino.
int button1Pin = 2; //The SW1 button is connect to pin 2 of the Arduino.
void setup() { //The Setup function runs once
pinMode(blueLedPin, OUTPUT); //Setup blue LED pin as an output pin.
pinMode(button1Pin, INPUT); //Setup button1 pin as an input pin.
}
void loop() { //The loop function runs forever.
if (digitalRead(button1Pin) == HIGH) { //Check to see if button1 is pressed.
digitalWrite(blueLedPin, HIGH);//Turn on the blue LED.
} else {
digitalWrite(blueLedPin, LOW); //Turn off the blue LED.
}
}
Level 2: Add a second button that will control a red led.
Level 3: Using the RGB LED, have button 1 turn on the RGB light, cycling through the three
colours in repetition(ex:Red→ Green→ Blue → Red→ ect.) with a 500 msec delay. Then use
the second button to shut off the lights. The light should shut off immediately after the current
colour and not continue to cycle to other colours after the button is pressed. There can be a 500
ms button press to shut off lights. Reference #1
Level 4: Add a green and red LED to the circuit with the RGB LED. Include 2 buttons. The first
button will toggle between the red and green LED. If the green LED is on when you hit button 2
the RGB will start cycling through the 3 colours. If the 2nd button is pressed the RGB lights stop.
If the 1st button is pressed the RGB stops AND the green LED turns off and the red LED turns
ON. If the red LED is on, nothing happens when you press the 2nd button. Here's the sequence
used to test your code
ACTION Green/Red LED RGB LED
Start Red OFF
Button #1 Pressed Green OFF
Button #1 Pressed Red OFF
Button #2 Pressed Red OFF
Button #1 Pressed Green OFF
Button #2 Pressed Green ON (cycling)
Button #2 Pressed Green OFF
Button #2 Pressed Green ON (cycling)
Button #1 Pressed Red OFF
End testing
Note: For full marks you must use at least 1 interrupt function during either
level 3 or level 4 code.
Extra Challenge: In this challenge, you will enhance the circuit from Level 4 by allowing the
user to input a sequence of colors using the letters RGBCMYW. The RGB LED will then display
the colors in the sequence entered by the user. Begin with the circuit from Level 4. After
receiving the input, check if the entered string consists only of the valid letters (RGBCMYW). If
the input is invalid, prompt the user to enter a new string.
If the green LED from Level 4 is ON:
When the user provides a valid input, the RGB LED should start cycling through the colors in
the sequence provided by the user.
If the 2nd button from Level 4 is pressed, the RGB lights stop cycling.
If the 1st button from Level 4 is pressed, the RGB lights stop cycling, the green LED turns off,
and the red LED turns ON.
If the red LED from Level 4 is ON:
The RGB LED should not display any colors, regardless of the user input. The 2nd button has
no effect in this state.
Allow the user to input a new sequence of colors after the sequence has been interrupted by the
buttons.