Initial attempt at the final chalenge

Dependencies:   ContinuousServo TCS3472_I2C Tach mbed

main.cpp

Committer:
nbchaskin
Date:
2018-04-25
Revision:
0:10faa982ae23
Child:
1:d57be3c5dae9
Child:
2:141024530b2f

File content as of revision 0:10faa982ae23:

#include "mbed.h"
#include "ContinuousServo.h"
#include "Tach.h"
#include "TCS3472_I2C.h"
//driving
Tach tLeft(p17,64);
Tach tRight(p13,64);

ContinuousServo left(p23);
ContinuousServo right(p26);

//color sensor
PwmOut LB(p25); // PWM out signal to power LED on the sensor
TCS3472_I2C rgb_sensor( p9, p10 ); // Establish RGB sensor object
Serial pc(USBTX,USBRX); // Establish serial connection with computer

//Hall effect
InterruptIn hall(p21);
DigitalOut led4(LED4);
DigitalOut toggle(p22);
int counter = 0;

//need distance to stop
float distance;
distance = //inches;
distance = //conversion to analog value;

int main() {
    hall.mode(PullUp);
    toggle = 1;
    led4 = 0;
    
    rgb_sensor.enablePowerAndRGBC(); // Enable RGB sensor
    rgb_sensor.setIntegrationTime(100); // Set integration time of sensor
    int rgb_data[4]; // declare a 4 element array to store RGB sensor readings
    float PWMbrightness = 1.0; // float specifying brightness of LED (between 0.0 and 1.0)
 
    while(1) {
        LB = PWMbrightness; // set brightness of sensor LED
        rgb_sensor.getAllColors( rgb_data ); // read the sensor to get red, green, and blue color data along with overall brightness
        
       if(hall == 1){
           led4 = 1;   
           wait(0.4);
           toggle = 0;
           wait(0.1);
           toggle = 1;
           led4 = 0;
           counter = counter +1;
           printf("Counter=%d\n",counter);
           }
        if (sonar > distance){
            float speed = 0.5;
            left.speed(speed);
            right.speed(-speed);
            if (rgb_data[] > ){//add value for too dark
                right.speed(-speed - 0.05);
                left.speed(speed - 0.05);
                }
            else if (rgb_data[] <){//add value for too light
                right.speed(-speed + 0.05);
                left.speed(speed + 0.05);
                }
            }
        else {
            break;
            }
    }
}