-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
86 lines (69 loc) · 2.16 KB
/
main.cpp
File metadata and controls
86 lines (69 loc) · 2.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#include <Arduino.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
// OLED Setup
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
#define OLED_RESET -1
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
// LED Pins
const int greenLED = 18;
const int yellowLED = 7;
const int redLED = 19;
// Button Pins
const int button1 = 0; // Change from GPIO 0 to GPIO 4
const int button2 = 1; // Change from GPIO 1 to GPIO 5
bool pedestrianWaiting = false;
unsigned long lastPressTime = 0;
void setup() {
Serial.begin(115200);
Wire.begin(9, 8);
pinMode(greenLED, OUTPUT);
pinMode(yellowLED, OUTPUT);
pinMode(redLED, OUTPUT);
pinMode(button1, INPUT_PULLUP);
pinMode(button2, INPUT_PULLUP);
Serial.println("We are ready");
display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(SSD1306_WHITE);
display.setCursor(0, 10);
display.println("Traffic light ready");
display.display();
}
// Traffic Light Function
void TrafficLightSys() {
if (pedestrianWaiting) {
Serial.println("Pedestrian button pressed! Switching to red.");
digitalWrite(greenLED, LOW);
digitalWrite(yellowLED, LOW);
digitalWrite(redLED, HIGH);
delay(3000); // Pedestrian crossing time
pedestrianWaiting = false; // Reset pedestrian request
}
Serial.println("Normal traffic cycle running...");
digitalWrite(redLED, HIGH);
delay(3000);
digitalWrite(yellowLED, HIGH);
digitalWrite(redLED, LOW);
delay(1500);
digitalWrite(yellowLED, LOW);
digitalWrite(greenLED, HIGH);
delay(3000);
digitalWrite(yellowLED, HIGH);
digitalWrite(greenLED, LOW);
delay(1500);
digitalWrite(yellowLED, LOW);
}
void loop() {
if (digitalRead(button1) == LOW) {
if (millis() - lastPressTime > 300) { // Debounce logic
lastPressTime = millis();
Serial.println("Button Press Detected!");
pedestrianWaiting = true;
}
}
TrafficLightSys();
}