/************************************************************************************************
* LAB TEST 2025 - TRAFFIC LIGHTS *
* *
* Student Name: Zainab Khan *
* *
* Description: You will need to wire and code the following program: *
* Attach all items (breadboard is optional). When the program runs, *
* a green, yellow then red light should flash (none at the same time). When *
* the red light is on, a STOP! message should be displayed on the LCD screen, a SLOW! *
* message when the yellow light is on and a GO! message when the green light is on. *
* These messages should flash on and off (a blank screen between messages). *
* The speed that you cycle through the lights/delays should be controlled by the *
* potentiometer. The delay between lights should range between 200-500 milliseconds. *
* The program must be properly commented, indented and variables must be created *
* using proper naming procedures. You must use PORTB and DDRB commands when setting up and *
* controlling the LED's. The LiquidCrystal I2C library has been added for you. *
* *
* GOOD LUCK! *
* *
***********************************************************************************************/
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2); // I2C address 0x27, 16 columns and 2 rows
int potPin = A3; // Potentiometer is connected to A5
int potVal; // variable to store potentiometer reading
int lightSpeed; // variable for the delay between lights
void setup() {
lcd.init(); // initialize the lcd
lcd.backlight(); // turn on backlight
DDRB = B111111; // setting pins 8 - 13 to outputs
Serial.begin(9600);
} //end of void setup
void loop() {
potVal = analogRead(potPin); // read the value from the potentiometer - range (0-1023)
lightSpeed = map(potVal, 0, 1023, 200, 500); // changes the 0-1023 reading to a 200-500 millisecond scale
//displaying lightSpeed
Serial.print("lightSpeed: ");
Serial.print(lightSpeed);
Serial.println(" ");
lcd.setCursor (5,0); // move the cursor to second column in top row
lcd.print("GO!"); // print on lcd "GO!"
lcd.print(" ");
PORTB = B001000; // turns green LED on
delay(lightSpeed);
lcd.setCursor (5,0); // move the cursor to 5th column in top row
lcd.print(" ");
delay(lightSpeed);
lcd.setCursor (5,0); // move the cursor to 5th column in top row
lcd.print("SLOW!"); // print on lcd "SLOW!"
lcd.print(" ");
PORTB = B010000; // turns yellow LED on
delay(lightSpeed);
lcd.setCursor (5,0); // move the cursor to 5ht column in top row
lcd.print(" ");
delay(lightSpeed);
lcd.setCursor (5,0); // move the cursor to 5th column in top row
lcd.print("STOP!"); // print on lcd "STOP!"
lcd.print(" ");
PORTB = B100000; // turns red LED on
delay(lightSpeed);
lcd.setCursor (5,0); // move the cursor to 5th column in top row
lcd.print(" ");
delay(lightSpeed);
} // end of void loop