char *note; float freq = 0.0f; int count_freq; void MICROPHONE_init(void) { /* start audio interrupts */ MICROPHONE_DDR &= ~(1 << MICROPHONE_P); PCICR |= (1 << MICROPHONE_PCIE); MICROPHONE_PCMSK |= (1 << MICROPHONE_PCINT); } void TIMER1_init(void) { /* set timer to one second, or one Hertz */ TCCR1B |= (1 << CS12) | (1 << WGM12); TIMSK1 |= (1 << OCIE1A); OCR1A = 62500 - 1; } void TIMER1_free(void) { TCCR1B = 0; TIMSK1 = 0; OCR1A = 0; } ISR(TIMER1_COMPA_vect) { TIMER1_free(); char buff[BUFF_SIZE]; float f = count_freq; float df = freq - f; LCD_clear(); LCD_printAt(LCD_FIRST_LINE_BEGIN, note); sprintf(buff, "%d", count_freq); LCD_printAt(LCD_SECOND_LINE_BEGIN, buff); if (fabsf(df) < FREQ_TOLERANCE) { /* note frequency is in tolerance interval */ note = NULL; freq = 0.0f; _delay_ms(1000); LCD_clear(); LCD_printAt(LCD_FIRST_LINE_BEGIN, note); LCD_printAt(LCD_SECOND_LINE_BEGIN, "OK"); count_freq = 0; } else { /* tune note */ if (f > FREQ_TOLERANCE) STEPPER_rotation(df); count_freq = 0; TIMER1_init(); } } ISR(PCINT0_vect) { /* count changes of sign in oscillation */ if ((MICROPHONE_PIN & (1 << MICROPHONE_P)) == 0) count_freq++; }