#include #include #include #include #include "serial-neil.h" #define F_CPU 8000000UL // internal clock set to 8 MHz #define LED1 PA7 // define the pins for the LEDs #define LED2 PB2 #define LED3 PB1 #define LED4 PB0 #define LIGHTSENSOR PA2 // pin for the phototransistor #define PULSE PA4 // pin sending the pulse for the capacitive sensing #define SENSOR PA0 // pin doing the capacitive sensing #define CALIBRATION 600 // maximum value expected from the sensor void bouncy(void) // function making the LEDs bounce like KIT from K2000 { PORTA ^= (1 << LED1); _delay_ms(200); PORTA ^= (1 << LED1); PORTB ^= (1 << LED2); _delay_ms(200); PORTB ^= (1 << LED2); PORTB ^= (1 << LED3); _delay_ms(200); PORTB ^= (1 << LED3); PORTB ^= (1 << LED4); _delay_ms(200); PORTB ^= (1 << LED4); PORTB ^= (1 << LED3); _delay_ms(200); PORTB ^= (1 << LED3); PORTB ^= (1 << LED2); _delay_ms(200); PORTB ^= (1 << LED2); } void displayLevel(int16_t measure) // function displaying the intensity of what the sensor is getting { float level = (float) measure*1024/CALIBRATION; // define the maximum value here if( level<50 ) { PORTA = (0 << LED1); PORTB = (0 << LED2) | (0 << LED3) | (0 << LED4); } if( level>=50 && level<256 ) { PORTA |= (1 << LED1); PORTB = (0 << LED2) | (0 << LED3) | (0 << LED4); } if( level>=256 && level<512 ) { PORTA |= (1 << LED1); PORTB = (1 << LED2) | (0 << LED3) | (0 << LED4); } if( level>=512 && level<768 ) { PORTA |= (1 << LED1); PORTB = (1 << LED2) | (1 << LED3) | (0 << LED4); } if( level>=768 && level<1024 ) { PORTA |= (1 << LED1); PORTB = (1 << LED2) | (1 << LED3) | (1 << LED4); } } void turnOn(int led) { if (led == 1) { PORTA ^= (1 << LED1); } if (led == 2) { PORTB ^= (1 << LED2); } if (led == 3) { PORTB ^= (1 << LED3); } if (led == 4) { PORTB ^= (1 << LED4); } } int main() { uint16_t measurement = 0; uint16_t up, down; uint8_t sampling; static char chr; char str[10]; DDRA |= (1 << LED1); // define LED pins as outputs DDRB |= (1 << LED2); DDRB |= (1 << LED3); DDRB |= (1 << LED4); DDRA |= (1 << PULSE); CLKPR = (1 << CLKPCE); CLKPR = (0 << CLKPS3) | (0 << CLKPS2) | (0 << CLKPS1) | (0 << CLKPS0); // clock is not divided set(serial_port, serial_pin_out); output(serial_direction, serial_pin_out); ADMUX = (0 << REFS1) | (0 << REFS0) // use Vcc as a reference | (0 << MUX5) | (0 << MUX4) | (0 << MUX3) | (0 << MUX2) | (1 << MUX1) | (0 << MUX0); // set PA2 as the analog input ADCSRB = (0 << ADLAR) // the result is stored in ADCH and ADCL over 10 bits adjusted right | (0 << ADTS2) | (0 << ADTS1) | (0 << ADTS0); // free running mode ADCSRA = (1 << ADEN) // enable Analog to Digital Converter | (1 << ADPS2) | (1 << ADPS1) | (1 << ADPS0); // set prescaler to 128 bouncy(); while(1) { ADCSRA |= (1 << ADSC); // start conversion while (ADCSRA & (1 << ADSC)); measurement = (ADCH << 8) | ADCL; chr = ADCL; chr = ADCH; sprintf(str, "%d", measurement); // convert measurement into a string displayLevel(measurement); // display it with the LEDs put_string(&serial_port, serial_pin_out, str); put_char(&serial_port, serial_pin_out, 10); _delay_ms(50); } }