Just looking for some advice on coding. I am making a version of a color sorter that I have seen online using skittles. I want to add in a calibration for the colors when i first start the machine. Right not I am trying to use serial print functions in order to call upon the calibration for each individual color. What i am trying to do is when the user input is typed in as one, it then reads the value of the color sensor r, g, b, and then stores that into an integer or variable. Not sure if this can be done as simply as i am thinking of it. The color sensor uses SDA and SCL pins to transfer that data. The color sensor I am using is Adafruits TCS34725. The code is as shown below. Its a bit messy write now with many lines of code that may not be needed. This is from me trying to trouble shoot myself the past couple of hours. Ill appreciate any help. Currently only trying to make it work with the red color.
#include <Wire.h>
#include <Adafruit_TCS34725.h>
//Declare pin functions on Redboard
#define stp 2
#define dir 3
#define EN 6
#define MS1 4
#define MS2 5
#define EN2 12
#define LedRed 22
#define LedBlue 24
#define LedGreen 26
#define LedYellow 28
#define LedPurple 30
#define redpin 23
#define greenpin 25
#define bluepin 27
Adafruit_TCS34725 tcs = Adafruit_TCS34725(TCS34725_INTEGRATIONTIME_50MS, TCS34725_GAIN_4X);
int lastPosition = 0;
int newPosition = 0;
//Declare variables for functions
char user_input;
int x;
int y;
int state;
int RedCup;
void setup() {
Serial.begin(9600);
Serial.println("Color View Test!");
if (tcs.begin()) {
Serial.println("Found sensor");
} else {
Serial.println("No TCS34725 found ... check your connections");
while (1); // halt!
}
Serial.println("Insert Colors for Calibration Process");
pinMode(stp, OUTPUT);
pinMode(dir, OUTPUT);
pinMode(EN, OUTPUT);
pinMode(MS1, OUTPUT);
pinMode(MS2, OUTPUT);
pinMode(EN2, OUTPUT);
pinMode(LedRed, OUTPUT);
pinMode(LedBlue, OUTPUT);
pinMode(LedGreen, OUTPUT);
pinMode(LedYellow, OUTPUT);
pinMode(LedPurple, OUTPUT);
pinMode(redpin, OUTPUT);
pinMode(greenpin, OUTPUT);
pinMode(bluepin, OUTPUT);
lastPosition = 1;
newPosition = 0;
}
//Main loop
void loop() {
user_input = Serial.read(); //Read user input and trigger appropriate function
Serial.println("Enter number for control option:");
Serial.println("1. RED CUP");
Serial.println("2. BLUE CUP");
Serial.println("3. YELLOW CUP");
Serial.println("4. GREEN CUP");
Serial.println("5. PURPLE CUP");
Serial.println();
if(user_input =='1'){
uint16_t clearcol, red, green, blue ;
float average , r, g, b ;
delay ( 500 ) ; // Color measurement lasts c. 50ms
tcs. getRawData ( & red, & green, & blue, & clearcol ) ;
average = (red+green+blue)/3;
// color values by average,
// all values are now around 1
r = red/average ;
g = green/average ;
b = blue/average ;
Serial.print("\tClear:"); Serial.print(clearcol);
Serial.print("\tRed:"); Serial.print(r);
Serial.print("\tGreen:"); Serial.print(g);
Serial.print("\tBlue:"); Serial.print(b);
}
else if(user_input =='2')newPosition = 2;
else if(user_input =='3')newPosition = 3;
else if(user_input =='4')newPosition = 4;
else if(user_input =='5')newPosition = 5;
uint16_t clearcol, red, green, blue ;
float average , r, g, b ;
delay ( 100 ) ; // Color measurement lasts c. 50ms
tcs. getRawData ( & red, & green, & blue, & clearcol ) ;
// My attempt to determine color for the 5 M & M colors red, green, blue, orange and yellow
// Calculate
average = (red+green+blue)/3;
// color values by average,
// all values are now around 1
r = red/average ;
g = green/average ;
b = blue/average ;
// Clear value and r, g, b serial output to control
// r, g and b should move between 0.5 and 1.5
//. If the sensor looks red, then r should be well over 1.0
//, g and b between 0.5 and 1.0 etc.
/*
Serial.print("\tClear:"); Serial.print(clearcol);
Serial.print("\tRed:"); Serial.print(r);
Serial.print("\tGreen:"); Serial.print(g);
Serial.print("\tBlue:"); Serial.print(b);
*/
//Serial.print((int)r ); Serial.print(" "); Serial.print((int)g);Serial.print(" "); Serial.println((int)b );
analogWrite(redpin, red);
analogWrite(greenpin, green);
analogWrite(bluepin, blue);
digitalWrite(EN, LOW); //Pull enable pin low to allow motor control
if (RedCup)newPosition = 1;// color red
else if(b>1.5)newPosition = 2;// color blue
else if(r>1.2 & r<1.3 & g> 1.1 & g < 1.17)newPosition = 3;// color yellow
else if(r>0.9 & g>1.2)newPosition = 4;// color green
else if(r>1.1 & b>1 )newPosition = 5;// color purple
i