Hello,
Im currently working with some color sensors, and i want them to run some commands when the right color is found.
A setup picture can be found here:
As seen on the photo the lego brick has the color i want to meassure.
The code:
const int s0 = 8;
const int s1 = 9;
const int s2 = 12;
const int s3 = 11;
const int out = 10;
int currentState = 1;
unsigned long t[10];
// Variables
int red = 0;
int green = 0;
int blue = 0;
void setup()
{
Serial.begin(9600);
pinMode(s0, OUTPUT);
pinMode(s1, OUTPUT);
pinMode(s2, OUTPUT);
pinMode(s3, OUTPUT);
pinMode(out, INPUT);
digitalWrite(s0, HIGH);
digitalWrite(s1, HIGH);
}
void loop()
{
if (currentState == 1){
color();
Serial.print(" Red Intensity: ");
Serial.print(red, DEC);
Serial.print(" | Green Intensity: ");
Serial.print(green, DEC);
Serial.print(" | Blue Intensity: ");
Serial.print(blue, DEC);
//Serial.println();
if (red < blue && red < green && green > 13 && blue > 10 && green < 20 && red < 10){
t[0]=millis();
t[1]=millis()-t[0];
Serial.println("");
Serial.println("");
Serial.println("");
Serial.println("");
Serial.println("");
Serial.println(" --------------------------------- (Red Color) --------------------------------- ");
Serial.println("");
Serial.println("");
Serial.println("");
Serial.println("");
Serial.println("");
currentState = 0;
}
if (t[1] == 5000){
currentState = 1;
}
else if (red < green && green > 14 && blue > 9 && red > 9)
{
Serial.println(" - (Blue Color)");
}
else if (red < blue && red < green && green < 13 && blue > 10)
{
Serial.println(" - (Yellow Color)");
}
else{
Serial.println();
}
}
}
void color()
{
digitalWrite(s2, LOW);
digitalWrite(s3, LOW);
//count OUT, pRed, RED
red = pulseIn(out, digitalRead(out) == HIGH ? LOW : HIGH);
digitalWrite(s3, HIGH);
//count OUT, pBLUE, BLUE
blue = pulseIn(out, digitalRead(out) == HIGH ? LOW : HIGH);
digitalWrite(s2, HIGH);
//count OUT, pGreen, GREEN
green = pulseIn(out, digitalRead(out) == HIGH ? LOW : HIGH);
}
The code on which i want to focus:
void loop()
{
if (currentState == 1){
color();
Serial.print(" Red Intensity: ");
Serial.print(red, DEC);
Serial.print(" | Green Intensity: ");
Serial.print(green, DEC);
Serial.print(" | Blue Intensity: ");
Serial.print(blue, DEC);
//Serial.println();
if (red < blue && red < green && green > 13 && blue > 10 && green < 20 && red < 10){
t[0]=millis();
t[1]=millis()-t[0];
Serial.println("");
Serial.println("");
Serial.println("");
Serial.println("");
Serial.println("");
Serial.println(" --------------------------------- (Red Color) --------------------------------- ");
Serial.println("");
Serial.println("");
Serial.println("");
Serial.println("");
Serial.println("");
currentState = 0;
}
if (t[1] == 5000){
currentState = 1;
}
else if (red < green && green > 14 && blue > 9 && red > 9)
{
Serial.println(" - (Blue Color)");
}
else if (red < blue && red < green && green < 13 && blue > 10)
{
Serial.println(" - (Yellow Color)");
}
else{
Serial.println();
}
}
}
I have so far only tried to add the commands for the box lego brick, and there is a problem.
When currentState is 1 the serial communication will continuously tell me the color values that the sensor is seeing, and then it senses a red lego brick it will tell me that it has been found, and 5 seconds later it should try to sense another brick.
How do i get around doing this? Ive tried with millis as it worked for me before in another project. I DO NOT WANT TO USE THE DELAY COMMAND.
An example code of the serial communication:
Red Intensity: 3 | Green Intensity: 6 | Blue Intensity: 4
Red Intensity: 3 | Green Intensity: 4 | Blue Intensity: 4
Red Intensity: 2 | Green Intensity: 4 | Blue Intensity: 4
Red Intensity: 2 | Green Intensity: 3 | Blue Intensity: 3
Red Intensity: 1 | Green Intensity: 2 | Blue Intensity: 2
Red Intensity: 1 | Green Intensity: 2 | Blue Intensity: 2
Red Intensity: 1 | Green Intensity: 2 | Blue Intensity: 2
Red Intensity: 2 | Green Intensity: 3 | Blue Intensity: 2
Red Intensity: 2 | Green Intensity: 2 | Blue Intensity: 3
Red Intensity: 2 | Green Intensity: 4 | Blue Intensity: 3
Red Intensity: 3 | Green Intensity: 4 | Blue Intensity: 4
Red Intensity: 6 | Green Intensity: 8 | Blue Intensity: 8
Red Intensity: 8 | Green Intensity: 15 | Blue Intensity: 14
--------------------------------- (Red Color) ---------------------------------
Thanks in advance.