Advertisement
tartakynov

attiny13 imperial march

Jul 19th, 2012
2,397
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.74 KB | None | 0 0
  1. #include <avr/pgmspace.h>
  2.  
  3. // by @tartakynov
  4. // programmed for ATtiny13 in Arduino IDE using core13 http://sourceforge.net/projects/ard-core13/
  5.  
  6. #define PIN_LED 1
  7. #define PIN_BUZZER 0
  8. #define COUNT_NOTES 39
  9.  
  10. word frequences[COUNT_NOTES] PROGMEM = {
  11.                                 392, 392, 392, 311, 466, 392, 311, 466, 392,
  12.                                 587, 587, 587, 622, 466, 369, 311, 466, 392,
  13.                                 784, 392, 392, 784, 739, 698, 659, 622, 659,
  14.                                 415, 554, 523, 493, 466, 440, 466,
  15.                                 311, 369, 311, 466, 392 };
  16. word durations[COUNT_NOTES] PROGMEM = {  
  17.                                 350, 350, 350, 250, 100, 350, 250, 100, 700,
  18.                                 350, 350, 350, 250, 100, 350, 250, 100, 700,
  19.                                 350, 250, 100, 350, 250, 100, 100, 100, 200,
  20.                                 100, 350, 250, 100, 100, 100, 200,
  21.                                 100, 350, 250, 100, 750 };
  22.  
  23. void setup()
  24. {
  25.     pinMode(PIN_LED, OUTPUT);
  26.     pinMode(PIN_BUZZER, OUTPUT);
  27. }
  28.  
  29. void loop()
  30. {
  31.     for (byte i = 0; i < COUNT_NOTES; i++)
  32.     {
  33.         buzz(PIN_BUZZER, pgm_read_word(&(frequences[i])), 2 * pgm_read_word(&(durations[i])));
  34.         delay(100);
  35.     }
  36.     delay(3000);
  37. }
  38.  
  39. void buzz(unsigned char pin, word frequencyInHertz, word timeInMilliseconds)
  40. {
  41.     long delayAmount = (long)(long(1000000) / (long)frequencyInHertz);
  42.     long loopTime = (long)(((long)timeInMilliseconds * 500) / delayAmount);
  43.     for (long i = 0; i < loopTime; i++)  
  44.     {  
  45.         digitalWrite(pin, HIGH);
  46.         delayMicroseconds(delayAmount);
  47.         digitalWrite(pin, LOW);
  48.         delayMicroseconds(delayAmount);
  49.     }
  50. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement