Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: mbed-src anaDMA stm32_adafruit
main.cpp
- Committer:
- willybayot
- Date:
- 2014-12-31
- Revision:
- 0:a9ab98434bf2
- Child:
- 1:02d3732e1d76
File content as of revision 0:a9ab98434bf2:
#include "mbed.h"
PwmOut mypwm(PWM_OUT);          // This is the TX pulse pin
AnalogIn adc1(PA_0);              // This is the ADC pin
PwmOut buzzer(PC_9);             // This is the digital out pin connected to the buzzer
InterruptIn event(PA_0);          // This sets a semaphore on rising front of pulse pin
DigitalIn button(USER_BUTTON);
Timeout timer;                  // This sets the net reading period
float reading;                  // most current ADC capture value (0.0 to 1.0)
bool sema, end_period;
// system parameters
float Gradient_Threshold = 0.05;    // audio threshold 
int reading_period = 10000;         // in µsec
int integration_ratio = 10;
int pulse_period = 207;
int pulse_width = 200;
void read_one_pulse () {
    sema = false;
    while (!sema)
        ;
    reading += adc1.read();
    }
void trigger () {
    sema = true;
    }
void TO() {
   end_period = true;
   }
   
int main() {
 
float ref_reading;          // Reference reading of grounf signal
int sample_count;
int n, delta;
  
    mypwm.period_us(pulse_period);
    mypwm.pulsewidth_us(0);
    buzzer.period_ms(0);
    buzzer.write(50);
    event.rise(&trigger);
    
    while (button == 1)     //Wait to start session until button is pressed and released
        ;
    while (button == 0)
        ;       
    sample_count = 0;
    while(1) {
        end_period = false;
        reading = 0;
        timer.attach_us(&TO,reading_period);
        mypwm.pulsewidth_us(pulse_width);       // Start TX pulses
        for (n=0;n<integration_ratio;n++)
            read_one_pulse();                   // Accumulate ADC captures into reading
        mypwm.pulsewidth_us(0);                 // Stop TX pulses
        reading /= integration_ratio;           // Average reading
        if (sample_count == 0) {                // If first reading of session, save reading into ground reference signal level
            ref_reading = reading;
            sample_count++;
            }
        delta = reading - ref_reading;          // Delta is the gradient
        if (abs(delta) > Gradient_Threshold )   // if hte graient is larger than a threshold, start buzzing
            buzzer.period_ms(1);
        else 
            buzzer.period_ms(0);
        if (delta != 0 && abs(delta) <= 0.01 )  // If delta is small enough but not nul, update the ref_reading with th ecurrent reading through a powerful low pass FIR filter.
            ref_reading = (ref_reading*127 + reading ) / 128;
        if (button == 0) {                      // pushing again on the button and relasing it stops the session
            while (button == 0)
                ;
            return(0);
            }
        while (!end_period)             // Wait for end of reading period (i.e. 10msec)
            ;
      }
}