0% found this document useful (0 votes)
3 views

code

This document contains an Arduino code for controlling an LCD and traffic lights using an MCP23008-based LCD and LEDs. It manages the traffic light system for vehicles and pedestrians, displaying messages on the LCD and implementing countdowns for pedestrian waiting and crossing times. The code sets up the necessary pins, initializes the LCD, and uses a loop to alternate between vehicle and pedestrian signals based on button input.

Uploaded by

kanizmitu22
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

code

This document contains an Arduino code for controlling an LCD and traffic lights using an MCP23008-based LCD and LEDs. It manages the traffic light system for vehicles and pedestrians, displaying messages on the LCD and implementing countdowns for pedestrian waiting and crossing times. The code sets up the necessary pins, initializes the LCD, and uses a loop to alternate between vehicle and pedestrian signals based on button input.

Uploaded by

kanizmitu22
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

#include <Wire.

h>
#include <Adafruit_LiquidCrystal.h>

// Constructor for MCP23008-based LCD (0x20 is a common I2C address)


Adafruit_LiquidCrystal lcd(0); // 0 = address 0x20 by default

// Declare LEDs
const int greenLed = 8;
const int orangeLed = 9;
const int redLed = 10;

// Declare Push button


const int pushButton = 7;

void setup() {
// Set up LCD
lcd.begin(16, 2); // initialize 16x2 LCD
lcd.setBacklight(1); // turn on backlight

// Set LED pins


pinMode(greenLed, OUTPUT);
pinMode(orangeLed, OUTPUT);
pinMode(redLed, OUTPUT);

digitalWrite(greenLed, LOW);
digitalWrite(orangeLed, LOW);
digitalWrite(redLed, LOW);

// Set button pin


pinMode(pushButton, INPUT);

Serial.begin(9600);
}

void loop() {
int buttonState = digitalRead(pushButton);

if (buttonState == LOW) {
// Vehicles go
digitalWrite(greenLed, HIGH);
digitalWrite(orangeLed, LOW);
digitalWrite(redLed, LOW);

lcd.clear();
lcd.setCursor(0, 0);
lcd.print("VEHICLES GO");
lcd.setCursor(0, 1);
lcd.print("WAIT TO CROSS");

delay(1000);
} else {
// Pedestrian waiting
Serial.println("PEDESTRIAN WAITING");

digitalWrite(greenLed, LOW);
digitalWrite(orangeLed, HIGH);
digitalWrite(redLed, LOW);

// 5-second waiting countdown


for (int i = 5; i > 0; i--) {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("PEDESTRIAN");
lcd.setCursor(0, 1);
lcd.print("WAITING ");
lcd.print(i);
lcd.print(" sec");
delay(1000);
}

// Pedestrian crossing
Serial.println("PEDESTRIAN CROSSING");

digitalWrite(orangeLed, LOW);
digitalWrite(redLed, HIGH);

// 15-second crossing countdown


for (int i = 15; i > 0; i--) {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("PEDESTRIAN");
lcd.setCursor(0, 1);
lcd.print("CROSSING ");
lcd.print(i);
lcd.print(" sec");
delay(1000);
}

// After crossing, blink orange once


digitalWrite(redLed, LOW);
digitalWrite(orangeLed, HIGH);
delay(500);
digitalWrite(orangeLed, LOW);
delay(500);
}
}

You might also like