My project is a button controlled Siren with different LED transitions. You can change the siren sound (e.g. police or ambulance siren, etc.) for each press. I have added 2 different LED patterns for each siren using 10 LEDs. I have added a total of 4 Siren tones. You can checkout the Arduino code and the explanation here:
Code Explained1. Since we are using a button press to switch between the tones, we have to remove the problem of button debouncing, which I removed by software implemention using a boolean Debounce function.
2. The conditional if-else is used after to switch between different functions. Here one()
and oneA()
are for the first tone with two different LED transitions, similarly for other functions, too. And the tones for each function is synced with LED transitions using delay()
appropriately.
tone()
uses one of the builtin timer on the Arduino and that timer operates independently of the delay()
. Or in other words, we can say that if you want to play district beats, you should check the difference between the delay time and duration of tone()
as both these functions are working parallel. Now what I did is divide the delay into smaller parts to use it with different sets of LEDs. If you want a video tutorial on this, check this out:
Let us take three()
as an example to understand it.
void three() { //This function produces the 3rd siren (AMBULANCE sound).tone(buzz,440,200);
delay(300);
for(int i=3;i<=6;i++)
digitalWrite(i,HIGH);
noTone(buzz);
tone(buzz,494,500);
delay(300);
for(int i=3;i<=6;i++)
{ digitalWrite(i,LOW);
digitalWrite(i+6,HIGH); }
noTone(buzz);
tone(buzz,523,300);
delay(200);
digitalWrite(7,HIGH);
delay(50);
digitalWrite(8,HIGH);
delay(50);
noTone(buzz);
}
After the last tone()
I divided the delay of 300ms into 200, 50 and 50 so that the LED at pin 7 and 8 have a blinking effect at the end of 523hz tone, while the tone is continuous in the background for 300ms (since there is no difference between delay and tone duration as explained above).
Comments