An auto car with 3 IR sensors.

Dependencies:   Ping

main.cpp

Committer:
cudaChen
Date:
2018-07-01
Revision:
13:87cd0ae37e06
Parent:
12:e95ed962be7a
Child:
15:1d440beb24d3

File content as of revision 13:87cd0ae37e06:

#include "mbed.h"

#include "autocar.h"

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

// PID control params
int values = 0;
float Kp = 0.2, Ki = 0.0001, Kd = 1;

// 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);
            values = readIRValues(); // read IR values

            //driveMotor(left, middle, right);
            driveMotorPID(values, Kp, Ki, Kd);
        }
    }
}