Double Blink and delay for 1000 MS without delay()

why I wanted to know BlinkwithoutDelay is for my below mentioned project. The delay() is halting the system and didnot allow the button to respond.. But still I canot figured out how to solve it... Very disappointing.. Here is the code:

const int rightLed=12;
const int leftLed=11;
const int leftButton=8;
const int rightButton=9;
const int blinkerButton=10;
uint8_t brightness = 0;    // how bright the LED is
uint8_t fadeAmount = 5;
byte counter = 0;
byte OldCounter = 0;


bool leftButtonState = 1;
bool rightButtonState = 1;
bool blinkerButtonState = 1;


void setup()
{
  pinMode(leftLed,OUTPUT);
  pinMode(rightLed,OUTPUT);
  pinMode(leftButton,INPUT_PULLUP);
  pinMode(rightButton,INPUT_PULLUP);
  pinMode(blinkerButton,INPUT_PULLUP);
}




void loop()
{
  OldCounter = counter;
  switch (OldCounter)
  {
    case 1: Flasher1(); break;
    case 2: Flasher2(); break;
    case 3: Flasher3(); break;
    case 4: Flasher4(); break;
    case 5: Flasher5(); break;
    case 6: Flasher6(); break;
    case 7: Flasher7(); break;
    case 8: Flasher8(); break;
    case 9: Flasher9(); break;
    case 10: Flasher10(); break;
    case 11: Flasher11(); break;
    case 12: LeftIndicator(); break;
    case 13: RightIndicator(); break;
    default: Flasher2();
  }
  //for leftbutton
  leftButtonState=digitalRead(leftButton);
  if (leftButtonState == 0)
  {
   
    counter = 12;
  }


  //for right button
  rightButtonState=digitalRead(rightButton);
  if (rightButtonState== 0)
  {
  
    counter = 13;
  }

  //for blinker button
  blinkerButtonState=digitalRead(blinkerButton);
  if (blinkerButtonState == 0) {
    {
      counter++;
      if (counter > 11)counter = 1;
    }
  }
}


void Flasher1() {
  analogWrite(PB0, brightness);
  analogWrite(PB1, brightness);

  // change the brightness for next time through the loop:
  brightness = brightness + fadeAmount;

  // reverse the direction of the fading at the ends of the fade:
  if (brightness <= 0 || brightness >= 255) {

    fadeAmount = -fadeAmount;
    if (brightness == 0)   delay(2000) ;
  }
  // wait for 30 milliseconds to see the dimming effect
  delay(50) ;
}
void Flasher2()
{
  digitalWrite(leftLed, HIGH);  digitalWrite(rightLed, HIGH);     delay(500) ;
  digitalWrite(leftLed, LOW); digitalWrite(rightLed, LOW);    delay(500) ;
}
void Flasher3()
{
  LeftLedFlash();   LeftLedFlash();   LeftLedFlash();      LeftLedFlash();    delay(200) ;
  RightLedFlash();  RightLedFlash();  RightLedFlash();     RightLedFlash();   delay(200) ;
}

void Flasher4()
{
  LeftLedFlash();  LeftLedFlash();    delay(1000) ;
  RightLedFlash(); RightLedFlash();   delay(1000) ;
}
void Flasher5()
{
  LeftLedFlash();     delay(80);
  LeftLedFlash();     delay(1000) ;
  RightLedFlash();  delay(80) ;
  RightLedFlash();  delay(1000) ;
}

void Flasher6()
{
  LeftLedFlash();      RightLedFlash();      delay(60) ;
  LeftLedFlash();      RightLedFlash();     delay(500) ;
}
void Flasher7()
{
  digitalWrite(rightLed, HIGH);  digitalWrite(leftLed, HIGH);  delay(50) ;
  digitalWrite(rightLed, LOW); digitalWrite(leftLed, LOW); delay(50) ;
  digitalWrite(rightLed, HIGH);  digitalWrite(leftLed, HIGH);  delay(50) ;
  digitalWrite(rightLed, LOW); digitalWrite(leftLed, LOW); delay(500) ;
}
void Flasher8()
{
  digitalWrite(rightLed, HIGH);      delay(50) ;
  digitalWrite(rightLed, LOW);     digitalWrite(leftLed, HIGH);      delay(100) ;
  digitalWrite(leftLed, LOW);      digitalWrite(rightLed, HIGH);     delay(50) ;
  digitalWrite(rightLed, LOW);     digitalWrite(leftLed, HIGH);      delay(100) ;
  digitalWrite(leftLed, LOW);      digitalWrite(rightLed, HIGH);     delay(50) ;
  digitalWrite(rightLed, LOW);     delay(1500) ;
}
void Flasher9()
{
  for ( uint8_t i = 0; i < 3; i++)
  {
    digitalWrite(leftLed, HIGH);    delay(50) ;   digitalWrite(leftLed, LOW);    delay(50) ;
  }
  for (uint8_t i = 0; i < 3; i++)
  {
    digitalWrite(rightLed, HIGH);   delay(50) ;   digitalWrite(rightLed, LOW);    delay(50) ;
  }
}
void Flasher10()
{
  digitalWrite(leftLed, HIGH);    delay(50) ;    digitalWrite(leftLed, LOW);
  digitalWrite(rightLed, HIGH);   delay(50) ;    digitalWrite(rightLed, LOW);
  delay(2000) ;
}

