====== Microphone ====== //Necessary knowledge: [HW] [[en:hardware:homelab:digi]], [AVR] [[et:avr:dac]], [[et:avr:adc]] [LIB] [[en:software:homelab:library:adc]], [LIB] [[en:software:homelab:library:module:lcd_graphic]], [LIB] [[en:software:homelab:library:module:sensor]]// ===== Theory ===== [{{ :en:examples:sensor:mic.jpg?150|Microphone}}] ===== Practice ===== // Homelab microphone example // Microphone sound level graph is shown on LCD and when sound level exceeds threshold an led lights up. #include #include #include #include #include #define ADC_CHANNEL 12 // Main program int main(void) { signed short mic; BYTE n; // LCD setup function lcd_gfx_init(); // ADC muunduri seadistamine adc_init(ADC_REF_AVCC, ADC_PRESCALE_8); // Set LED pin as output pin_setup_output(led_yellow); pin_set(led_yellow); // LCD background draw FgColor = BLUE; BkColor = YELLOW; lcd_gfx_fillScreen(YELLOW); lcd_gfx_goto_char_xy(6,1); lcd_gfx_write_string("Mikrofon"); lcd_gfx_drawLine(0,40,128,40,BLUE); FgColor = RED; // Infinite loop while (1) { // Clear graph area from LCD lcd_gfx_fillRect(0, 41, 128, 119, WHITE); // Drawing the graph on LCD. Width of LCD is 128 pixels. for (n = 0; n <= 128; n++) { // Microphone ADC channel read and converting to different values range. mic = map(adc_get_value(ADC_CHANNEL), 0, 2047, 0, 120); // Draw value on graph as dot lcd_gfx_drawPixel(n, 160 - mic + 2); // Light up LED if value exceeds threshold (30) if(mic > 30) { pin_toggle(led_yellow); } } } }