Hey every one,
Im new to the arduino and I was given two separate assignments where I pulled some code out of a book, they were both successful now Im trying to create an actual fully functional traffic light. The gist is I want a traffic light to run under a certain loop constantly but then when a button is pressed I want it to give the green light a set amount of time before turning on a pedestrian light. Say in the normal loop the light is green for 15s and I press the button at 5s, have the light stay on another 5s before changing to red to allow for a pedestrian. run this loop once then move back to the normal light. Im using an interrupt function (which Im still wrapping my head around) and you might be able to see what I had in mind, or I might have just created a hack job. Ill paste my code below and any other hints to help me expedite use of this forum would be welcome with much appreciation.
Thank you,
// Project 4 - Interactive Traffic Lights
int carRed = 10; // assign the car lights
int carYellow = 9;
int carGreen = 8;
int pedRed = 11; // assign the pedestrian lights
int pedGreen =12;
int button = 2; // button pin
int crossTime = 5000; // time allowed to cross
int ledDelay = 5000;
unsigned long changeTime; // time since button pressed
// declare state, volatile has to do w/interrupt
volatile int state = LOW;
void setup() {
pinMode(carRed, OUTPUT);
pinMode(carYellow, OUTPUT);
pinMode(carGreen, OUTPUT);
pinMode(pedRed, OUTPUT);
pinMode(pedGreen, OUTPUT);
pinMode(button, INPUT); // button on pin 2
// turn on the green light
digitalWrite(carGreen, HIGH);
digitalWrite(pedRed, HIGH);
/* RISING cause when ur button is pressed it changes from LOW to HIGH to LOW, so rly any of the commands wld do
but if you had a sit like flipping a switch then you wld have to choose the correct one */
attachInterrupt(0, buttonPressed, RISING);
}
void loop() {
if (state == LOW){
normalOperation();}
/* check if button is pressed and if it is
over 5 seconds since last button press /
/ Added another 2 brackets around the second part cause the >5000 part wasnt inside em */
if (state == HIGH) {
// Call the function to change the lights
changeLights();
}
void normalOperation(){
delay(10000);
digitalWrite(carGreen, LOW);
digitalWrite(carRed, LOW);
digitalWrite(carYellow, LOW);
digitalWrite(pedGreen, LOW);
digitalWrite(pedRed, LOW);
digitalWrite(carRed, HIGH);
digitalWrite(pedGreen, HIGH);
delay(ledDelay);
digitalWrite(pedGreen, LOW);
for (int x=0; x<10; x++) {
digitalWrite(pedGreen, HIGH);
delay(250);
digitalWrite(pedGreen, LOW);
delay(250);
}
digitalWrite(carRed, LOW);
digitalWrite(carGreen, HIGH);
digitalWrite(pedRed, HIGH);
delay(ledDelay);
digitalWrite(carGreen, LOW);
digitalWrite(carYellow, HIGH);
delay(3000);
digitalWrite(carYellow, LOW);
digitalWrite(pedRed, LOW);
}
void changeLights() {
delay(5000);
digitalWrite(carGreen, LOW);
digitalWrite(carRed, HIGH); // red on
delay(1000); // wait 1 second till its safe
digitalWrite(pedRed, LOW); // ped red off
digitalWrite(pedGreen, HIGH); // ped green on
delay(crossTime); // wait for preset time period
// flash the ped green
for (int x=0; x<10; x++) {
digitalWrite(pedGreen, HIGH);
delay(250);
digitalWrite(pedGreen, LOW);
delay(250);
}
// turn ped red on
digitalWrite(pedRed, HIGH);
delay(500);
digitalWrite(carYellow, HIGH); // yellow on
digitalWrite(carRed, LOW); // red off
delay(1000);
digitalWrite(carGreen, HIGH);
digitalWrite(carYellow, LOW); // yellow off
// record the time since last change of lights
changeTime = millis();
// then return to the main program loop
/* return state to low after this loop so that the normal operation will run again and someone cant just keep hitting the button and stay in pedestrian mode. */
state = LOW;
}
//**** Added function to change state to high w/buttonpress
void buttonPressed(){
state = HIGH;}