rattling and beeping when detecting sound

This commit is contained in:
bigalex 2012-10-06 00:06:04 +02:00
parent 30851157c7
commit 935dcba060
3 changed files with 45 additions and 23 deletions

View File

@ -128,7 +128,11 @@ void init_motor(void)
} }
void set_motor(int val){ void set_motor(int val){
PORTB = ~(val); if (val==MOTOR_ON) {
PORTB |= 0x02;
} else {
PORTB &= ~(0x02);
}
return; return;
} }

View File

@ -11,8 +11,8 @@ enum { BUZZR_OUT, //initialize buzzer for OUTPUT mode (emmiting soundwaves)
#define LED_L (1 << PORTC0) #define LED_L (1 << PORTC0)
#define LED_R (1 << PORTC2) #define LED_R (1 << PORTC2)
#define MOTOR_ON (0<<PORTB1) #define MOTOR_ON 1
#define MOTOR_OFF (1<<PORTB1) #define MOTOR_OFF 0
#define BTN_RIGHT 1 #define BTN_RIGHT 1
#define BTN_LEFT 0 #define BTN_LEFT 0

View File

@ -46,32 +46,50 @@ void do_mode0(void){
static uint16_t maxval; static uint16_t maxval;
static uint16_t lastmaxval; static uint16_t lastmaxval;
uint16_t curval; uint16_t curval;
static uint16_t soundon;
if (ModeChanged){ if (ModeChanged){
timer_set(&mytimer, 10); timer_set(&mytimer, 10);
maxval=0; maxval=0;
ModeChanged = false; ModeChanged = false;
soundon = false;
init_mic(); init_mic();
maxval=0; maxval=0;
}; };
// single measurement
curval =ADCW; // read result
maxval = (curval>maxval) ? curval : maxval;
if(timer_expired(&mytimer)){ if(timer_expired(&mytimer)){
if (soundon) {
//turn off sound
music_setNote(NOTE_PAUSE,0); //mute
set_motor(MOTOR_OFF);
//re-init mic
init_mic();
led_off(LED_R|LED_L);
timer_set(&mytimer, 1);
soundon = false;
lastmaxval= 10000;
} else { //sound was off wer're in measuring mode
if(maxval>lastmaxval){ if(maxval>lastmaxval){
USART0_put_uint16(maxval); USART0_put_uint16(maxval);
USART0_crlf(); USART0_crlf();
}; led_on(LED_R|LED_L);
init_buzzr(); //buzzr to output
music_setNote(NOTE_C,5);
set_motor(MOTOR_ON);
soundon = true;
timer_set(&mytimer, 5); //sound duration
} else {
timer_set(&mytimer, 1); timer_set(&mytimer, 1);
if (maxval>lastmaxval) {led_on(LED_R|LED_L);} else {led_off(LED_R|LED_L);}; };
lastmaxval=maxval; lastmaxval=maxval;
maxval=0; maxval=0;
}; //end if soundon
}; //end if timer_expired }; //end if timer_expired
// single measurement
curval =ADCW; // read result
maxval = (curval>maxval) ? curval : maxval;
}; //end do_mode0 }; //end do_mode0