Initial attempt at the final chalenge
Dependencies: ContinuousServo TCS3472_I2C Tach mbed
main.cpp@0:10faa982ae23, 2018-04-25 (annotated)
- Committer:
- nbchaskin
- Date:
- Wed Apr 25 14:19:25 2018 +0000
- Revision:
- 0:10faa982ae23
- Child:
- 1:d57be3c5dae9
- Child:
- 2:141024530b2f
Rough initial attempt
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
nbchaskin | 0:10faa982ae23 | 1 | #include "mbed.h" |
nbchaskin | 0:10faa982ae23 | 2 | #include "ContinuousServo.h" |
nbchaskin | 0:10faa982ae23 | 3 | #include "Tach.h" |
nbchaskin | 0:10faa982ae23 | 4 | #include "TCS3472_I2C.h" |
nbchaskin | 0:10faa982ae23 | 5 | //driving |
nbchaskin | 0:10faa982ae23 | 6 | Tach tLeft(p17,64); |
nbchaskin | 0:10faa982ae23 | 7 | Tach tRight(p13,64); |
nbchaskin | 0:10faa982ae23 | 8 | |
nbchaskin | 0:10faa982ae23 | 9 | ContinuousServo left(p23); |
nbchaskin | 0:10faa982ae23 | 10 | ContinuousServo right(p26); |
nbchaskin | 0:10faa982ae23 | 11 | |
nbchaskin | 0:10faa982ae23 | 12 | //color sensor |
nbchaskin | 0:10faa982ae23 | 13 | PwmOut LB(p25); // PWM out signal to power LED on the sensor |
nbchaskin | 0:10faa982ae23 | 14 | TCS3472_I2C rgb_sensor( p9, p10 ); // Establish RGB sensor object |
nbchaskin | 0:10faa982ae23 | 15 | Serial pc(USBTX,USBRX); // Establish serial connection with computer |
nbchaskin | 0:10faa982ae23 | 16 | |
nbchaskin | 0:10faa982ae23 | 17 | //Hall effect |
nbchaskin | 0:10faa982ae23 | 18 | InterruptIn hall(p21); |
nbchaskin | 0:10faa982ae23 | 19 | DigitalOut led4(LED4); |
nbchaskin | 0:10faa982ae23 | 20 | DigitalOut toggle(p22); |
nbchaskin | 0:10faa982ae23 | 21 | int counter = 0; |
nbchaskin | 0:10faa982ae23 | 22 | |
nbchaskin | 0:10faa982ae23 | 23 | //need distance to stop |
nbchaskin | 0:10faa982ae23 | 24 | float distance; |
nbchaskin | 0:10faa982ae23 | 25 | distance = //inches; |
nbchaskin | 0:10faa982ae23 | 26 | distance = //conversion to analog value; |
nbchaskin | 0:10faa982ae23 | 27 | |
nbchaskin | 0:10faa982ae23 | 28 | int main() { |
nbchaskin | 0:10faa982ae23 | 29 | hall.mode(PullUp); |
nbchaskin | 0:10faa982ae23 | 30 | toggle = 1; |
nbchaskin | 0:10faa982ae23 | 31 | led4 = 0; |
nbchaskin | 0:10faa982ae23 | 32 | |
nbchaskin | 0:10faa982ae23 | 33 | rgb_sensor.enablePowerAndRGBC(); // Enable RGB sensor |
nbchaskin | 0:10faa982ae23 | 34 | rgb_sensor.setIntegrationTime(100); // Set integration time of sensor |
nbchaskin | 0:10faa982ae23 | 35 | int rgb_data[4]; // declare a 4 element array to store RGB sensor readings |
nbchaskin | 0:10faa982ae23 | 36 | float PWMbrightness = 1.0; // float specifying brightness of LED (between 0.0 and 1.0) |
nbchaskin | 0:10faa982ae23 | 37 | |
nbchaskin | 0:10faa982ae23 | 38 | while(1) { |
nbchaskin | 0:10faa982ae23 | 39 | LB = PWMbrightness; // set brightness of sensor LED |
nbchaskin | 0:10faa982ae23 | 40 | rgb_sensor.getAllColors( rgb_data ); // read the sensor to get red, green, and blue color data along with overall brightness |
nbchaskin | 0:10faa982ae23 | 41 | |
nbchaskin | 0:10faa982ae23 | 42 | if(hall == 1){ |
nbchaskin | 0:10faa982ae23 | 43 | led4 = 1; |
nbchaskin | 0:10faa982ae23 | 44 | wait(0.4); |
nbchaskin | 0:10faa982ae23 | 45 | toggle = 0; |
nbchaskin | 0:10faa982ae23 | 46 | wait(0.1); |
nbchaskin | 0:10faa982ae23 | 47 | toggle = 1; |
nbchaskin | 0:10faa982ae23 | 48 | led4 = 0; |
nbchaskin | 0:10faa982ae23 | 49 | counter = counter +1; |
nbchaskin | 0:10faa982ae23 | 50 | printf("Counter=%d\n",counter); |
nbchaskin | 0:10faa982ae23 | 51 | } |
nbchaskin | 0:10faa982ae23 | 52 | if (sonar > distance){ |
nbchaskin | 0:10faa982ae23 | 53 | float speed = 0.5; |
nbchaskin | 0:10faa982ae23 | 54 | left.speed(speed); |
nbchaskin | 0:10faa982ae23 | 55 | right.speed(-speed); |
nbchaskin | 0:10faa982ae23 | 56 | if (rgb_data[] > ){//add value for too dark |
nbchaskin | 0:10faa982ae23 | 57 | right.speed(-speed - 0.05); |
nbchaskin | 0:10faa982ae23 | 58 | left.speed(speed - 0.05); |
nbchaskin | 0:10faa982ae23 | 59 | } |
nbchaskin | 0:10faa982ae23 | 60 | else if (rgb_data[] <){//add value for too light |
nbchaskin | 0:10faa982ae23 | 61 | right.speed(-speed + 0.05); |
nbchaskin | 0:10faa982ae23 | 62 | left.speed(speed + 0.05); |
nbchaskin | 0:10faa982ae23 | 63 | } |
nbchaskin | 0:10faa982ae23 | 64 | } |
nbchaskin | 0:10faa982ae23 | 65 | else { |
nbchaskin | 0:10faa982ae23 | 66 | break; |
nbchaskin | 0:10faa982ae23 | 67 | } |
nbchaskin | 0:10faa982ae23 | 68 | } |
nbchaskin | 0:10faa982ae23 | 69 | } |