Final

Dependencies:   Motor TCS3472_I2C mbed

Committer:
m173120
Date:
Tue Apr 28 01:17:38 2015 +0000
Revision:
0:3b44eb67d53a
Final Project Operating Code

Who changed what in which revision?

UserRevisionLine numberNew contents of line
m173120 0:3b44eb67d53a 1 #include "mbed.h"
m173120 0:3b44eb67d53a 2 #include "TCS3472_I2C.h"
m173120 0:3b44eb67d53a 3 #include "Motor.h"
m173120 0:3b44eb67d53a 4
m173120 0:3b44eb67d53a 5 PwmOut LB(p22); // PWM out signal to power LED on the sensor
m173120 0:3b44eb67d53a 6 TCS3472_I2C rgb_sensor( p9, p10 ); // Establish RGB sensor object
m173120 0:3b44eb67d53a 7
m173120 0:3b44eb67d53a 8 Motor m(p26, p30, p29);
m173120 0:3b44eb67d53a 9 Motor m2(p25, p27, p28);
m173120 0:3b44eb67d53a 10 AnalogIn ultraSonic(p17);
m173120 0:3b44eb67d53a 11 int bob=0;
m173120 0:3b44eb67d53a 12
m173120 0:3b44eb67d53a 13 int main()
m173120 0:3b44eb67d53a 14 {
m173120 0:3b44eb67d53a 15 rgb_sensor.enablePowerAndRGBC(); // Enable RGB sensor
m173120 0:3b44eb67d53a 16 rgb_sensor.setIntegrationTime(100); // Set integration time of sensor
m173120 0:3b44eb67d53a 17
m173120 0:3b44eb67d53a 18 float measurements = ultraSonic.read();
m173120 0:3b44eb67d53a 19 float meas_dist = 531.3726*measurements + 0.8440;
m173120 0:3b44eb67d53a 20
m173120 0:3b44eb67d53a 21
m173120 0:3b44eb67d53a 22 int rgb_data[4]; // declare a 3 element array to store RGB sensor readings
m173120 0:3b44eb67d53a 23 float PWMbrightness = 1.0; // declare a float specifying brightness of LED (between 0.0 and 1.0)
m173120 0:3b44eb67d53a 24 LB = PWMbrightness; // set brightness of sensor LED
m173120 0:3b44eb67d53a 25 wait(1);
m173120 0:3b44eb67d53a 26 while(meas_dist > 20) {
m173120 0:3b44eb67d53a 27
m173120 0:3b44eb67d53a 28
m173120 0:3b44eb67d53a 29 rgb_sensor.getAllColors( rgb_data ); // read the sensor to get red, green, and blue color data along with overall brightness
m173120 0:3b44eb67d53a 30
m173120 0:3b44eb67d53a 31 // Print data to serial port
m173120 0:3b44eb67d53a 32 m2.speed(0);
m173120 0:3b44eb67d53a 33 m.speed(.6);
m173120 0:3b44eb67d53a 34 while ((rgb_data[2] >= 300)) {//Right If Green color is greater than 200 (the color is green)
m173120 0:3b44eb67d53a 35 m2.speed(.6);
m173120 0:3b44eb67d53a 36 m.speed(0);
m173120 0:3b44eb67d53a 37 rgb_sensor.getAllColors( rgb_data );
m173120 0:3b44eb67d53a 38
m173120 0:3b44eb67d53a 39 measurements = ultraSonic.read();
m173120 0:3b44eb67d53a 40 meas_dist = 531.3726*measurements + 0.8440;
m173120 0:3b44eb67d53a 41 if (meas_dist <10)
m173120 0:3b44eb67d53a 42 {
m173120 0:3b44eb67d53a 43 //int bob=1;
m173120 0:3b44eb67d53a 44 break;};
m173120 0:3b44eb67d53a 45 };
m173120 0:3b44eb67d53a 46 while ((rgb_data[1] <= 225)) {//if red color is less than 180 (the color is blue)
m173120 0:3b44eb67d53a 47 m2.speed(0);
m173120 0:3b44eb67d53a 48 m.speed(0.5);
m173120 0:3b44eb67d53a 49 rgb_sensor.getAllColors( rgb_data );
m173120 0:3b44eb67d53a 50
m173120 0:3b44eb67d53a 51 measurements = ultraSonic.read();
m173120 0:3b44eb67d53a 52 meas_dist = 531.3726*measurements + 0.8440;
m173120 0:3b44eb67d53a 53 if (meas_dist <10)
m173120 0:3b44eb67d53a 54 {
m173120 0:3b44eb67d53a 55 //int bob=1;
m173120 0:3b44eb67d53a 56 break;};
m173120 0:3b44eb67d53a 57 };
m173120 0:3b44eb67d53a 58 while ((rgb_data[3] <= 180)) {//Left If the blue is less than 95 (the color is red)
m173120 0:3b44eb67d53a 59 m2.speed(-.6);
m173120 0:3b44eb67d53a 60 m.speed(0);
m173120 0:3b44eb67d53a 61 rgb_sensor.getAllColors( rgb_data );
m173120 0:3b44eb67d53a 62
m173120 0:3b44eb67d53a 63 measurements = ultraSonic.read();
m173120 0:3b44eb67d53a 64 meas_dist = 531.3726*measurements + 0.8440;
m173120 0:3b44eb67d53a 65 if (meas_dist <10)
m173120 0:3b44eb67d53a 66 {
m173120 0:3b44eb67d53a 67 //int bob=1;
m173120 0:3b44eb67d53a 68 break;
m173120 0:3b44eb67d53a 69 };
m173120 0:3b44eb67d53a 70 };
m173120 0:3b44eb67d53a 71
m173120 0:3b44eb67d53a 72 };
m173120 0:3b44eb67d53a 73 m.speed(0);
m173120 0:3b44eb67d53a 74 m2.speed(0);
m173120 0:3b44eb67d53a 75 wait(.75);
m173120 0:3b44eb67d53a 76
m173120 0:3b44eb67d53a 77 }