s2 project 1st try

Dependencies:   TB6612 HCSR04 UoY-serial

main.cpp

Committer:
sas638
Date:
2022-05-08
Revision:
5:57196959be02
Parent:
4:137b119e5198

File content as of revision 5:57196959be02:

#include "mbed.h"
#include "HCSR04.h"
#include "tb6612.h"
//Library for controlling ultrasonic module HCSR04
//Ported by hiawoood from arduino library orgininally created by ITead studio.
HCSR04 sensorF(D7,D2); //Create sensor object of type HCSR04 (class borrowed HCSR04.cpp), with pins D2 and D3 assigned to trig and echo respectively
HCSR04 sensorL(D12,D13);
//HCSR04 sensorR(D9,D10);
TB6612 motorL(D9,D10,D11);
TB6612 motorR(D3,D4,D5);
Thread turnL;
Thread turnR;
Thread turnN;

void turnLFN() {
    motorL.setSpeed(-1);
    motorR.setSpeed(1);
    thread_sleep_for(1500);
    }
    
void turnRFN() {
    motorR.setSpeed(-1);
    motorL.setSpeed(1);
    thread_sleep_for(1500);
    }

void turnNFN() {
    motorL.setSpeed(1);
    motorR.setSpeed(1);
    }

//long distanceL = sensorL.distance(CM);
int turn = 5000;

int main()
{
    long distanceF;
    long distanceL;

    while(true) {
        distanceF = sensorF.distance(CM);
        distanceL = sensorL.distance(CM);

        if (distanceL<10 && distanceF>10) {
            /*motorL.setSpeed(0.50);
            motorR.setSpeed(0.50);
            printf ("forward \r\n");*/
            turnN.start(turnNFN);
        }

        if (distanceL>=10) {
            /*motorL.setSpeed(-0.50);
            motorR.setSpeed(0.50);
            printf ("turn left \r\n");
            thread_sleep_for(turn);
            
            motorL.setSpeed(0.50);
            motorR.setSpeed(0.50);
            thread_sleep_for(300);*/
            turnL.start(turnLFN);
        }

        if (distanceL<10 && distanceF<10) {
            /*motorL.setSpeed(0.50);
            motorR.setSpeed(-0.50);
            printf("turn right \r\n");
            thread_sleep_for(turn);*/
            turnR.start(turnRFN);
        }
    }
}