Sirine Polisi dengan Arduino

Membuat sirine yang mirip suara sirine polisi dengan arduino

Bahan :

  1. Arduino Board
  2. Speaker Aktif (bisa juga pake buzzer)

Sketch :


#include <TimerOne.h>


boolean tungguSampai(unsigned long *counter, unsigned long curtime, int interval){
    if((curtime - *counter) >  interval){
        *counter = curtime;
        return true;
        
    } 
    return false;
  
  }
unsigned long timecounter = 0;
int mynote = 500;
boolean up = false;
boolean on = false;

int pinalarm = 9;


void alarmPolisi(){
    if(tungguSampai(&timecounter,micros(), mynote / 2)){
      if(on){
            digitalWrite(pinalarm,HIGH);
            on = false;
      } else {
           digitalWrite(pinalarm,LOW);
            on = true;
      } 
      
    }
}

void setup() {
  pinMode(pinalarm,OUTPUT);
  Timer1.initialize(50);
  Timer1.attachInterrupt(alarmPolisi);
}

void loop() {
  if(up)  mynote += 100;
        else  mynote -= 100;

      if(mynote > 7000) up = false;
      if(mynote < 500) up = true;

   delay(10);
}

Video