/************************************************* * Public Constants *************************************************/ #define NOTE_B0 31 #define NOTE_C1 33 #define NOTE_CS1 35 #define NOTE_D1 37 #define NOTE_DS1 39 #define NOTE_E1 41 #define NOTE_F1 44 #define NOTE_FS1 46 #define NOTE_G1 49 #define NOTE_GS1 52 #define NOTE_A1 55 #define NOTE_AS1 58 #define NOTE_B1 62 #define NOTE_C2 65 #define NOTE_CS2 69 #define NOTE_D2 73 #define NOTE_DS2 78 #define NOTE_E2 82 #define NOTE_F2 87 #define NOTE_FS2 93 #define NOTE_G2 98 #define NOTE_GS2 104 #define NOTE_A2 110 #define NOTE_AS2 117 #define NOTE_B2 123 #define NOTE_C3 131 #define NOTE_CS3 139 #define NOTE_D3 147 #define NOTE_DS3 156 #define NOTE_E3 165 #define NOTE_F3 175 #define NOTE_FS3 185 #define NOTE_G3 196 #define NOTE_GS3 208 #define NOTE_A3 220 #define NOTE_AS3 233 #define NOTE_B3 247 #define NOTE_C4 262 #define NOTE_CS4 277 #define NOTE_D4 294 #define NOTE_DS4 311 #define NOTE_E4 330 #define NOTE_F4 349 #define NOTE_FS4 370 #define NOTE_G4 392 #define NOTE_GS4 415 #define NOTE_A4 440 #define NOTE_AS4 466 #define NOTE_B4 494 #define NOTE_C5 523 #define NOTE_CS5 554 #define NOTE_D5 587 #define NOTE_DS5 622 #define NOTE_E5 659 #define NOTE_F5 698 #define NOTE_FS5 740 #define NOTE_G5 784 #define NOTE_GS5 831 #define NOTE_A5 880 #define NOTE_AS5 932 #define NOTE_B5 988 #define NOTE_C6 1047 #define NOTE_CS6 1109 #define NOTE_D6 1175 #define NOTE_DS6 1245 #define NOTE_E6 1319 #define NOTE_F6 1397 #define NOTE_FS6 1480 #define NOTE_G6 1568 #define NOTE_GS6 1661 #define NOTE_A6 1760 #define NOTE_AS6 1865 #define NOTE_B6 1976 #define NOTE_C7 2093 #define NOTE_CS7 2217 #define NOTE_D7 2349 #define NOTE_DS7 2489 #define NOTE_E7 2637 #define NOTE_F7 2794 #define NOTE_FS7 2960 #define NOTE_G7 3136 #define NOTE_GS7 3322 #define NOTE_A7 3520 #define NOTE_AS7 3729 #define NOTE_B7 3951 #define NOTE_C8 4186 #define NOTE_CS8 4435 #define NOTE_D8 4699 #define NOTE_DS8 4978 int tuneType = 0; int brightness = 120; // how bright the LED is, start at half brightness int fadeAmount = 10; // how many points to fade the LED by // //unsigned long currentTime; //unsigned long loopTime; //const int encoder1A_pin = A0; // PC0 //const int encoder1B_pin = A1; // PC1 //const int encoder2A_pin = A2; // PC2 //const int encoder2B_pin = A3; // PC3 //const int encoder3A_pin = A4; // PC4 //const int encoder3B_pin = A5; // PC5 //const int encoder4A_pin = 5; // PD5 //const int encoder4B_pin = 6; // PD6 //const int encoder5A_pin = 7; // PD7 //const int encoder5B_pin = 8; // PB0 //const int encoder6A_pin = 9; // PB1 //const int encoder6B_pin = 10; // PB2 //const int speaker_pin = 3; // PD3 //const int led_pin = 13; //PB5 unsigned long currentTime; unsigned long loopTime; const int encoder1A_pin = A0; // PC0 const int encoder1B_pin = A1; // PC1 const int encoder2A_pin = A2; // PC2 const int encoder2B_pin = A3; // PC3 const int encoder3A_pin = 2; // PC4 const int encoder3B_pin = 3; // PC5 const int encoder4A_pin = 4; // PD5 const int encoder4B_pin = 5; // PD6 const int encoder5A_pin = 6; // PD7 const int encoder5B_pin = 7; // PB0 const int encoder6A_pin = 8; // PB1 const int encoder6B_pin = 9; // PB2 const int speaker_pin = 10; // PD3 const int led_pin = 13; //PB5 unsigned char encoder1A; unsigned char encoder1B; unsigned char encoder1A_prev=0; unsigned char encoder2A; unsigned char encoder2B; unsigned char encoder2A_prev=0; unsigned char encoder3A; unsigned char encoder3B; unsigned char encoder3A_prev=0; unsigned char encoder4A; unsigned char encoder4B; unsigned char encoder4A_prev=0; unsigned char encoder5A; unsigned char encoder5B; unsigned char encoder5A_prev=0; unsigned char encoder6A; unsigned char encoder6B; unsigned char encoder6A_prev=0; int length = 15; // the number of notes int index = 0; //twinkle twinkle little star //char notes[] = "ccggaag ffeeddc ggffeed ggffeed ccggaag ffeeddc "; // a space represents a rest char notes[] = "ccggaagffeeddc "; // a space represents a rest int beats[] = { 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 2, 4 }; int tempo = 300; //Mario main theme melody int mario_melody[] = { NOTE_E7, NOTE_E7, 0, NOTE_E7, 0, NOTE_C7, NOTE_E7, 0, NOTE_G7, 0, 0, 0, NOTE_G6, 0, 0, 0, NOTE_C7, 0, 0, NOTE_G6, 0, 0, NOTE_E6, 0, 0, NOTE_A6, 0, NOTE_B6, 0, NOTE_AS6, NOTE_A6, 0, NOTE_G6, NOTE_E7, NOTE_G7, NOTE_A7, 0, NOTE_F7, NOTE_G7, 0, NOTE_E7, 0, NOTE_C7, NOTE_D7, NOTE_B6, 0, 0, NOTE_C7, 0, 0, NOTE_G6, 0, 0, NOTE_E6, 0, 0, NOTE_A6, 0, NOTE_B6, 0, NOTE_AS6, NOTE_A6, 0, NOTE_G6, NOTE_E7, NOTE_G7, NOTE_A7, 0, NOTE_F7, NOTE_G7, 0, NOTE_E7, 0, NOTE_C7, NOTE_D7, NOTE_B6, 0, 0 }; //Mario main them tempo int mario_tempo[] = { 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 9, 9, 9, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 9, 9, 9, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, }; void playMario() { int size = sizeof(mario_melody) / sizeof(int); for (int thisNote = 0; thisNote < size; thisNote++) { // to calculate the note duration, take one second // divided by the note type. //e.g. quarter note = 1000 / 4, eighth note = 1000/8, etc. int noteDuration = 1000 / mario_tempo[thisNote]; buzz(speaker_pin, mario_melody[thisNote], noteDuration); // to distinguish the notes, set a minimum time between them. // the note's duration + 30% seems to work well: int pauseBetweenNotes = noteDuration * 1.30; delay(pauseBetweenNotes); // stop the tone playing: buzz(speaker_pin, 0, noteDuration); } } void buzz(int targetPin, long frequency, long length) { digitalWrite(13, HIGH); long delayValue = 1000000 / frequency / 2; // calculate the delay value between transitions //// 1 second's worth of microseconds, divided by the frequency, then split in half since //// there are two phases to each cycle long numCycles = frequency * length / 1000; // calculate the number of cycles for proper timing //// multiply frequency, which is really cycles per second, by the number of seconds to //// get the total number of cycles to produce for (long i = 0; i < numCycles; i++) { // for the calculated length of time... digitalWrite(targetPin, HIGH); // write the buzzer pin high to push out the diaphram delayMicroseconds(delayValue); // wait for the calculated delay value digitalWrite(targetPin, LOW); // write the buzzer pin low to pull back the diaphram delayMicroseconds(delayValue); // wait again or the calculated delay value } digitalWrite(13, LOW); } void setup() { // declare pin 9 to be an output: // pinMode(9, OUTPUT); pinMode(encoder1A_pin, INPUT); pinMode(encoder1B_pin, INPUT); pinMode(encoder2A_pin, INPUT); pinMode(encoder2B_pin, INPUT); pinMode(encoder3A_pin, INPUT); pinMode(encoder3B_pin, INPUT); pinMode(encoder4A_pin, INPUT); pinMode(encoder4B_pin, INPUT); pinMode(encoder5A_pin, INPUT); pinMode(encoder5B_pin, INPUT); pinMode(encoder6A_pin, INPUT); pinMode(encoder6B_pin, INPUT); pinMode(led_pin, OUTPUT); pinMode(speaker_pin, OUTPUT); //pull up resistors digitalWrite(encoder1A_pin, HIGH); digitalWrite(encoder1B_pin, HIGH); digitalWrite(encoder2A_pin, HIGH); digitalWrite(encoder2B_pin, HIGH); digitalWrite(encoder3A_pin, HIGH); digitalWrite(encoder3B_pin, HIGH); digitalWrite(encoder4A_pin, HIGH); digitalWrite(encoder4B_pin, HIGH); digitalWrite(encoder5A_pin, HIGH); digitalWrite(encoder5B_pin, HIGH); digitalWrite(encoder6A_pin, HIGH); digitalWrite(encoder6B_pin, HIGH); currentTime = millis(); loopTime = currentTime; // chirp(); playMario(); Serial.begin (19200); Serial.println("Hello!"); } void playTwinkle() { // get the current elapsed time playNoteMusic(notes[index], beats[index] * tempo); // pause between notes delay(tempo / 2); index = (index + 1) % (length - 1); } int animalIndex =0; int animalLength = 5; void playAnimal() { if (animalIndex == 0) { chirp(); chirp(); } else if (animalIndex == 1){ meow(); meow(); } else if (animalIndex == 2){ meow2(); meow2(); } else if (animalIndex == 3){ ruff(); ruff(); } else { arf(); arf(); } animalIndex = (animalIndex + 1) % (animalLength); } void loop() { currentTime = millis(); if(currentTime >= (loopTime + 50)){ // 5ms since last check of encoder = 200Hz // 1ms since last check of encoder = 1000Hz encoder1A = digitalRead(encoder1A_pin); // Read encoder pins encoder1B = digitalRead(encoder1B_pin); encoder2A = digitalRead(encoder2A_pin); // Read encoder pins encoder2B = digitalRead(encoder2B_pin); encoder3A = digitalRead(encoder3A_pin); // Read encoder pins encoder3B = digitalRead(encoder3B_pin); encoder4A = digitalRead(encoder4A_pin); // Read encoder pins encoder4B = digitalRead(encoder4B_pin); encoder5A = digitalRead(encoder5A_pin); // Read encoder pins encoder5B = digitalRead(encoder5B_pin); encoder6A = digitalRead(encoder6A_pin); // Read encoder pins encoder6B = digitalRead(encoder6B_pin); isTurned(encoder1A_prev, encoder1A, encoder1B, 0, 1); isTurned(encoder2A_prev, encoder2A, encoder2B, 2, 3); isTurned(encoder3A_prev, encoder3A, encoder3B, 4, 5); isTurned(encoder4A_prev, encoder4A, encoder4B, 6, 7); isTurned(encoder5A_prev, encoder5A, encoder5B, 8, 9); isTurned(encoder6A_prev, encoder6A, encoder6B, 10, 11); encoder1A_prev = encoder1A; // Store value of A for next time encoder2A_prev = encoder1A; // Store value of A for next time encoder3A_prev = encoder1A; // Store value of A for next time encoder4A_prev = encoder1A; // Store value of A for next time encoder5A_prev = encoder1A; // Store value of A for next time encoder6A_prev = encoder1A; // Store value of A for next time // set the brightness of pin 9: // analogWrite(9, brightness); loopTime = currentTime; // Updates loopTime } // Other processing can be done here } void isTurned(uint8_t encoder_A_prev, uint8_t encoder_A, uint8_t encoder_B, uint8_t noteIndex1, uint8_t noteIndex2) { if((!encoder_A) && (encoder_A_prev)){ // A has gone from high to low // chirp(); if (tuneType == 0) { playTwinkle(); playTwinkle(); } else { playAnimal(); } Serial.println ("1"); if(encoder_B) { // B is high so clockwise // increase the brightness, dont go over 255 Serial.println ("CW"); digitalWrite(led_pin, !digitalRead(led_pin)); } else { // B is low so counter-clockwise // decrease the brightness, dont go below 0 digitalWrite(led_pin, !digitalRead(led_pin)); Serial.println ("CCW"); } } } void playNote(uint16_t period, uint16_t duration) { uint16_t elapsed; uint16_t i; for (elapsed = 0; elapsed < duration; elapsed += period) { /* For loop with variable delay selects the pitch */ for (i = 0; i < period; i++) { _delay_us(1); } digitalWrite(speaker_pin, HIGH); } } void rest(uint16_t duration) { do { _delay_us(1); } while (--duration); } // Generate the Bird Chirp sound void chirp() { for(uint8_t i=200; i>180; i--) playTone(i,9); } // Play a tone for a specific duration. value is not frequency to save some // cpu cycles in avoiding a divide. void playTone(int16_t tonevalue, int duration) { for (long i = 0; i < duration * 1000L; i += tonevalue * 2) { digitalWrite(speaker_pin, HIGH); delayMicroseconds(tonevalue); digitalWrite(speaker_pin, LOW); delayMicroseconds(tonevalue); } } void playNoteMusic(char note, int duration) { char names[] = { 'c', 'd', 'e', 'f', 'g', 'a', 'b', 'C' }; int tones[] = { 1915, 1700, 1519, 1432, 1275, 1136, 1014, 956 }; // play the tone corresponding to the note name for (int i = 0; i < 8; i++) { if (names[i] == note) { playTone(tones[i], duration); } } } void meow() { // cat meow (emphasis ow "me") uint16_t i; playTone(5100,50); // "m" (short) playTone(394,180); // "eee" (long) for(i=990; i<1022; i+=2) // vary "ooo" down playTone(i,8); playTone(5100,40); // "w" (short) } void meow2() { // cat meow (emphasis on "ow") uint16_t i; playTone(5100,55); // "m" (short) playTone(394,170); // "eee" (long) delay(30); // wait a tiny bit for(i=330; i<360; i+=2) // vary "ooo" down playTone(i,10); playTone(5100,40); // "w" (short) } void mew() { // cat mew uint16_t i; playTone(5100,55); // "m" (short) playTone(394,130); // "eee" (long) playTone(384,35); // "eee" (up a tiny bit on end) playTone(5100,40); // "w" (short) } void ruff() { // dog ruff uint16_t i; for(i=890; i<910; i+=2) // "rrr" (vary down) playTone(i,3); playTone(1664,150); // "uuu" (hard to do) playTone(12200,70); // "ff" (long, hard to do) } void arf() { // dog arf uint16_t i; playTone(890,25); // "a" (short) for(i=890; i<910; i+=2) // "rrr" (vary down) playTone(i,5); playTone(4545,80); // intermediate playTone(12200,70); // "ff" (shorter, hard to do) }