void Flasher11()
{
  digitalWrite(leftLed, HIGH); digitalWrite(rightLed, HIGH);   delay(50) ;  digitalWrite(leftLed, LOW);  digitalWrite(rightLed, LOW);
  delay(3000) ;
}
void LeftLedFlash()
{
  digitalWrite(leftLed, HIGH);  delay(30) ;  digitalWrite(leftLed, LOW);  delay(30) ;
}
void RightLedFlash()
{
  digitalWrite(rightLed, HIGH);  delay(30) ;
  digitalWrite(rightLed, LOW); delay(30) ;
}
void LeftIndicator()
{
  digitalWrite(rightLed, LOW);
  digitalWrite(leftLed, HIGH);  delay(500) ;
  digitalWrite(leftLed, LOW); delay(500) ;
}
void RightIndicator()
{
  digitalWrite(leftLed, LOW);
  digitalWrite(rightLed, HIGH);  delay(500) ;
  digitalWrite(rightLed, LOW); delay(500) ;
}

Ok, this is suddenly much more complicated. But it is not impossible.

Question: if we help you get this working, what next? More LEDs, more patterns, or is this everything?

I am thinking of a "player piano" approach to solve this, with a section of the "roll" for each pattern, and that section gets repeated until the "track" or "mode" changes.

... and changes the underlying nature of the project. It might deserve to be spun off into a new thread.

Yes this much of my project. I need it.
Cant understand your example of Player Pinno.
can u pls make a short programe for it..
pls

I have converted Flasher2() to Flasher5() using my "player piano" idea. It compiles but I have not tested it, so if there are any problems, please be sure to explain them fully.

const int rightLed=12;
const int leftLed=11;
const int leftButton=8;
const int rightButton=9;
const int blinkerButton=10;
uint8_t brightness = 0;    // how bright the LED is
uint8_t fadeAmount = 5;
byte counter = 0;
byte OldCounter = 0;

struct {
  byte leftLed;
  byte rightLed;
  int period;
} sequence[] = {
  /* Flasher 2 */
  {HIGH, HIGH, 500},
  {LOW,  LOW,  500},
  /* Flasher 3 */
  {HIGH, LOW,   30},
  {LOW,  LOW,   30},
  {HIGH, LOW,   30},
  {LOW,  LOW,   30},
  {HIGH, LOW,   30},
  {LOW,  LOW,   30},
  {HIGH, LOW,   30},
  {LOW,  LOW,   30+200},
  {LOW, HIGH,   30},
  {LOW,  LOW,   30},
  {LOW, HIGH,   30},
  {LOW,  LOW,   30},
  {LOW, HIGH,   30},
  {LOW,  LOW,   30},
  {LOW, HIGH,   30},
  {LOW,  LOW,   30+200},
  /* Flasher 4 */
  {HIGH, LOW,   30},
  {LOW,  LOW,   30},
  {HIGH, LOW,   30},
  {LOW,  LOW,   30+1000},
  {LOW, HIGH,   30},
  {LOW,  LOW,   30},
  {LOW, HIGH,   30},
  {LOW,  LOW,   30+1000},
  /* Flasher 5 */
  {HIGH, LOW,   30},
  {LOW,  LOW,   30+80},
  {HIGH, LOW,   30},
  {LOW,  LOW,   30+1000},
  {LOW, HIGH,   30},
  {LOW,  LOW,   30+80},
  {LOW, HIGH,   30},
  {LOW,  LOW,   30+1000}
};

int step;
unsigned long lastChange;

void Flasher(int first, int last) {
  if (step < first || step > last || lastChange - millis() >= sequence[step].period) {
    /* move to next step */
    step++;
    if (step < first || step > last) step = first;
    /* Update Leds */
    digitalWrite(leftLed, sequence[step].leftLed);
    digitalWrite(rightLed, sequence[step].rightLed);
    lastChange = millis();
  }
}


bool leftButtonState = 1;
bool rightButtonState = 1;
bool blinkerButtonState = 1;


void setup()
{
  pinMode(leftLed,OUTPUT);
  pinMode(rightLed,OUTPUT);
  pinMode(leftButton,INPUT_PULLUP);
  pinMode(rightButton,INPUT_PULLUP);
  pinMode(blinkerButton,INPUT_PULLUP);
}




