hello,
im working color sensors, , want them run commands when right color found.
a setup picture can found here:
as seen on photo lego brick has color want meassure.
the code:
the code on want focus:
i have far tried add commands box lego brick, , there problem.
when currentstate 1 serial communication continuously tell me color values sensor seeing, , senses red lego brick tell me has been found, , 5 seconds later should try sense brick.
how around doing this? ive tried millis worked me before in project. not want use delay command.
an example code of serial communication:
thanks in advance.
im working color sensors, , want them run commands when right color found.
a setup picture can found here:
as seen on photo lego brick has color want meassure.
the code:
code: [select]
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 want focus:
code: [select]
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 far tried add commands box lego brick, , there problem.
when currentstate 1 serial communication continuously tell me color values sensor seeing, , senses red lego brick tell me has been found, , 5 seconds later should try sense brick.
how around doing this? ive tried millis worked me before in project. not want use delay command.
an example code of serial communication:
code: [select]
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.
that code not easy follow....
Arduino Forum > Using Arduino > Project Guidance > Color Sensor - Delay (millis)
arduino
Comments
Post a Comment