473,386 Members | 1,827 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

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_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;
}
}
Jun 2 '14 #1

✓ answered by weaknessforcats

This code:

Expand|Select|Wrap|Line Numbers
  1. void LEDcount (int led)
  2.     {
  3.         led=led+1;
  4.         if led=1{
  5.             PORTB=0b00111100;
  6.         }
  7.         if led=2{
  8.             PORTB=0b00111010;
  9.         }
  10.         if led=3{
  11.             PORTB=0b00110110;
  12.         }
  13.     }
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:


Expand|Select|Wrap|Line Numbers
  1. int count = 0;
  2.  while(1)
  3. {
  4.   LEDcount(&count);
  5.    etc...
  6. }
  7.  
  8. void LEDcount(int* led)
  9. {
  10.     *led += 1;   //increment the variable in main()
  11.      etc..
  12. }

2 3853
weaknessforcats
9,208 Expert Mod 8TB
This code:

Expand|Select|Wrap|Line Numbers
  1. void LEDcount (int led)
  2.     {
  3.         led=led+1;
  4.         if led=1{
  5.             PORTB=0b00111100;
  6.         }
  7.         if led=2{
  8.             PORTB=0b00111010;
  9.         }
  10.         if led=3{
  11.             PORTB=0b00110110;
  12.         }
  13.     }
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:


Expand|Select|Wrap|Line Numbers
  1. int count = 0;
  2.  while(1)
  3. {
  4.   LEDcount(&count);
  5.    etc...
  6. }
  7.  
  8. void LEDcount(int* led)
  9. {
  10.     *led += 1;   //increment the variable in main()
  11.      etc..
  12. }
Jun 2 '14 #2
Thank you very much! I will try this ASAP. One more question: Am I debouncing the button correctly?
Jun 9 '14 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

9
by: aaron | last post by:
I have a few documents in which I need to get a total word count. Can anyone help? I'd like to create this program so I can get the result after entering the filename, and have the option to...
1
by: heckstein | last post by:
I am working in Access 2002 and trying to create a report from our company's learming management system. I am not a DBA and most of my SQL knowledge has been self taught through trial and error. I...
5
by: isabelle | last post by:
hi, every body.. I have two program I couldn’t solve them So, can any body help me. please!! 1-Write a program that accepts a character and count number of occurrences in a file. The file...
1
by: manojrai84 | last post by:
give me the codes in C Q- write a prg. to count the charachter ,which is given by the user??
11
by: Mack | last post by:
Hi all, I want to write a program to count number of bits set in a number. The condition is we should not loop through each bit to find whether its set or not. Thanks in advance, -Mukesh
2
by: lengyun | last post by:
Hi there, I've just started to learn c and there's some sections which i have no idea of. I'm hoping some one can help me. I'm required to write a program which counts the number of characters and...
4
by: sahil | last post by:
Hello frends i am learning c language, I want to make a program which count occurence of each element in an array .I write following code for it but ity is not giving me desired result.pls help me....
1
by: jlt206 | last post by:
This code <?php include("counter.php")?> on the webpage produces the count number. (function code below) I want to place the current number into a variable $MemberNo or into a FormField to be sent...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.