Color change timer 2

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 :cry:

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!!

you just need a counter.

void loop(void) {

  static uint_8 counter = 0; 
  /* this is good for 0...255, for more use: uint_16
      static: do not forget the value for next run
  */

  if( digitalRead(sensor_pin) == 1 ) { 
    counter++; /* do your stuff */
  }
  else { /* do other stuff */ }

  if ( counter > 10 ) { /* blink red */ }
  else { /* blink blue */ }

}

Thanks, madworm!! :slight_smile:

Now I understand that a counter is required for it. but I don't understand "where I can add your code into my sketch"... Sorry, I'm new to programming... :cry:

  1. How come there is "(void)" after "void loop"?
  2. What is 'uint_8" and "uint_16" ?
  3. I don't know "what kind of code I should put in the part of "* do your stuff /" and " / do other stuff */ "?

Could someone combine my code and madworm's code?
I really really appreciate your help!!

I just tried to add codes in /* blink red / and / blink blue */ parts. It may be wrong...

void loop(void) { 

  static uint_8 counter = 0;
  /* this is good for 0...255, for more use: uint_16
      static: do not forget the value for next run
  */

  if( digitalRead(sensor_pin) == 1 ) {
    counter++; /* do your stuff */   
  }
  else { /* do other stuff */ } 

  if ( counter > 10 ) { 
    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      
  }
}
  else { 
  //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      
  } 
  }

}

uint_8 == unsigned integer with length of 8 bits.
You can store numbers from 0 to 255 in it. It is the same as a byte. uint_16 is 2 bytes long. It takes twice the memory, but it also stores bigger numbers (0 to 65536).

You can replace the (void) with () in this case. Usually in C one needs to define each function once (prototype), before actually writing the code. The arduino software does that for you. Unfortunately I keep mixing the two things. So normally you'd have to define the function name, return type (what goes out) and input type (what type of data goes in). The (void) after loop indicates that it does not take any data as input. Normally this would go into the prototype section.

Detailed info: http://www.le.ac.uk/cc/tutorials/c/

I just meant to show you how you can do basic control structures. You already know which parts of your code work right ? You cannot break things by playing with the code a bit, so be brave and see what happens. Try a simple example first to see how a counter works, then add complexity step by step.

Somehow I found a solution for it.

thanks for your reply :wink: