diff --git a/firmware/apps/buggy.c b/firmware/apps/buggy.c index e04d436..080b5ea 100644 --- a/firmware/apps/buggy.c +++ b/firmware/apps/buggy.c @@ -13,7 +13,7 @@ static void init(void) { static void run(void) { uint16_t light = photons_measure(); - pentatonic_all_led_set(light >> 7); + pentatonic_all_led_set(light >> 3); wait_ms(500); } diff --git a/firmware/include/pentabug/photons.h b/firmware/include/pentabug/photons.h index ff2b5cd..b2cd2ce 100644 --- a/firmware/include/pentabug/photons.h +++ b/firmware/include/pentabug/photons.h @@ -5,6 +5,6 @@ void photons_init(void); -uint16_t photons_measure(void); +uint8_t photons_measure(void); #endif /* PHOTONS_H */ diff --git a/firmware/lib/photons.c b/firmware/lib/photons.c index 498236c..20653f0 100644 --- a/firmware/lib/photons.c +++ b/firmware/lib/photons.c @@ -5,9 +5,11 @@ #include #include -void photons_init(void) {} +void photons_init(void) { + ADCSRA |= (1 << ADEN); +} -uint16_t photons_measure(void) { +uint8_t photons_measure(void) { // save old state uint8_t old_port = PORTC; @@ -18,34 +20,40 @@ uint16_t photons_measure(void) { led_off(RIGHT); led_off(LEFT); - // set direction, no pullups + // set to ground for discharge - DDRC &= ~(1 << 3); PORTC &= ~(1 << 3); // wait for discharge - // TODO: needed? - wait_ms(10); + wait_ms(1); + + // set direction to input for measurement + + DDRC &= ~(1 << 3); + + // collect some photons + + wait_ms(1); // start measurement - ADMUX = (1 << REFS0); - ADCSRA = (1 << ADPS2) | (1 << ADPS1); - ADCSRA |= (1 << ADEN); + ADMUX = (1 << REFS0) | (1 << ADLAR); + ADCSRA |= (1 << ADPS2) | (1 << ADPS1); ADMUX = (ADMUX & ~(0x1F)) | 3; + ADCSRA |= (1 << ADSC); // wait for measurement to finish while (ADCSRA & (1 << ADSC)) test_stop_app(); - uint16_t res = ADCW; + uint8_t res = ADCH; // disable adc - ADCSRA &= ~(1 << ADEN); + /*ADCSRA &= ~(1 << ADEN);*/ // restore state