int main(void) { bit_set(DDRB, BIT(3)); bit_set(DDRB, BIT(2)); bit_clear(PORTB, BIT(3)); bit_set(PORTB, BIT(2)); pwm_init(); adc_init(); int recording = 0; int saved; int playing = 0; int reading; int noteSlot = 0; while(1) { if(playing) { // GET NOTE reading = eeprom_read_byte ((uint8_t*)noteSlot); reading = reading * 4; //reading = 900; } else { // READ ADC reading = adc_read(); } if (reading < 128) { bit_clear(PORTB, BIT(3)); //bit_clear(PORTB, BIT(2)); dontplay(); if(recording){ bit_clear(PORTB, BIT(2)); _delay_ms(10); if(adc_read()==reading) { saved = 0; } } } else { if(recording && !saved) { // RECORD NOTE int readingLess = reading / 4; eeprom_write_byte ((uint8_t *)noteSlot, (uint8_t)readingLess); noteSlot++; if(noteSlot>60) { recording = 0; noteSlot = 0; bit_flip(PORTB, BIT(2)); } else { saved = 1; } } bit_set(PORTB, BIT(3)); if(recording) bit_set(PORTB, BIT(2)); if ((reading <= 310)) // RECORD { // DEBOUNCE _delay_ms(50); if(adc_read()==reading) { // Debounce ok, toggle recording mode recording = !recording; if(!recording) { noteSlot++; eeprom_write_byte ((uint8_t *)noteSlot, (uint8_t)0); } noteSlot = 0; //bit_flip(PORTB, BIT(2)); while(adc_read()>128){} } } else if ((reading <= 340)) // PLAY { if(!recording){ _delay_ms(2); int secondRead = adc_read(); if(secondRead==reading || secondRead==reading+1 || secondRead==reading-1 || secondRead==reading+2 || secondRead==reading-2) { // Debounce ok, start playing playing = 1; noteSlot = 0; bit_clear(PORTB, BIT(2)); } while(adc_read()>128){} } } else if ((reading <= 359)) { playnote(A1); } else if (reading <= 380) { playnote(AS1); } else if (reading <= 387) { playnote(B1); } else if (reading <= 402) { playnote(C1); } else if (reading <= 418) { playnote(CS1); } else if (reading <= 436) { playnote(D1); } else if (reading <= 455) { playnote(DS1); } else if (reading <= 477) { playnote(E1); } else if (reading <= 500) { playnote(F1); } else if (reading <= 525) { playnote(FS1); } else if (reading <= 554) { playnote(G1); } else if (reading <= 586) { playnote(GS1); } else if (reading <= 621) { playnote(A2); } else if (reading <= 661) { playnote(AS2); } else if (reading <= 707) { playnote(B2); } else if (reading <= 760) { playnote(C2); } else if (reading <= 821) { playnote(CS2); } else if (reading <= 892) { playnote(D2); } else if (reading <= 977) { playnote(DS2); } else if (reading > 977) { playnote(E2); } } if(playing) { // wait for time _delay_ms(200); dontplay(); _delay_ms(200); noteSlot++; if(noteSlot>60 || reading==0) { noteSlot = 0; playing = 0; bit_set(PORTB, BIT(2)); } } else { // loop quickly _delay_us(255); } } }
void loop(void) { //if we are in playback mode if (playing) { // fetch the next note from the eeprom reading = eeprom_read_byte((uint8_t*) noteSlot); reading = reading * 4; } else { // fetch the next note from the probe reading = adc_read(); } // if nothing is being pressed if (reading < 128) { // turn the touch LED On bit_clear(PORTB, LED_TOUCH); // Stop playing any notes we might be playing dontplay(); // if we are recording if (recording) { // turn the REC LED on bit_clear(PORTB, LED_REC); _delay_ms(10); if (adc_read() == reading) { saved = 0; } } } else { // if we are recording and the note hasn't changed if (recording && !saved) { // crop off the top two bits to make the note fit in an 8 bit memory address int readingLess = reading / 4; // store the note in the eeprom eeprom_write_byte((uint8_t *) noteSlot, (uint8_t) readingLess); // move to the next eeprom location noteSlot++; // if the eeprom is full if (noteSlot > 60) { // stop recording & reset to the beginning of the recording recording = FALSE; noteSlot = 0; // turn the recording LED Off bit_set(PORTB, LED_REC); } else { // mark the current note as saved saved = 1; } } // turn the TOUCH LED off bit_set(PORTB, LED_TOUCH); // if we are recording, turn the RED LED off (why here?) if (recording) bit_set(PORTB, LED_REC); // average out the reading over a few samples to make sure it is correct _delay_ms(50); reading = reading + adc_read() + adc_read() + adc_read() / 4; if ((reading <= 310)) // Record button was pressed { // Toggle Recording Mode recording = !recording; // If we are finished recording if (!recording) { // add a '0' to the end of the recording, to make sure the playback is turned off noteSlot++; eeprom_write_byte((uint8_t *) noteSlot, (uint8_t) 0); } // reset our 'pointer' to the beginning of the eeprom noteSlot = 0; // wait for the button to be released while (adc_read() > 128); } else if ((reading <= 340)) // Play button was pressed { // we cant play while recording if (!recording) { // start playing back from the beginning of the eeprom playing = TRUE; noteSlot = 0; // turn on the REC LED bit_clear(PORTB, LED_REC); // wait for the button to be released while (adc_read() > 128); } } else if ((reading <= 359)) { playnote(A1); } else if (reading <= 380) { playnote(AS1); } else if (reading <= 387) { playnote(B1); } else if (reading <= 402) { playnote(C1); } else if (reading <= 418) { playnote(CS1); } else if (reading <= 436) { playnote(D1); } else if (reading <= 455) { playnote(DS1); } else if (reading <= 477) { playnote(E1); } else if (reading <= 500) { playnote(F1); } else if (reading <= 525) { playnote(FS1); } else if (reading <= 554) { playnote(G1); } else if (reading <= 586) { playnote(GS1); } else if (reading <= 621) { playnote(A2); } else if (reading <= 661) { playnote(AS2); } else if (reading <= 707) { playnote(B2); } else if (reading <= 760) { playnote(C2); } else if (reading <= 821) { playnote(CS2); } else if (reading <= 892) { playnote(D2); } else if (reading <= 977) { playnote(DS2); } else if (reading > 977) { playnote(E2); } } if (playing) { // wait for time _delay_ms(200); dontplay(); _delay_ms(200); noteSlot++; if (noteSlot > 60 || reading == 0) { noteSlot = 0; playing = FALSE; bit_set(PORTB, LED_REC); } } else { // loop quickly _delay_us(255); } }