Question
I had to do an arduinos code for traffic light. I came up with this sketch but it says there is an error. My circuit
I had to do an arduinos code for traffic light. I came up with this sketch but it says there is an error. My circuit has 10 less and two buttons. So, there is 3 traffic lights and one pedestrian light. Can anyone help me figure this out or help me do the code.
int cross= 4; int red1= 13; int yellow1= 12; int green1= 11; int red2= 10; int yellow2= 9; int green2= 8; int red3= 7; int yellow3= 6; int green3= 5; int button= 3; int red1_time= 5; int yellow1_time= 3; int green1_time= 5; int red2_time= 5; int yellow2_time= 3; int green2_time= 5; int red3_time= 5; int yellow3_time= 3; int green3_time= 5; int cross_time= 5; int Delay_time= 500;
volatile int button_state=0; int i=0; int Flag; int SetFlag; int Pedestian; int Light_State;
void setup() { pinMode (cross, OUTPUT); pinMode (red1,OUTPUT); pinMode (yellow1, OUTPUT); pinMode (green1, OUTPUT); pinMode (red2, OUTPUT); pinMode (yellow2, OUTPUT); pinMode (green2, OUTPUT); pinMode (red3, OUTPUT); pinMode (yellow3, OUTPUT); pinMode(green3, OUTPUT); pinMode (button, INPUT); attachInterrupt (0, SetFlag, CHANGE); Serial.begin(9600); Flag=0;
}
void loop() { LOOP: //Set Green1 ON, red1 and yellow1 OFF //Green State digitalWrite (red1, LOW); digitalWrite (yellow1, LOW); digitalWrite(green1, HIGH); digitalWrite (cross, LOW); for (i=0; i<=green1_time; i++) {delay (Delay_time); if (Flag==1){ Light_State=1; Pedestian (); goto LOOP; } } //Yellow State digitalWrite (green1, LOW); digitalWrite (yellow1, HIGH); for (i=0; i<=red1_time; i++); { delay (Delay_time); if (Flag==1){ Light_State=2; Pedestian (); goto LOOP; } } // Red State digitalWrite (yellow1, LOW); digitalWrite (red1, HIGH); for ( i=0; i<=yellow1_time; i++) { delay (Delay_time); if (Flag==1){ Light_State=3; Pedestian(); goto LOOP; } } void SetFlag (void){ Flag=1; } void Pedestrian (void){ switch (Light_State){ case1:{ digitalWrite(green1, LOW); digitalWrite (yellow1, HIGH); delay (2000); //Set red on and pedestrian on digitalWrite (yellow1, LOW); digitalWrite (red1, HIGH); digitalWrite (cross, HIGH); delay (5000); break; } case 2: {//Yellow State // delay(500);//yellow for 1/2 second delay(1000); //Red on and Pedestrian on digitalWrite (yellow1, LOW); digitalWrite (red1, HIGH); digitalWrite (cross, HIGH); delay (5000); break; } case 3:{//Pedestrian on, red on digitalWrite (red1, HIGH); digitalWrite (cross, HIGH); delay (3000); break; } default:{}; } for (i=0; i<=5;i++){ digitalWrite (cross, LOW); delay(1000); digitalWrite (cross, HIGH); delay(1000); } Flag=0; } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started