Table of Contents

Microphone

Necessary knowledge: [HW] User Interface Module, [AVR] Digitaal-analoogmuundur, Analoog-digitaalmuundur [LIB] Analog to Digital Converter, [LIB] Graphic LCD, [LIB] Sensors

Theory

Invalid Link
Microphone

Practice

// Homelab microphone example
// Microphone sound level graph is shown on LCD and when sound level exceeds threshold an led lights up.
#include <stdio.h>
#include <homelab/adc.h>
#include <homelab/delay.h>
#include <homelab/module/sensors.h>
#include <homelab/module/lcd_gfx.h>
 
#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);
			} 
		}		
 
	}
}