Blink LED several times and stop

Hi,
I’m working on a new project and i got stuck in a really dummy thing.

i have 3 push buttons that are working as switches (push on push off):
left button, right button , ldr button

and two leds:
left led, right led

i managed to get the left button & right leds to fade while the buttons state is HIGH and to turn off the led while they are LOW
what i’m trying to do and i have no idea how is to blink both leds several times when i change the state of the ldr button:

when ldr button state changed to HIGH → both LEDS blinks 4 times and turn off LEDS
when ldr button state changed to LOW → both LEDS blinks 2 times and turn off LEDS

i know it’s a dummy question but i searched for a solution and tried many things and i’m still stuck…
can you help me implement this thing ?

Many thanks !

here’s the code:

#include <Servo.h> 
#include <CapacitiveSensorDue.h>

Servo myservo;  // create servo object to control a servo 

int angle = myservo.read();

int LDRSwitch =3;         // the number of the input pin
int RightSwitch =0;         // the number of the input pin
int LeftSwitch =0;         // the number of the input pin

int Reed1 = 1;
int Reed2 = 2;

int Reed1Read;
int Reed2Read;

int motorLeft = 11;       // the number of the output pin
int motorRight= 12;       // the number of the output pin

CapacitiveSensorDue cs_7_8 = CapacitiveSensorDue(7,8);	// 10M resistor between pins 4 & 2, pin 2 is sensor pin, add a wire and or foil if desired
CapacitiveSensorDue cs_10_9 = CapacitiveSensorDue(10,9);	// 10M resistor between pins 4 & 2, pin 2 is sensor pin, add a wire and or foil if desired

int LDR = A5;
int stateLeft = LOW;      // the current stateLeftLeft of the output pin
int stateLDR = LOW;      // the current stateLeftLeft of the output pin
int stateRight = LOW;      // the current stateLeftLeft of the output pin
int readingLeft;           // the current reading from the input pin
int readingRight;           // the current reading from the input pin
int readingLDR;           // the current reading from the input pin
int previousLeft = HIGH;    // the previous reading from the input pin
int previousRight = HIGH;    // the previous reading from the input pin
int previousLDR = HIGH;    // the previous reading from the input pin
int led1 = 5;           // the pin that the LED is attached to
int led2 = 6;           // the pin that the LED is attached to

// will quickly become a bigger number than can be stored in an int.
long time = 0;         // the last time the output pin was toggled
long debounce = 10;   // the debounce time, increase if the output flickers

void setup()
{
  pinMode(LeftSwitch, INPUT);
  pinMode(RightSwitch, INPUT);
  pinMode(LDRSwitch, INPUT);  
  pinMode(Reed1, INPUT);
  pinMode(Reed2, INPUT);
  pinMode(motorLeft, OUTPUT);
  pinMode(motorRight, OUTPUT);
  pinMode(LDR, INPUT);
  pinMode(led1, OUTPUT);
    pinMode(led2, OUTPUT);

  myservo.attach(13);  // attaches the servo on pin 9 to the servo object 

}

void loop()
{
  long totalLeft = cs_10_9.read(10);
  long totalRight = cs_7_8.read(10);

  {
    //readingLeft = digitalRead(LeftSwitch);
    readingLeft = totalLeft;
    Reed1Read= digitalRead(Reed1);
    readingRight = digitalRead(RightSwitch);
    readingRight = totalRight;
    Reed2Read= digitalRead(Reed2);
    readingLDR = digitalRead(LDRSwitch);

    // if the input just went from LOW and HIGH and we've waited long enough
    // to ignore any noise on the circuit, toggle the output pin and remember
    // the time

    if (readingLeft>100 && previousLeft <100 && millis() - time > debounce) {
      if (stateLeft == HIGH)
        stateLeft = LOW;
      else
        stateLeft = HIGH;
      time = millis();    
    }

    // if the input just went from LOW and HIGH and we've waited long enough
    // to ignore any noise on the circuit, toggle the output pin and remember
    // the time
    if (readingLDR == HIGH && previousLDR == LOW && millis() - time > debounce) {
      if (stateLDR == HIGH)
        stateLDR = LOW;
      else
        stateLDR = HIGH;
      time = millis();    
    }

    if (readingRight >100 && previousRight <100 && millis() - time > debounce) {
      if (stateRight == HIGH)
        stateRight = LOW;
        
      else
        stateRight = HIGH;
      time = millis();    

    }  
    if (stateLeft == HIGH || stateRight == HIGH ){
      myservo.write(180);                // sets the servo position according to the scaled value 

    }
    if (stateLeft == LOW &&stateRight == LOW){
      myservo.write(90);                // sets the servo position according to the scaled value 
    }

        if (stateLeft == LOW ){
      analogWrite(led1, 0);
    }  
            if (stateRight == LOW ){
      analogWrite(led2, 0);
    }  
    
    if (stateLeft == HIGH ){
      float val = (exp(sin(millis()/1000.0*PI)) - 0.36787944)*108.0;
      analogWrite(led1, val);
    }

    if (stateRight == HIGH ){
      float val = (exp(sin(millis()/1000.0*PI)) - 0.36787944)*108.0;
      analogWrite(led2, val);
    }

    digitalWrite(motorLeft, stateLeft);
    digitalWrite(motorRight, stateRight);
    
    previousLeft = readingLeft;
    previousRight = readingRight;
    previousLDR = readingLDR;
  }
  if  (analogRead(LDR)<150&&stateLDR == HIGH)
  {
    stateRight = HIGH;
    }
    if  (Reed2Read==HIGH)
    {
      stateRight = LOW;
      stateLDR = LOW;
    }
    {
      if  (Reed1Read==HIGH)
      {
        stateLeft = LOW;
      }
    }
  }

