Hi All,
Complete beginner out of his dept and wasting allot of time on one particular issue.
Okay I'm using a sensor to read colors blue, green and red. Once the sensor reads red for example I need a red LED to delay 2 seconds come on for a second and go off again. When the sensor reads blue I want a delay of 4 seconds and with green a delay of 6 seconds to the LEDs. Using delays in the code is slowing the colour sensor, and does not allow my circuit to multitask.
I want to be able to take readings very second, so if the sensor read green (6 second delay needed) then red(2 second delay needed), my code will activate the red LED before the green LED. I think I need a timer here using the millis function but I have tried and made very little progress so far, I need a push in the right direction! Would be extremely appreciative of some input here!!
This is the code I'm using so far, which is not what I what but might help someone understand my problem and what I'm trying to do.
// TCS230 connections:
const int outputEnabled = 2; // write LOW to turn on Note, may not be hooked up.
const int s0 = 3; // sensor pins
const int s1 = 4;
const int s2 = 5;
const int s3 = 6;
const int nLED = 7; // illuminating LED
const int out = 8; // TCS230 output
const int LED = 10;
const int LED2 = 11;
const int LED3 = 12;
// variables to store color values
int red = 0;
int green = 0;
int blue = 0;
void setup() {
pinMode(outputEnabled, OUTPUT);
pinMode(s0, OUTPUT);
pinMode(s1, OUTPUT);
pinMode(s2, OUTPUT);
pinMode(s3, OUTPUT);
pinMode(nLED, OUTPUT);
pinMode(LED, OUTPUT);
pinMode(LED2, OUTPUT);
pinMode(LED3,OUTPUT);
pinMode(out, INPUT);
Serial.begin(9600);
//This pin may be set to ground and not available on the breakout
//If not available don't worry about it.
digitalWrite(outputEnabled, LOW);
//Set Frequency scaling to largest value
digitalWrite(s0, HIGH);
digitalWrite(s1, HIGH);
digitalWrite(nLED, LOW);
}
void loop() {
color();
Serial.print("R");
Serial.print(red, DEC);
Serial.print(" G");
Serial.print(green, DEC);
Serial.print(" B");
Serial.print(blue, DEC);
Serial.println();
//Simple logic to test for color
if (red < blue && red < green ) { // RED
delay(2000); // wait for two second
digitalWrite(LED, HIGH); // set the LED on
delay(1000);
digitalWrite(LED, LOW);
}
else { digitalWrite (LED, LOW);}
if (blue < red && blue < green ) { // BLUE
delay(4000); // wait for four seconds
digitalWrite(LED2, HIGH); // set the LED on
delay(1000);
digitalWrite(LED2, LOW);
}
else { digitalWrite (LED2, LOW);}
if (green < red && green < blue ) { // GREEN
delay(6000); // wait for six seconds
digitalWrite(LED3, HIGH); // set the LED on
delay(1000);
digitalWrite(LED3, LOW);
}
else { digitalWrite (LED3, LOW);}
}
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);
}
Moderator edit:
</mark> <mark>[code]</mark> <mark>
</mark> <mark>[/code]</mark> <mark>
tags added.