#include <Arduino.h>
// Define the GPIO pins for the LEDs and the push button
const int redLED = 3; // GP3
const int greenLED = 0; // GP0
const int orangeLED = 2; // GP2
const int buttonPin = 1; // GP1
const int crossWalkGreenLED = 28; // GP28
const int crossWalkRedLED = 21; // GP21
const int LDRPin = 15; // GP15 for LDR AO
int LDRValue = 0;
bool isDay = false;
void setup() {
// Initialize serial communication
Serial1.begin(115200);
Serial1.println("Hello, Raspberry Pi Pico!");
// Set the LED pins as outputs
pinMode(greenLED, OUTPUT);
pinMode(orangeLED, OUTPUT);
pinMode(redLED, OUTPUT);
pinMode(crossWalkGreenLED, OUTPUT);
pinMode(crossWalkRedLED, OUTPUT);
// Set the button pin as input with pull-down resistor
pinMode(buttonPin, INPUT_PULLDOWN);
}
void loop() {
// Read LDR sensor value
LDRValue = analogRead(LDRPin);
// Determine day/night condition
if (LDRValue > 512) {
isDay = true;
} else {
isDay = false;
}
isDay = true;
// Daytime operations
if (isDay) {
// Check if the button is pressed and it's day
if (digitalRead(buttonPin) == LOW) {
// Crosswalk sequence
digitalWrite(greenLED, LOW); // Turn off green LED
digitalWrite(orangeLED, LOW); // Turn off orange LED
digitalWrite(redLED, HIGH); // Turn on red LED
digitalWrite(crossWalkGreenLED, HIGH); // Turn on CrossWalk Green LED
digitalWrite(crossWalkRedLED, LOW); // Turn off CrossWalk Red LED
delay(8000); // Keep the LED on for 8 seconds
digitalWrite(crossWalkGreenLED, LOW); // Turn off CrossWalk Green LED
digitalWrite(crossWalkRedLED, HIGH); // Turn on CrossWalk Red LED
delay(5000); // Keep the LED on for 5 seconds
digitalWrite(redLED, LOW); // Turn off red LED
}
else {
// Regular traffic light sequence if it's not day
digitalWrite(greenLED, HIGH); // Turn on green LED
delay(5000); // Keep the LED on for 5 seconds
digitalWrite(greenLED, LOW); // Turn off green LED
digitalWrite(orangeLED, HIGH); // Turn on orange LED
delay(1000); // Keep the LED on for 1 second
digitalWrite(orangeLED, LOW); // Turn off orange LED
digitalWrite(redLED, HIGH); // Turn on red LED
delay(4000); // Keep the LED on for 4 seconds
digitalWrite(redLED, LOW); // Turn off red LED
}
}
// Nighttime operations
else {
digitalWrite(redLED, LOW); // Turn off red LED
digitalWrite(greenLED, LOW); // Turn off green LED
digitalWrite(orangeLED, HIGH); // Turn on orange LED
delay(1000); // Keep the LED on for 1 second
digitalWrite(orangeLED, LOW); // Turn off orange LED
digitalWrite(crossWalkRedLED, LOW); // Turn off crosswalk red LED
digitalWrite(crossWalkGreenLED, HIGH); // Turn on crosswalk green LED
delay(1000); // Keep the LED on for 1 second
}
}