To blink N times:

Start with the BlinkWithoutDelay example.
Modify it to blink both lights.
Add a global variable to act as a blink counter and initialize it to 0.
In the part where it is time to turn on the LEDs, only do so if the blink counter > 0
Decrement the blink counter when you turn on the LEDs
Put this code in the top of loop().

In your button handling code:
when the ldr button changes from LOW to HIGH set the blink counter to 4
when the ldr button changes from HIGH to LOW set the blink counter to 2

The demo several things at a time flashes 3 LEDs. You could extend it to do many more. It uses the BWoD technique.

...R

ok, so after johnwasser idea (thanks johnwasser), i managed to merge the “Switch” example http://www.arduino.cc/en/tutorial/switch
and the “Blink Without Delay” example http://arduino.cc/en/Tutorial/BlinkWithoutDelay

and i’m stuck again, i managed to get the led blink 4 times when the button state changes to HIGH, but i couldn’t figure out how to also blink the led 2 time when the button state changes to LOW (it’s currently do nothing…) :~

here’s the code…

any ideas ?

many thanks !

Maayan

int inPin = 3;         // the number of the input pin
int outPin = 5;       // the number of the output pin
int counter = 0;
int state = HIGH;      // the current state of the output pin
int reading;           // the current reading from the input pin
int previous = LOW;    // the previous reading from the input pin

long previousMillis = 0;        // will store last time LED was updated
long interval = 200;           // interval at which to blink (milliseconds)
int ledState = LOW;             // ledState used to set the LED

// the follow variables are long's because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
long time = 0;         // the last time the output pin was toggled
long debounce = 200;   // the debounce time, increase if the output flickers

void setup()
{
  pinMode(inPin, INPUT);
  pinMode(outPin, OUTPUT);

  Serial.begin(9600);
}
void loop()
{
  unsigned long currentMillis = millis();

  if(currentMillis - previousMillis > interval) {
    // save the last time you blinked the LED 
    previousMillis = currentMillis;   

    // if the LED is off turn it on and vice-versa:
    if (ledState == LOW)
      ledState = HIGH;
    else
      ledState = LOW;

    // set the LED with the ledState of the variable:
    counter--;
    Serial.println(counter);

  }

  {
    reading = digitalRead(inPin);

    // if the input just went from LOW and HIGH and we've waited long enough
    // to ignore any noise on the circuit, toggle the output pin and remember
    // the time
    if (reading == HIGH && previous == LOW && millis() - time > debounce) {
      if (state == HIGH){
        state = LOW;
        counter=8;
      }
      else
        state = HIGH;
      time = millis();    
    }
  }
  previous = reading;

  if (counter > 0){
    digitalWrite(outPin, ledState);
  }
  else
    digitalWrite(outPin, LOW);
}

I've had a quick look at your code but I can't make sense of it.

Think of the problem like this

void loop() {
   switchVal = readSwitch();

   if (switchVal == HIGH ) {
        blinkLed(8);
   }
   else {
       blinkLed(2);
   }
}
 
byte readSwitch() {
   // stuff to detect a switch including debouncing
   return swVal;
}

void blinkLed(byte numBlinks) {
    // code to cause numBlinks
}

By separating the logic from the actions it is much easier to see what is happening.

...R