Overcome Delay problem

Hi I want to create a project for my bike with ATTINY13 microcontroller. I have two leds for LEFT indicator and RIGHT indicator. and Three Button for Left_indicator, Right_indicator and Flasher_effect.

As soon as any button change, the corresponding led effect should blink. But whenever a large delay type flasher running, the button execution got halt. I had to press more time to see the effect.

I need to change the button function as soon as the button is press...
pls help me out.

attaching the schematic and code for your reference.

#define leftLedOn PORTB |= (1 << PB0)
#define leftLedOff PORTB &= ~(1 << PB0)
#define rightLedOn PORTB |= (1 << PB1)
#define rightLedOff PORTB &= ~(1 << PB1)
uint8_t brightness = 0;    // how bright the LED is
uint8_t fadeAmount = 5;
byte counter = 0;
byte OldCounter = 0;
unsigned long timer = 0;

bool PINB2State = LOW;
bool PINB3State = LOW;
bool PINB4State = LOW;


void setup()
{
  DDRB |= (1 << PB0) | (1 << PB1) ; //LEDs ARE CONNECTED TO PB0 AND PB1, GND IS COMMON
  DDRB &= ~((1 << PB2) | (1 << PB3) | (1 << PB4)); //INPUT BUTTON ON PB3,PB4& PB2
  PORTB |= (1 << PB2) | (1 << PB3) | (1 << PB4); //ACTIVATE INPUT_PULLUP

  // Enable Pin Change Interrupts on PB3 and PB4
  /* cli();
    GIMSK |= (1 << PCIE);
    PCMSK |= (1 << PCINT2) | (1 << PCINT3) | (1 << PCINT4);
    sei();*/
}


void mydelay(unsigned delay1)
      {
        timer = millis();
        while (millis() < timer + delay1);
      }



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: break;
  }
  //for PB2 button
  if ((PINB & (1 << PINB2)) == 0 )
  {
    PINB2State = HIGH;
    counter = 12;
  }


  //for PB3 button
  if ((PINB & (1 << PINB3)) == 0 )
  {
    PINB3State = HIGH;
    counter = 13;
  }

  //for PB4 button
  if ((PINB & (1 << PINB4)) == 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)   mydelay(2000) ;
  }
  // wait for 30 milliseconds to see the dimming effect
  mydelay(50) ;
}
void Flasher2()
{
  leftLedOn;  rightLedOn;     mydelay(500) ;
  leftLedOff; rightLedOff;    mydelay(500) ;
}
void Flasher3()
{
  LeftLedFlash();   LeftLedFlash();   LeftLedFlash();      LeftLedFlash();    mydelay(200) ;
  RightLedFlash();  RightLedFlash();  RightLedFlash();     RightLedFlash();   mydelay(200) ;
}

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

void Flasher6()
{
  LeftLedFlash();      RightLedFlash();      mydelay(60) ;
  LeftLedFlash();      RightLedFlash();     mydelay(500) ;
}
void Flasher7()
{
  rightLedOn;  leftLedOn;  mydelay(50) ;
  rightLedOff; leftLedOff; mydelay(50) ;
  rightLedOn;  leftLedOn;  mydelay(50) ;
  rightLedOff; leftLedOff; mydelay(500) ;
}
void Flasher8()
{
  rightLedOn;      mydelay(50) ;
  rightLedOff;     leftLedOn;      mydelay(100) ;
  leftLedOff;      rightLedOn;     mydelay(50) ;
  rightLedOff;     leftLedOn;      mydelay(100) ;
  leftLedOff;      rightLedOn;     mydelay(50) ;
  rightLedOff;     mydelay(1500) ;
}
void Flasher9()
{
  for ( uint8_t i = 0; i < 3; i++)
  {
    leftLedOn;    mydelay(50) ;   leftLedOff;    mydelay(50) ;
  }
  for (uint8_t i = 0; i < 3; i++)
  {
    rightLedOn;   mydelay(50) ;   rightLedOff;    mydelay(50) ;
  }
}
void Flasher10()
{
  leftLedOn;    mydelay(50) ;    leftLedOff;
  rightLedOn;   mydelay(50) ;    rightLedOff;
  mydelay(2000) ;
}

void Flasher11()
{
  leftLedOn; rightLedOn;   mydelay(50) ;  leftLedOff;  rightLedOff;
  mydelay(3000) ;
}
void LeftLedFlash()
{
  leftLedOn;  mydelay(30) ;  leftLedOff;  mydelay(30) ;
}
void RightLedFlash()
{
  rightLedOn;  mydelay(30) ;
  rightLedOff; mydelay(30) ;
}
void LeftIndicator()
{
  rightLedOff;
  leftLedOn;  mydelay(500) ;
  leftLedOff; mydelay(500) ;
}
void RightIndicator()
{
  leftLedOff;
  rightLedOn;  mydelay(500) ;
  rightLedOff; mydelay(500) ;
}

See Using millis() for timing. A beginners guide, Several things at the same time and the BlinkWithoutDelay example in the IDE

how to use blinkwithoutdelay in my code.. pls let me know..

BTW

  • LEDs require a series dropping resistor to limit the LED current.
  • Switches should connect to GND.

You must be a fast reader, to have looked it over in no more than one minute...

I repeat - See Using millis() for timing. A beginners guide, Several things at the same time and the BlinkWithoutDelay example in the IDE

You do know this is as bad as delay( ) ?

so how to overcome this pls.

Please read the links offered you.

all switch connected to GND. yes. I will connect three series led for this.. my problem is with code not schematic.. This is for reference..

Thank you

But I will use several delay type in my code. like delay(50), delay(100), delay(3000)...

How to use this with Blinkwithoutdelay ....

Pls give me an example.

what does it means.

How many requests do you will open addtionally?
Don´t make cross post!

Your schematic shows only one switch connected to GND.
Your schematic does not have resistors on each LED.
Your problem is the schematic is not draw correctly and you are not incorporating millis( ) properly to achieve the BWD benefits.

Is there a language you need that excellent website you've been referred to translated into?

It may take you more than a few minutes, but if you work through it, you might figure this out.

a7

Please stop begging for answers, and go read the information you were given. If you can't understand it there, you won't understand it here.

by mistake i forgot to add resisitor in series with leds.. also gnd to other two switch. Practically it has not done.. Only code error pls

Thanks for error finding.

Maybe this guy can help, he seems to be on to a nifty trick:

a7

There is no code error. You have to learn the techniques that are suggested in the documents that were linked to, in order to do what you are asking. Nobody here is going to just fix your code for you.

Yes I am requesting all the senior for my code error... If you find problem in my post then pls ignore it.