Hi Guys,
At the moment i'm trying to build something like a policecar. My idea is to let hem just drive around and that works fine. But what i want to add next is that when i clap or make some other hard noise the policecar starts doing something else.
So far i figured out how to do that but there is 1 problem. What i want is de policecar to go back to normal when i clap again.
In short Policecar should start at status1, when i clap change to status2 and when i clap again go back to status1.
I have been searching for similar codes but can't find anything that i understand.
Hope you guys can help me (my current code is below).
int stat = 0;
void setup() {
Serial.begin(9600);
pinMode(12, OUTPUT); // Blue LED
pinMode(13, OUTPUT); // Red LED
}
void loop() {
int sound = analogRead(A5); // A5 is sound detector
Serial.println(sound);
delay(10);
normal();
police();
}
void normal(){
int sound = analogRead(A5);
if (stat%2 == 0){
if (sound > 25){
stat = stat + 1;
delay(200);
}
else {
digitalWrite(13, HIGH);
delay(1000);
digitalWrite(13, LOW);
delay(1000);
}
}
}
void police(){
int sound = analogRead(A5);
if (stat%2 == 1){
if (sound > 25){
stat = stat + 1;
delay(200);
}
else{
digitalWrite(13, HIGH);
delay(500);
digitalWrite(13, LOW);
digitalWrite(12, HIGH);
delay(500);
digitalWrite(12, LOW);
delay(500);
}
}
}