cockroach: Some random behaviour

This commit is contained in:
Thammi 2012-10-12 02:16:21 +02:00
parent 9ac3083a8b
commit c559d7884b

View File

@ -8,6 +8,10 @@
#include "../lib/music.h" #include "../lib/music.h"
#include "../lib/usart.h" #include "../lib/usart.h"
inline uint16_t biased_random(uint16_t value) {
return value / 8 * (rand() & 7);
}
static void led_sound(void) static void led_sound(void)
{ {
static bool discharge; static bool discharge;
@ -26,6 +30,8 @@ static void led_sound(void)
} }
if (timer_expired(&mytimer)) { if (timer_expired(&mytimer)) {
if (discharge) { if (discharge) {
led_off(LED_R | LED_L);
DDRC |= (1 << PORTC0) | (1 << PORTC1); //set LED Ports to output: DDRC |= (1 << PORTC0) | (1 << PORTC1); //set LED Ports to output:
/*discharge*/ /*discharge*/
PORTC = (PORTC & 0b11110000); PORTC = (PORTC & 0b11110000);
@ -34,6 +40,8 @@ static void led_sound(void)
/*pull ups off*/ /*pull ups off*/
PORTC &= ~(1 << PORTC0); PORTC &= ~(1 << PORTC0);
discharge = false; discharge = false;
timer_set(&mytimer, 1);
} else { } else {
/*single measurement*/ /*single measurement*/
ADMUX = (ADMUX & ~(0x1F)) | 0; // select channel 0 ADMUX = (ADMUX & ~(0x1F)) | 0; // select channel 0
@ -41,11 +49,33 @@ static void led_sound(void)
while (ADCSRA & (1 << ADSC)) ; // wait for conversion to end while (ADCSRA & (1 << ADSC)) ; // wait for conversion to end
led1 = ADCW; // read result led1 = ADCW; // read result
set_motor(led1 > 0x50 ? MOTOR_ON : MOTOR_OFF); DDRC |= (1 << PORTC0) | (1 << PORTC1); //set LED Ports to output:
set_motor(biased_random(led1) > 0x50 ? MOTOR_ON : MOTOR_OFF);
if(biased_random(led1) > 0x20) {
led_on(LED_R);
} else {
led_off(LED_R);
}
if(biased_random(led1) > 0x20) {
led_on(LED_L);
} else {
led_off(LED_L);
}
if(biased_random(led1) > 0x90) {
uint16_t tone = (biased_random(led1) * 2) + 500;
music_setNote(tone, 0);
} else {
music_setNote(NOTE_PAUSE, 0);
}
discharge = true; discharge = true;
timer_set(&mytimer, 20);
} }
timer_set(&mytimer, 10); //relaunch timer
} //end if timer_expired } //end if timer_expired
} //end mode5 } //end mode5