void loop()
{
  OldCounter = counter;
  switch (OldCounter)
  {
    case 1: Flasher1(); break;
    case 2: Flasher(0, 1); break;
    case 3: Flasher(2, 19); break;
    case 4: Flasher(20, 29); break;
    case 5: Flasher(30, 37); break;
    case 6: Flasher6(); break;
    case 7: Flasher7(); break;
    case 8: Flasher8(); break;
    case 9: Flasher9(); break;
    case 10: Flasher10(); break;
    case 11: Flasher11(); break;
    case 12: LeftIndicator(); break;
    case 13: RightIndicator(); break;
    default: Flasher(0, 1);
  }
  //for leftbutton
  leftButtonState=digitalRead(leftButton);
  if (leftButtonState == 0)
  {
   
    counter = 12;
  }


  //for right button
  rightButtonState=digitalRead(rightButton);
  if (rightButtonState== 0)
  {
  
    counter = 13;
  }

  //for blinker button
  blinkerButtonState=digitalRead(blinkerButton);
  if (blinkerButtonState == 0) {
    {
      counter++;
      if (counter > 11)counter = 1;
    }
  }
}


void Flasher1() {
  analogWrite(PB0, brightness);
  analogWrite(PB1, brightness);

  // change the brightness for next time through the loop:
  brightness = brightness + fadeAmount;

  // reverse the direction of the fading at the ends of the fade:
  if (brightness <= 0 || brightness >= 255) {

    fadeAmount = -fadeAmount;
    if (brightness == 0)   delay(2000) ;
  }
  // wait for 30 milliseconds to see the dimming effect
  delay(50) ;
}

void Flasher6()
{
  LeftLedFlash();      RightLedFlash();      delay(60) ;
  LeftLedFlash();      RightLedFlash();     delay(500) ;
}
void Flasher7()
{
  digitalWrite(rightLed, HIGH);  digitalWrite(leftLed, HIGH);  delay(50) ;
  digitalWrite(rightLed, LOW); digitalWrite(leftLed, LOW); delay(50) ;
  digitalWrite(rightLed, HIGH);  digitalWrite(leftLed, HIGH);  delay(50) ;
  digitalWrite(rightLed, LOW); digitalWrite(leftLed, LOW); delay(500) ;
}
void Flasher8()
{
  digitalWrite(rightLed, HIGH);      delay(50) ;
  digitalWrite(rightLed, LOW);     digitalWrite(leftLed, HIGH);      delay(100) ;
  digitalWrite(leftLed, LOW);      digitalWrite(rightLed, HIGH);     delay(50) ;
  digitalWrite(rightLed, LOW);     digitalWrite(leftLed, HIGH);      delay(100) ;
  digitalWrite(leftLed, LOW);      digitalWrite(rightLed, HIGH);     delay(50) ;
  digitalWrite(rightLed, LOW);     delay(1500) ;
}
void Flasher9()
{
  for ( uint8_t i = 0; i < 3; i++)
  {
    digitalWrite(leftLed, HIGH);    delay(50) ;   digitalWrite(leftLed, LOW);    delay(50) ;
  }
  for (uint8_t i = 0; i < 3; i++)
  {
    digitalWrite(rightLed, HIGH);   delay(50) ;   digitalWrite(rightLed, LOW);    delay(50) ;
  }
}
void Flasher10()
{
  digitalWrite(leftLed, HIGH);    delay(50) ;    digitalWrite(leftLed, LOW);
  digitalWrite(rightLed, HIGH);   delay(50) ;    digitalWrite(rightLed, LOW);
  delay(2000) ;
}

void Flasher11()
{
  digitalWrite(leftLed, HIGH); digitalWrite(rightLed, HIGH);   delay(50) ;  digitalWrite(leftLed, LOW);  digitalWrite(rightLed, LOW);
  delay(3000) ;
}
void LeftLedFlash()
{
  digitalWrite(leftLed, HIGH);  delay(30) ;  digitalWrite(leftLed, LOW);  delay(30) ;
}
void RightLedFlash()
{
  digitalWrite(rightLed, HIGH);  delay(30) ;
  digitalWrite(rightLed, LOW); delay(30) ;
}
void LeftIndicator()
{
  digitalWrite(rightLed, LOW);
  digitalWrite(leftLed, HIGH);  delay(500) ;
  digitalWrite(leftLed, LOW); delay(500) ;
}
void RightIndicator()
{
  digitalWrite(leftLed, LOW);
  digitalWrite(rightLed, HIGH);  delay(500) ;
  digitalWrite(rightLed, LOW); delay(500) ;
}

You should be able to convert the other functions in the same way. However, Flasher1() is different to the others and does not fit with my "player piano" idea, so that must be converted using the typical "blink without delay" technique.

1 Like

Sure Sir, I will check today evening and tell u the outcome..
Thanks for the effort you made