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) ;
}
