Important changes to forums and questions
All forums and questions are now archived. To start a new conversation or read the latest updates go to forums.mbed.com.
8 years, 6 months ago.
Interrupt in problem with sensor
I want to read an Ultrasonic sensor srf05, but i don't understand why my program doesn't work. Pin are good, but the "echo.rise(...)" doesn't detect anythink.
Device: LPC1768
Thank
main.cpp
#include "mbed.h" DigitalOut trigger(p24); InterruptIn echo(p23); Serial pc(USBTX, USBRX); int distance; Timer t; Ticker mesure; void mesurer() { trigger=1; //Send the trigger signal to get measurment wait_us(10); trigger=0; wait_us(10); } void start_timer() { t.reset(); // rising edge detected t.start(); // start measurment } void lecture_distance() { t.stop(); // falling edge detected distance=(t.read_us()/58); } int main() { pc.baud(9600); mesure.attach(&mesurer,0.5); //10 mesures par seconde echo.rise(&start_timer); echo.fall(&lecture_distance); while(1){ printf("Distance %d cm\n\r", distance); } }
1 Answer
8 years, 6 months ago.
Hello Emma,
I'm not sure but most likely the serial terminal on your PC is overwhelmed with data sent by the printf
function in the while
loop. Try to modify the code as follows:
#include "mbed.h" DigitalOut trigger(p24); InterruptIn echo(p23); Serial pc(USBTX, USBRX); int distance; Timer t; Ticker mesure; void mesurer(void) { trigger = 1; //Send the trigger signal to get measurement wait_us(10); trigger = 0; //wait_us(10); } void start_timer(void) { t.reset(); // rising edge detected t.start(); // start measurement } void lecture_distance(void) { t.stop(); // falling edge detected distance = (t.read_us() / 58); printf("Distance %d cm\n\r", distance); } int main(void) { pc.baud(9600); mesure.attach(&mesurer, 0.5); //two measurements per second echo.rise(&start_timer); echo.fall(&lecture_distance); while(1) {} }