Skip to content

Instantly share code, notes, and snippets.

@akoskovacs
Last active August 29, 2015 14:05
Show Gist options
  • Select an option

  • Save akoskovacs/81df05baa48af7100f39 to your computer and use it in GitHub Desktop.

Select an option

Save akoskovacs/81df05baa48af7100f39 to your computer and use it in GitHub Desktop.
Tetris theme for Arduino with the 16bit timer
#include <avr/io.h>
void changeFrequency(unsigned int freq)
{
int ocr = F_CPU/freq-1;
cli();
TCCR1A = _BV(COM1A0);
OCR1A = ocr;
sei();
}
void setup()
{
// OC0A - Pin 9 as outout
DDRB = _BV(PORTB1);
TCCR1A = _BV(COM1A0);
TCCR1B = _BV(WGM12)|_BV(CS10); // CTC, div 1
}
void disableTimer()
{
TCCR1A &= (uint8_t)~_BV(COM1A0);
}
const int notes[] = {
1320, 990, 1056, 1188, 1320, 1188, 1056, 990, 880, 880, 1056, 1320, 1188, 1056, 990, 1056, 1188,
1320, 1056, 880, 880, 1188, 1408, 1760, 1584, 1408, 1320, 1056, 1320, 1188, 1056, 990, 990, 1056,
1188, 1320, 1056, 880, 880, 1320, 990, 1056, 1188, 1320, 1188, 1056, 990, 880, 880, 1056, 1320,
1188, 1056, 990, 1056, 1188, 1320, 1056, 880, 880, 1188, 1408, 1760, 1584, 1408, 1320, 1056,
1320, 1188, 1056, 990, 990, 1056, 1188, 1320, 1056, 880, 880, 660, 528, 594, 495, 528, 440, 419,
495, 660, 528, 594, 495, 528, 660, 880, 838, 660, 528, 594, 495, 528, 440, 419, 495, 660, 528, 594,
495, 528, 660, 880, 838
};
const int note_len[] = {
500, 250, 250, 250, 125, 125, 250, 250, 500, 250, 250, 500, 250, 250, 750, 250, 500, 500, 500,
500, 500, 500, 250, 500, 250, 250, 750, 250, 500, 250, 250, 500, 250, 250, 500, 500, 500, 500,
500, 500, 250, 250, 250, 125, 125, 250, 250, 500, 250, 250, 500, 250, 250, 750, 250, 500, 500,
500, 500, 500, 500, 250, 500, 250, 250, 750, 250, 500, 250, 250, 500, 250, 250, 500, 500, 500,
500, 500, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 500, 500, 1000,
2000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 500, 500, 1000, 2000
};
#define NOTE_CNT sizeof(notes)/sizeof(int)
void loop()
{
int i;
delay(500);
for (i = 0; i < NOTE_CNT; i++) {
changeFrequency(notes[i]);
delay(note_len[i]/1.5);
// disableTimer();
// delay(delay_len[i]/1.5);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment