for distance sensor

Dependencies:   mbed HCSR04

main.cpp

Committer:
mlosa010
Date:
2019-11-15
Revision:
0:47ec319db65c

File content as of revision 0:47ec319db65c:

#define LENOFBOT 2.5
#define LENOFSENSOR 0.394
HCSR04 mySensor(p26,p13);
Serial pc(USBTX, USBRX);
PwmOut trigger(p24);
PwmOut trigger_b(p23);
InterruptIn echoStart(p13,PullDown);
InterruptIn echoStop(p14,PullDown);
Timer timer;
Timer timer_b;
volatile float front_reading;
volatile float back_sensor;
void start(void){
    timer.start();
    }
void stop (void){
    timer.stop();
    front_reading = (float)timer.read()*13504;
    timer.reset();
    }
void start_back(void){
    timer_b.start();
    
    }
void stop_back (void){
    timer_b.stop();
    back_sensor = (float)timer_b.read()*13480;
    timer_b.reset();
    }
int main() {
    echoStart.rise(&start);
    echoStart.fall(&stop);
    echoStop.rise(&start_back);
    echoStop.fall(&stop_back);
    trigger.period(0.06);
    int pi[]= {3,1,4,2};
    int currentdigit =0;
    int front_distance;
    int back_distance;
    while(1) {
        trigger.pulsewidth_us(10);
        front_distance = (3*pi[currentdigit])+9;
        back_distance= 27-front_distance;
        
        if((front_reading/2)+LENOFBOT+LENOFSENSOR>front_distance){
        pc.printf("moving back distance read: %.3f digit: %d\n\r",(front_reading/2)+LENOFBOT+LENOFSENSOR,pi[1]);
        if(((back_sensor/2)+LENOFBOT+LENOFSENSOR <= back_distance-0.1||(back_sensor/2)+LENOFBOT+LENOFSENSOR <= back_distance+0.1))
            int r =1;
            //currentdigit++;
        }else if((front_reading/2)+LENOFBOT+LENOFSENSOR<=front_distance){
            pc.printf("moving forward distance read: %.3f should be %d\n\r",(front_reading/2)+LENOFBOT+LENOFSENSOR, pi[0]);
        if((back_sensor/2)+LENOFBOT+LENOFSENSOR <= back_distance-0.1||(back_sensor/2)+LENOFBOT+LENOFSENSOR <= back_distance+0.1)
            int i=1;
            //currentdigit++;
        }
    }
}