This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revision | |||
| en:examples:sensor:mic [2015/12/28 15:50] – kaupo.raid | en:examples:sensor:mic [2020/07/20 12:00] (current) – external edit 127.0.0.1 | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| + | ====== Microphone ====== | ||
| + | //Necessary knowledge: | ||
| + | [HW] [[en: | ||
| + | [AVR] [[et: | ||
| + | [LIB] [[en: | ||
| + | [LIB] [[en: | ||
| + | |||
| + | ===== Theory ===== | ||
| + | [{{ : | ||
| + | |||
| + | ===== Practice ===== | ||
| + | |||
| + | |||
| + | <code c> | ||
| + | // 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, | ||
| + | |||
| + | // 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, | ||
| + | lcd_gfx_write_string(" | ||
| + | lcd_gfx_drawLine(0, | ||
| + | FgColor = RED; | ||
| + | |||
| + | // Infinite loop | ||
| + | while (1) | ||
| + | { | ||
| + | // Clear graph area from LCD | ||
| + | lcd_gfx_fillRect(0, | ||
| + | |||
| + | // 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), | ||
| + | |||
| + | // Draw value on graph as dot | ||
| + | lcd_gfx_drawPixel(n, | ||
| + | |||
| + | // Light up LED if value exceeds threshold (30) | ||
| + | if(mic > 30) | ||
| + | { | ||
| + | pin_toggle(led_yellow); | ||
| + | } | ||
| + | } | ||
| + | |||
| + | } | ||
| + | } | ||
| + | |||
| + | </ | ||