Counting Button Presses: I am trying to program an ATtiny13A to count the number of b

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • KLindley
    New Member
    • Jun 2014
    • 2

    Counting Button Presses: I am trying to program an ATtiny13A to count the number of b

    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_Confide nce_Level = 0; //Measure button press confidence
    int Released_Confid ence_Level = 0; //Measure button release confidence


    while (1) //infinite loop
    {

    if (bit_is_clear(P INB, 4)) //if PB4 (button) is pressed
    {
    Pressed_Confide nce_Level ++; //Increase Pressed confidence
    Released_Confid ence_Level = 0; //Reset released button confidence since there is a button press
    if (Pressed_Confid ence_Level >1) //Indicator of good button press
    {
    if (Pressed == 0)
    {
    PORTB = 0b00110001;
    LEDcount(0);
    }
    Pressed_Confide nce_Level = 0;
    }
    }

    else
    {
    Released_Confid ence_Level ++; //This works just like the pressed
    Pressed_Confide nce_Level = 0; //Reset pressed button confidence since the button is released
    if (Released_Confi dence_Level >100)
    {
    PORTB = 0b00111110; //return all ports to their original output
    Pressed = 0;
    Released_Confid ence_Level = 0;
    }
    }
    }
    }
    void LEDcount (int led)
    {
    led=led+1;
    if led=1{
    PORTB=0b0011110 0;
    }
    if led=2{
    PORTB=0b0011101 0;
    }
    if led=3{
    PORTB=0b0011011 0;
    }
    }
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    This code:

    Code:
    void LEDcount (int led)
    	{
    		led=led+1;
    		if led=1{
    			PORTB=0b00111100;
    		}
    		if led=2{
    			PORTB=0b00111010;
    		}
    		if led=3{
    			PORTB=0b00110110;
    		}
    	}
    is called from inside your loop. The argument led is an int and the function increments the int. Unfortunately, the led variable in the function gets destroyed when the function completes so if you are counting on incremented value for the next cycle of the loop you aren't going to get it.

    Change this function to have an int* argument. In the loop you call the function with the address of an int in main(). The function dereferences the address, does the increment (which increments the variable in main() and not the one in the function).

    Example:


    Code:
    int count = 0;
     while(1)
    {
      LEDcount(&count);
       etc...
    }
    
    void LEDcount(int* led)
    {
        *led += 1;   //increment the variable in main()
         etc..
    }

    Comment

    • KLindley
      New Member
      • Jun 2014
      • 2

      #3
      Thank you very much! I will try this ASAP. One more question: Am I debouncing the button correctly?

      Comment

      Working...