boolean lastbutton=LOW;
boolean currentbutton=LOW;
int input=0;
int buzz=13; // Buzzer Pin
int j=3;
int k=12;
unsigned long lastToneTime = 0;
unsigned long lastLedTime = 0;
unsigned long lastDebounceTime = 0; // Waktu terakhir tombol berubah
unsigned long debounceDelay = 50; // Waktu debounce (dalam milidetik)
bool lastButtonState = LOW; // Status tombol terakhir
bool buttonState = LOW;
void setup() {
Serial.begin(115200); // Any baud rate should work
Serial.println("Hello Arduino\n");
for(int i=3;i<=12;i++)
pinMode(i,OUTPUT);
pinMode(2,INPUT);
}
int input_terakhir=0;
void loop() {
for(int i=3;i<=12;i++)
digitalWrite(i,LOW);
// millis
bool reading = digitalRead(2);
// Jika status tombol berubah (karena noise atau penekanan tombol)
if (reading != lastButtonState) {
// Reset waktu debounce
lastDebounceTime = millis();
}
// Jika waktu debounce telah berlalu
if ((millis() - lastDebounceTime) > debounceDelay) {
// Jika status tombol telah berubah
if (reading != buttonState) {
buttonState = reading;
// Hanya jika tombol ditekan (bukan dilepas)
if (buttonState == HIGH) {
input++;
Serial.println("Tombol ditekan!");
}
}
}
// Simpan status tombol terakhir
lastButtonState = reading;
//.milis
if(input!= input_terakhir){
Serial.print("input: ");
Serial.println(input);
input_terakhir=input;
}
lastbutton=currentbutton;
settone(input);
}
void settone(int input)
{
if(input==1)
one();
else if(input==2)
oneA();
else if(input==3)
two();
else if(input==4)
twoA();
/*
else if(input==5)
three();
else if(input==6)
threeA();
else if(input==7)
four();
*/
}
void one() {
static unsigned long lastToneTime = 0;
static int hz = 440;
static bool whoopUp = true;
if (millis() - lastToneTime >= 5) {
lastToneTime = millis();
if (whoopUp) {
tone(buzz, hz, 50);
hz += 25;
if (hz >= 1000) {
whoopUp = false;
}
} else { //whoopDown
tone(buzz, hz, 50);
hz -= 25;
if (hz <= 440) {
whoopUp = true;
}
}
for (int i = 3; i <= 7; i++) {
digitalWrite(i, whoopUp ? HIGH : LOW);
digitalWrite(i + 5, whoopUp ? LOW : HIGH);
}
}
}
void oneA() {
static unsigned long lastToneTime = 0;
static bool whoopUp = true;
static int hz = 440;
unsigned long currentMillis = millis();
// Whoop up
if (whoopUp && currentMillis - lastToneTime >= 5) {
lastToneTime = currentMillis;
tone(buzz, hz, 50);
hz += 25;
if (hz >= 1000) {
whoopUp = false;
}
}
// Whoop down
if (!whoopUp && currentMillis - lastToneTime >= 5) {
lastToneTime = currentMillis;
tone(buzz, hz, 50);
hz -= 25;
if (hz <= 440) {
whoopUp = true;
}
}
// LED transition
if (currentMillis - lastLedTime >= 50) {
lastLedTime = currentMillis;
if (j >= 3) {
digitalWrite(j, HIGH);
j = j + 1;
digitalWrite(k, HIGH);
k = k - 1;
if (j == 8) j = 3;
if (k == 7) k = 12;
}
}
}
// void two() { //This function produces the 2nd siren sound with progressive led transition.
// // Whoop up
// for(int hz = 440; hz < 1000; hz+=25){
// tone(buzz, hz, 50);
// delay(5);
// }
// loopF(3,12,20);
// loopR(12,3,20);
// // Whoop down
// for(int hz = 1000; hz > 440; hz-=25){
// tone(buzz, hz, 50);
// delay(5);
// }
// }
void two() {
static unsigned long lastToneTime = 0;
static bool whoopUp = true;
static int hz = 440;
unsigned long currentMillis = millis();
// Whoop up
if (whoopUp && currentMillis - lastToneTime >= 5) {
lastToneTime = currentMillis;
tone(buzz, hz, 50);
hz += 25;
if (hz >= 1000) {
whoopUp = false;
}
}
// Whoop down
if (!whoopUp && currentMillis - lastToneTime >= 5) {
lastToneTime = currentMillis;
tone(buzz, hz, 50);
hz -= 25;
if (hz <= 440) {
whoopUp = true;
}
}
}
void twoA() { //This function produces differnt transition on 2nd siren.
// Whoop up
for(int hz = 440; hz < 1000; hz+=25){
tone(buzz, hz, 50);
delay(5);
}
loopF(3,k,20);
loopR(12,j,20);
k--;
if(k==3)
k=12;
j++;
if(j==12)
j=3;
// Whoop down
for(int hz = 1000; hz > 440; hz-=25){
tone(buzz, hz, 50);
delay(5);
}
}
// void three() { //This function produces the 3rd siren(AMBULANCE) sound with led transition.
// 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);
// }
// void threeA() { //This function produces differnt transition on 3rd siren.
// tone(buzz,440,200);
// delay(100);
// loopF(5,10,20);
// loopR(10,5,20);
// noTone(buzz);
// for(int i=3;i<=4;i++){
// digitalWrite(i,HIGH);
// digitalWrite(i+8,HIGH);}
// tone(buzz,494,500);
// delay(300);
// noTone(buzz);
// for(int i=3;i<=4;i++){
// digitalWrite(i,LOW);
// digitalWrite(i+8,LOW);
// }
// tone(buzz,523,300);
// delay(300);
// noTone(buzz);
// }
// void four() { //This function produces the 4th siren(POLICE) sound with led transition.
// for(int i=3;i<=11;i+=2)
// digitalWrite(i,HIGH);
// for(int hz = 440; hz < 1000; hz++){
// tone(buzz, hz, 50);
// delay(5);
// }
// for(int i=3;i<=11;i+=2)
// digitalWrite(i,LOW);
// for(int i=4;i<=12;i+=2)
// digitalWrite(i,HIGH);
// for(int hz = 1000; hz > 440; hz--){
// tone(buzz, hz, 50);
// delay(5);
// }
// }
// SOME EXTRA FUNCTIONS OTHER THAN THE SIREN TONES
void low(){ //Used by loopF and loopR
for(int i=3;i<=12;i++)
digitalWrite(i,LOW);
}
// bool forward = true;
// void loopF(int spin, int epin, int dela) {
// unsigned long currentMillis = millis();
// if (currentMillis - lastLedTime >= dela) {
// lastLedTime = currentMillis;
// low();
// // Nyalakan LED saat ini
// digitalWrite(spin, HIGH);
// // Update pin LED berikutnya
// if (forward) {
// spin++;
// if (spin > epin) {
// spin = spin;
// forward = false;
// }
// } else {
// spin--;
// if (spin < spin) {
// spin = epin;
// forward = true;
// }
// }
// }
// }
// loopR(12,3,20);
void loopR(int epin,int spin,int dela){ //loopR can blink the led in reverse/backward direction so epin must be lower than spin.
for(int i=epin; i >=spin; i--){
digitalWrite(i,HIGH);
delay(dela);
low();
if(spin==epin){
spin=3;
epin=12;
}
}
}
void loopF(int spin,int epin,int dela){ //loopF can blink the led in forward direction so spin must be lower than epin.
for(int i=spin;i<=epin;i++){
digitalWrite(i,HIGH);
delay(dela);
low();
if(spin==epin){
spin=3;
epin=12;}
}
}