Colors

Dependencies:   mbed TCS3472_I2C Tach ContinuousServo

Committer:
ewingswim
Date:
Mon Apr 22 14:47:32 2019 +0000
Revision:
1:99d1aa030525
Parent:
0:97e8d0a2752d
brightness;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ewingswim 0:97e8d0a2752d 1 #include "mbed.h"
ewingswim 0:97e8d0a2752d 2 #include "ContinuousServo.h"
ewingswim 0:97e8d0a2752d 3 #include "Tach.h"
ewingswim 0:97e8d0a2752d 4 #include "TCS3472_I2C.h"
ewingswim 0:97e8d0a2752d 5
ewingswim 0:97e8d0a2752d 6 // color sensor
ewingswim 0:97e8d0a2752d 7 PwmOut LB(p22); //PWM out signal to power LED on teh sensor
ewingswim 0:97e8d0a2752d 8 TCS3472_I2C rgb_sensor(p9, p10); // Establish RGB sensor object
ewingswim 0:97e8d0a2752d 9 Serial pc(USBTX,USBRX); //Establish serial connection
ewingswim 0:97e8d0a2752d 10 int rgb_data[4]; //store sensor readings
ewingswim 0:97e8d0a2752d 11
ewingswim 0:97e8d0a2752d 12 //lights for colors
ewingswim 0:97e8d0a2752d 13 DigitalOut red(LED1);
ewingswim 0:97e8d0a2752d 14 DigitalOut green(LED2);
ewingswim 0:97e8d0a2752d 15 DigitalOut blue(LED3);
ewingswim 0:97e8d0a2752d 16 //DigitalOut magnet(LED4);
ewingswim 0:97e8d0a2752d 17
ewingswim 0:97e8d0a2752d 18 ContinuousServo left(p23);
ewingswim 0:97e8d0a2752d 19 ContinuousServo right(p26);
ewingswim 0:97e8d0a2752d 20
ewingswim 0:97e8d0a2752d 21 //encoders
ewingswim 0:97e8d0a2752d 22 Tach tLeft(p17,64);
ewingswim 0:97e8d0a2752d 23 Tach tRight(p13,64);
ewingswim 0:97e8d0a2752d 24
ewingswim 0:97e8d0a2752d 25 int main()
ewingswim 0:97e8d0a2752d 26 {
ewingswim 0:97e8d0a2752d 27 rgb_sensor.enablePowerAndRGBC(); //Enable RGB sensor
ewingswim 0:97e8d0a2752d 28 rgb_sensor.setIntegrationTime(100); //Set integration time of sensor
ewingswim 0:97e8d0a2752d 29
ewingswim 0:97e8d0a2752d 30 pc.baud(9600); //set baud rate
ewingswim 0:97e8d0a2752d 31 float PWMbrightness = 1.0; //float specifying brightness of LED (between 0.0 and 1.0)
ewingswim 0:97e8d0a2752d 32
ewingswim 0:97e8d0a2752d 33 while(1) {
ewingswim 0:97e8d0a2752d 34
ewingswim 0:97e8d0a2752d 35 left.speed(0.1);
ewingswim 0:97e8d0a2752d 36 right.speed(-0.105); //right negative to go forward
ewingswim 0:97e8d0a2752d 37
ewingswim 1:99d1aa030525 38
ewingswim 0:97e8d0a2752d 39 LB = PWMbrightness; //set brightness of sensor LED
ewingswim 0:97e8d0a2752d 40 rgb_sensor.getAllColors(rgb_data);
ewingswim 0:97e8d0a2752d 41
ewingswim 1:99d1aa030525 42 if(rgb_data[0] > 1000 && rgb_data[0]<5000){
ewingswim 1:99d1aa030525 43 left.speed(0.3);
ewingswim 1:99d1aa030525 44 right.speed(-0.305);
ewingswim 1:99d1aa030525 45 }
ewingswim 1:99d1aa030525 46 if (rgb_data[0]>5000)
ewingswim 1:99d1aa030525 47 { left.speed(0.35);
ewingswim 1:99d1aa030525 48 right.speed(-0.3);
ewingswim 1:99d1aa030525 49 }
ewingswim 1:99d1aa030525 50 if(rgb_data[0] < 1000)
ewingswim 1:99d1aa030525 51 { left.speed(0.25);
ewingswim 1:99d1aa030525 52 right.speed(-0.35);
ewingswim 1:99d1aa030525 53 }
ewingswim 1:99d1aa030525 54
ewingswim 1:99d1aa030525 55
ewingswim 1:99d1aa030525 56
ewingswim 1:99d1aa030525 57
ewingswim 1:99d1aa030525 58
ewingswim 0:97e8d0a2752d 59 if ((rgb_data[0] > 10000))
ewingswim 0:97e8d0a2752d 60 {
ewingswim 0:97e8d0a2752d 61 blue = 0;
ewingswim 0:97e8d0a2752d 62 green = 0;
ewingswim 0:97e8d0a2752d 63 red = 0;
ewingswim 0:97e8d0a2752d 64 }
ewingswim 0:97e8d0a2752d 65
ewingswim 0:97e8d0a2752d 66 else if ((rgb_data[1] > rgb_data[2]) && (rgb_data[1] > rgb_data[3]) )
ewingswim 0:97e8d0a2752d 67 {
ewingswim 0:97e8d0a2752d 68 red = 1;
ewingswim 0:97e8d0a2752d 69 green = 0;
ewingswim 0:97e8d0a2752d 70 blue = 0;
ewingswim 0:97e8d0a2752d 71 }
ewingswim 0:97e8d0a2752d 72 else if ((rgb_data[2] > rgb_data[1]) && (rgb_data[2] > rgb_data[3]) )
ewingswim 0:97e8d0a2752d 73 {
ewingswim 0:97e8d0a2752d 74 if(rgb_data[2] > 1100)
ewingswim 0:97e8d0a2752d 75 {
ewingswim 0:97e8d0a2752d 76 green = 1;
ewingswim 0:97e8d0a2752d 77 red = 0;
ewingswim 0:97e8d0a2752d 78 blue = 0;
ewingswim 0:97e8d0a2752d 79 }
ewingswim 0:97e8d0a2752d 80 else
ewingswim 0:97e8d0a2752d 81 {
ewingswim 0:97e8d0a2752d 82 green = 0;
ewingswim 0:97e8d0a2752d 83 red = 0;
ewingswim 0:97e8d0a2752d 84 blue = 0;
ewingswim 0:97e8d0a2752d 85 }
ewingswim 0:97e8d0a2752d 86 }
ewingswim 0:97e8d0a2752d 87 else if ((rgb_data[3] > rgb_data[2]) && (rgb_data[3] > rgb_data[1]) )
ewingswim 0:97e8d0a2752d 88 {
ewingswim 0:97e8d0a2752d 89 blue = 1;
ewingswim 0:97e8d0a2752d 90 green = 0;
ewingswim 0:97e8d0a2752d 91 red = 0;
ewingswim 0:97e8d0a2752d 92 }
ewingswim 0:97e8d0a2752d 93
ewingswim 0:97e8d0a2752d 94 pc.printf("unfiltered: %d, red: %d, green: %d, blue: %d \n", rgb_data[0], rgb_data[1], rgb_data[2], rgb_data[3]);
ewingswim 0:97e8d0a2752d 95
ewingswim 0:97e8d0a2752d 96 } //while
ewingswim 0:97e8d0a2752d 97 } //main