I have a board set up with two sets of traffic light, Green Yellow Red and an additional Green for a turn signal. I plan to use a 3 button function. 1st button interrupts sequence for the North turn arrow (second green light) to come on and blink then resumes sequence. This part works fine with the following code. When I try to add the second button to do the same thing with the East light, I get errors every way I try. The 3rd button will interrupt for both red lights to blink but I am focused on getting the 2nd one working first. Any suggestions?
const int maindelay = 1000;
const byte red = 0; //first column in array signal is red
const byte amber = 1; //second column in array signal is amber
const byte green = 2; //third column in array signal is green
const byte totalcolours = 3; //there are red, amber and green
const byte north = 0; //first row in array signal is north
const byte east = 1; // second row in array signal is east
const byte totalsignals = 2;
const int signal[totalsignals][totalcolours] ={
{ 6, 5, 4},
{ 10, 9, 8}
};
byte signalID = 0; // the number of the "active" signal
//turn arrow variables and constants
volatile byte TurnRequest = 0;
const byte Button = 2;
const byte Button2 = 3;
const byte Arrow2 = 11;
const byte Arrow1 = 7;
void setup() {
//set up serial output
Serial.begin(9600);
// set up the signals
for (byte i = 0; i < totalsignals; i++) //cycle through the signals
{
for (byte j = 0; j < totalcolours; j++) //cycle through the colours
{
pinMode(signal[i][j],OUTPUT); //set the pins as outputs
Serial.print("setting pin ");
Serial.print( signal[i][j]);
Serial.println(" as output");
}
}
//set the signals to an initial state
for (byte i = 0; i < totalsignals; i++) //cycle through the signals
{
digitalWrite(signal[i][color=red],HIGH); //light all the red lights
Serial.print("setting signal ");
Serial.print( i);
Serial.println( " to red");
}
//set up the turn arrow and signal
Serial.println("setting up turn lights");
pinMode(Button,INPUT); //set the button as an input
digitalWrite(Button, HIGH); //inturrupt for North
pinMode(Arrow1,OUTPUT); //Set the North turn light as an output
pinMode(Button2,INPUT); //set the button2 as an input
digitalWrite(Button2, HIGH); //inturrupt for East
pinMode(Arrow2,OUTPUT); //Set the East turn light as an output
attachInterrupt(0, ArrowButton, CHANGE);
attachInterrupt(0, ArrowButton2, CHANGE);
}
void loop()
{
if(signalID < totalsignals)
{
//make sure a valid signal is the active one.
settogreen(signalID); //turn the signal called signalID to green
delay(maindelay*2);
settored(signalID); //turn the lights back to red
delay(maindelay);
if(TurnRequest ==1) //see if the North turn button has been pressed
{
Turn1(); //if it has, go to the turn arrow function
}
signalID++; //cycle through the signals
}
else
{
signalID=0; //repeat cycling through all the signals
}
}
void settogreen(byte i)
{
//function to set a signal to green
Serial.print("Changing signal ");
Serial.print(i);
Serial.println(" to green");
delay(maindelay);
digitalWrite(signal[i][color=green],HIGH);
digitalWrite(signal[i][color=red],LOW);
}
void settored(byte i)
{
//function to set a signal to red
Serial.print("Changing signal ");
Serial.print(i);
Serial.println(" to red");
digitalWrite(signal[i][amber],HIGH);
digitalWrite(signal[i][color=green],LOW);
delay(maindelay);
digitalWrite(signal[i][color=red],HIGH);
digitalWrite(signal[i][amber],LOW);
}
void ArrowButton()
{
//function for when North button is pressed
TurnRequest = 1;// now there is a request to turn
Serial.println( "Turn request received");
}
void Turn1()
{
// Turn light function
Serial.println( "Turn light on");
delay(maindelay);
Serial.println( "Turn now");
digitalWrite(Arrow1, HIGH);
delay(maindelay*2);
Serial.println( "Turn time is up, flashing green");
for (int i=1; i<6; i++)
{
//run the next code 5 times to flash the green turn arrow
digitalWrite(Arrow1,LOW);
delay(200);//Pause
digitalWrite(Arrow1,HIGH);
delay(200);//Pause
}
digitalWrite(Arrow1, LOW);
Serial.println( "No Turn");
delay(maindelay);
TurnRequest = 0; // clear turn request
}
void ArrowButton2()
{
//function for when turn button is pressed
TurnRequest = 1; // active turn request
Serial.println( "turn request received");
(Mod edit)