-
-
Save JeffersGlass/859ca5e330a0097f2e67b0ae1d2585ba to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "pitches.h" | |
#define EN 100 //length of an eighth-note in MS | |
#define GAP 5 //How long a small-gap to leave between identical notes | |
#define SILENCE 30000 //frequency of 'silence' (above hearing range) | |
//C Major Scale | |
//const int toneList[] = {262, EN, 294, EN, 330, EN, 349, EN, 392, EN, 440, EN, 494, EN, 523, EN}; | |
//Tetris | |
const int toneList[] = { | |
NOTE_E4, EN*2, NOTE_B3, EN, NOTE_C4, EN, NOTE_D4, EN*2, NOTE_C4, EN, NOTE_B3, EN, | |
NOTE_A3, EN*2-GAP, SILENCE, GAP, NOTE_A3, EN, NOTE_C4, EN, NOTE_E4, EN*2, NOTE_D4, EN, NOTE_C4, EN, | |
NOTE_B3, EN*3, NOTE_C4, EN, NOTE_D4, EN*2, NOTE_E4, EN*2, | |
NOTE_C4, EN*2, NOTE_A3, EN*2-GAP, SILENCE, GAP, NOTE_A3, EN*2, SILENCE, EN*2, | |
SILENCE, EN, NOTE_D4, EN*2, NOTE_F4, EN, NOTE_A4, EN*2, NOTE_G4, EN, NOTE_F4, EN, | |
NOTE_E4, EN*3, NOTE_C4, EN, NOTE_E4, EN*2, NOTE_D4, EN, NOTE_C4, EN, | |
NOTE_B3, EN*3, NOTE_C4, EN, NOTE_D4, EN*2, NOTE_E4, EN*2, | |
NOTE_C4, EN*2, NOTE_A3, EN*2-GAP, SILENCE, GAP, NOTE_A3, EN*2, SILENCE, EN*2, | |
NOTE_E4, EN*4, NOTE_C4, EN*4, NOTE_D4, EN*4, NOTE_B3, EN*4, | |
NOTE_C4, EN*4, NOTE_A3, EN*4, NOTE_GS3, EN*4, NOTE_B3, EN*3, SILENCE, EN, | |
NOTE_E4, EN*4, NOTE_C4, EN*4, NOTE_D4, EN*4, NOTE_B3, EN*4, | |
NOTE_C4, EN*2, NOTE_E4, EN*2, NOTE_A4, EN*4, NOTE_GS4, EN*4, SILENCE, EN*4 | |
}; | |
const int tonepin = PORTB1; | |
volatile int toneIndex = 0; | |
volatile long timePeriods = 0; | |
int maxTone; | |
void setup() { | |
maxTone = sizeof(toneList)/sizeof(toneList[1])-1; | |
DDRB = _BV(tonepin); //Set up tone pin as output | |
GTCCR |= _BV(PSRASY); | |
TCCR2A = 0; //_BV(COM2A0); //Mode will be normal, WGM12 is in B register | |
TCCR2B = _BV(CS22); //Set up /64 prescaler | |
TIMSK2 = _BV(TOIE2); //Enable overflow interrupt | |
TCCR1A = _BV(COM1A0); //Mode will be CTC; WGM12 is in B register | |
TCCR1B = _BV(WGM12) | _BV(CS11); //Set up x8 prescaler | |
OCR1A = int(1000000/ (long) toneList[0]); | |
interrupts(); //Enable interrupts globally | |
} | |
void loop() { | |
//timer 2 overflows every 4 microseconds; one millisecond is therefore 250 overflows | |
if (timePeriods > (toneList[toneIndex+1] * long(2))){ | |
timePeriods = 0; | |
toneIndex += 2; | |
if (toneIndex > maxTone) toneIndex = 0; | |
OCR1A = int(1000000/ (long) toneList[toneIndex]); //16 MHz / (8x prescaler * 2) | |
} | |
} | |
ISR(TIMER2_OVF_vect){ | |
timePeriods++; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment