Hi, all
I have a question about this color change timer. :-/
I want to add another function in this color change timer code (see last).
The important thing is that I'm using a light sensor as a switch.
I really need someone's help
The question is;-
How I can count "how many times I covered the light sensor?"
and if the sensor is covered over 10 times (it means "cover the light sensor and uncover the light sensor" x 10),
I want to change the "RED led blink"part to "Blue led blink".
I want to add this sketch into the next code.
//blue led blink
while ( digitalRead(switchPin) == LOW){
digitalWrite(ledPinB,HIGH); // Blue LED on
delay(250); // 2.5 seconds delay
digitalWrite(ledPinB,LOW); // Blue LED off
delay(250);
if( digitalRead(switchPin) == HIGH)
setColor(0,0,0,0); // turn LEDs off
}
int ledPinG=10; //Green LED
int ledPinB=11; //Blue LED
int ledPinR=12; //Red LED
int switchPin=2; //Light sensor
int val;// Switch connected to digital pin 2
int value = LOW; // previous value of the LED
long previousMillis = 0; // will store last time LED was updated
long interval = 250; // interval at which to blink (milliseconds)
void setup() {
Serial.begin(9600); // set up Serial library at 9600 bps
pinMode(switchPin,INPUT); // Light sensor as a switch
pinMode(ledPinG,OUTPUT); // Green LED pin as input to read switch
pinMode(ledPinB,OUTPUT); // Blue LED pin as input to read switch
pinMode(ledPinR,OUTPUT); // Red LED pin as input to read switch
}
void loop() { // G, B, R, time(seconds)
if( digitalRead(switchPin) == LOW) // blue
setColor(1,1,0,2000);
if( digitalRead(switchPin) == LOW) // light blue
setColor(0,1,0,2000);
if( digitalRead(switchPin) == LOW) //green
setColor(1,0,0,2000);
if( digitalRead(switchPin) == LOW) //yellow
setColor(1,0,1,2000);
if( digitalRead(switchPin) == LOW) //purple
setColor(0,1,1,2000);
if( digitalRead(switchPin) == LOW) //red
setColor(0,0,1,2000);
// do your other colors ....
//RED led blink
while ( digitalRead(switchPin) == LOW){
digitalWrite(ledPinR,HIGH); // Red LED on
delay(250); // 2.5 seconds delay
digitalWrite(ledPinR,LOW); // Red LED off
delay(250);
if( digitalRead(switchPin) == HIGH)
setColor(0,0,0,0); // turn LEDs off
}
//turn off the switch
if( digitalRead(switchPin) == HIGH)
setColor(0,0,0,0); // turn LEDs off
}
// Switch can be turned off anytime you want.
void setColor(int R, int G, int B, int delayMs){
// set the LED colors and delay for the given number of milliseconds
// but return if sensor is covered
digitalWrite(ledPinG, R);
digitalWrite(ledPinB, G);
digitalWrite(ledPinR, B);
while(delayMs--){
// this loop checks the sensor every millisecond
delay(1);
if(digitalRead(switchPin) == HIGH)
return; // exit the while loop if sensor is covered
}
}
Please tell me, how I can complete this idea with these sketches.
Thanks heaps and heaps!!