My Daily Gist | Ferdinand Silva


Ang Bayan Kong Sinilangan Tune With Arduino In Pure C (Passive Buzzer)

 
/*
Thanks to this post: https://balau82.wordpress.com/2014/10/15/using-a-buzzer-with-arduino-in-pure-c/
*/
#ifndef F_CPU
#define F_CPU 16000000UL //set frequency
#endif
#define __DELAY_BACKWARD_COMPATIBLE__
#include <avr/io.h>
#include <util/delay.h>
int tones[17] = {
247,
330,
330,
370,
330,
370,
392,
392,
392,
440,
494,
440,
440,
370,
392,
370,
330
};
int toneDelays[17] = {
500,
500,
500,
200,
250,
450,
500,
500,
200,
250,
450,
500,
500,
200,
250,
450,
1100
};
void tone(unsigned long frequency) {
unsigned long timer_frequency = (F_CPU + (256/2)) / 256;
OCR0A = (timer_frequency + ((frequency*2)/2)) / (frequency*2);
TCCR0A = _BV(COM0A0) | _BV(WGM01);
}
int main() {
DDRD |= _BV(DDD6); //digital pin 6
TCCR0B = _BV(CS02);
for(;;) {
for(int x=0; x < 17; x++) {
tone(tones[x]);
_delay_ms(toneDelays[x]);
}
_delay_ms(400);
}
return 0;
}
view raw test.c hosted with ❤ by GitHub