An auto car with 3 IR sensors.

Dependencies:   Ping

main.cpp

Committer:
cudaChen
Date:
2018-06-30
Revision:
12:e95ed962be7a
Parent:
11:3e9d4c345ebd
Child:
13:87cd0ae37e06

File content as of revision 12:e95ed962be7a:

#include "mbed.h"

#include "autocar.h"

// trigger button and triggered LED
DigitalOut led1(LED1);
DigitalIn pb(PC_13);
int lastButtonState = 0;
bool ledState = false;

// main() runs in its own thread in the OS
int main()
{
    bool left = false;
    bool middle = false;
    bool right = false;

    // here I use 500 as threshold
    int threshold = 500;

    // set two motors to stop
    init();

    while (true) {
        int reading1 = pb.read();

        if(reading1 != lastButtonState) {
            wait_ms(20);

            int reading2 = pb.read();

            if(reading2 == reading1) {
                lastButtonState = reading2;
            }

            if(lastButtonState == 1) {
                ledState = !ledState;
            }
        }

        led1.write(ledState);

        if(ledState) {
            // not on track: > 500
            // on track (black): < 500
            readIR(&left, &middle, &right, threshold);

            driveMotor(left, middle, right);
        }
    }
}