Hello, I wish to use an interrupt code to sound a siren, if a car were to pass over an LDR when the light is red I wished to have a siren sound, while still having my basic code to transition between the traffic light colours run. I plan to use two LDRs and was wondering if anyone had any suggestions, regarding how I would accomplish this.
This is the code I have so far (it hasn't worked well):
int red1= 4; // the red light at the north light is declared at pin 2
int yellow1= 5; //the yellow light at the north light is declared at pin 3
int green1= 6; //the green light at the northt light is declared at pin 4
int red2= 7; //the red light at the east light is declared at pin 5
int yellow2= 8; //the yellow light at the east light is declared at pin 6
int green2= 9; //the green light at the east light is declared at pin 7
int LDR1= 2; // the LDR at the north light is declared at analong pin 1
int LDR2= 3; //the LDR at the east light is declared at analong pin 2
int speaker1= 10; // the speaker is declared at pin 8
void setup(){
Serial.begin(9600);
pinMode(3,INPUT);
digitalWrite(3, HIGH);
attachInterrupt(LDR2, Siren1, CHANGE);
pinMode(2,INPUT);
digitalWrite(2, HIGH);
attachInterrupt(LDR1, Siren2, CHANGE);
// the leds for the north light are declared as output
pinMode(red1,OUTPUT);
pinMode (yellow1,OUTPUT);
pinMode(green1, OUTPUT);
// the leds for the east light are declared as output
pinMode(red2, OUTPUT);
pinMode (yellow2,OUTPUT);
pinMode(green2, OUTPUT);
pinMode(LDR1, INPUT);// LDR2 is declared as input
pinMode(LDR2, INPUT); // LDR2 is declared as input
pinMode(speaker1, OUTPUT); //the speaker pin is declared as output
}
void Siren1(){
int LDR2_Status= analogRead(LDR2);
LDR2_Status= 0;
if (digitalRead(red2)==HIGH){
if (LDR2_Status<600){//if the LDR at the east light is <600
tone(speaker1, 960); //the speaker will sound the siren
delay(600);
tone(speaker1, 770);
delay(600);
tone(speaker1,960);
delay(600);
tone(speaker1, 770);
delay(600);
tone(speaker1,960);
delay(600);
tone(speaker1, 770);
delay(600);
noTone(speaker1);
}
}
}
void Siren2(){
int LDR1_Status= analogRead(LDR1);
LDR1_Status= 0;
if (digitalRead(red1)==HIGH){
if (LDR1_Status<600){ //if the LDR at the east light is <600
tone(speaker1, 960); //the speaker will sound the siren
delay(600);
tone(speaker1, 770);
delay(600);
tone(speaker1,960);
delay(600);
tone(speaker1, 770);
delay(600);
tone(speaker1,960);
delay(600);
tone(speaker1, 770);
delay(600);
noTone(speaker1);
}
}
}
void loop(){ // declared the function changeLight
int LDR1_Status= analogRead(LDR1);
int LDR2_Status= analogRead(LDR2);
LDR1_Status= 0;
LDR2_Status= 0;
//the north light will be green and the east light will be red
digitalWrite(green1, HIGH);
digitalWrite(yellow1, LOW);
digitalWrite(red1, LOW);
digitalWrite(green2, LOW);
digitalWrite(yellow2, LOW);
digitalWrite(red2, HIGH);
//program for sounding the siren
delay(35000); // the north light will be green and the east light will be red for 35 secs
//the north light will be yellow and the east light will be red
digitalWrite(green1, LOW);
digitalWrite(yellow1, HIGH);
digitalWrite(red1, LOW);
digitalWrite(green2, LOW);
digitalWrite(yellow2, LOW);
digitalWrite(red2, HIGH);
Serial.println(LDR2_Status);
delay(10000); // the north light will be yellow and the east light will be red for 10 secs
//the north light will be red and the east light will be green
digitalWrite(green2, HIGH);
digitalWrite(yellow2, LOW);
digitalWrite(red2, LOW);
digitalWrite(green1, LOW);
digitalWrite(yellow1, LOW);
digitalWrite(red1, HIGH);
delay(15000); // the north light will be red and the east light will be green for 15 secs
//the north light will be red and the east light will be yellow
digitalWrite(green1, LOW);
digitalWrite(yellow1, LOW);
digitalWrite(red1, HIGH);
digitalWrite(green2, LOW);
digitalWrite(yellow2,HIGH);
digitalWrite(red2,LOW);
delay(5000); // the north light will be red and the east light will be yellow for 5 secs
}