Skip to content

Instantly share code, notes, and snippets.

@six519
Created April 27, 2018 00:53
Show Gist options
  • Save six519/0eaac04c7d52406ce6e271a55ed73902 to your computer and use it in GitHub Desktop.
Save six519/0eaac04c7d52406ce6e271a55ed73902 to your computer and use it in GitHub Desktop.
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;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment