I am trying to program an ATtiny13A to count the number of button presses and light up the corresponding LED.
I.E. IF the button (PB4) is pressed once, the button in PB1 lights up. The second time it is pressed LED2 (PB2) lights up.
I am new to Programming in C, I read a lot about button debouncing but I am not sure if I am using it correctly.
Here is my code:
#include <avr/io.h>
#include <util/delay.h>
int main(void)
{
DDRB = 0b00001111; //initial i/o port settings
PORTB = 0b00111110; //initial h/l port settings according to PB-
void LEDcount (int led)
int Pressed = 0;
int Pressed_Confidence_Level = 0; //Measure button press confidence
int Released_Confidence_Level = 0; //Measure button release confidence
while (1) //infinite loop
{
if (bit_is_clear(PINB, 4)) //if PB4 (button) is pressed
{
Pressed_Confidence_Level ++; //Increase Pressed confidence
Released_Confidence_Level = 0; //Reset released button confidence since there is a button press
if (Pressed_Confidence_Level >1) //Indicator of good button press
{
if (Pressed == 0)
{
PORTB = 0b00110001;
LEDcount(0);
}
Pressed_Confidence_Level = 0;
}
}
else
{
Released_Confidence_Level ++; //This works just like the pressed
Pressed_Confidence_Level = 0; //Reset pressed button confidence since the button is released
if (Released_Confidence_Level >100)
{
PORTB = 0b00111110; //return all ports to their original output
Pressed = 0;
Released_Confidence_Level = 0;
}
}
}
}
void LEDcount (int led)
{
led=led+1;
if led=1{
PORTB=0b00111100;
}
if led=2{
PORTB=0b00111010;
}
if led=3{
PORTB=0b00110110;
}
}