Jump to content

Simple push buttons polling

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
8 replies to this topic

#1
anita_p

anita_p

    Newbie

  • Members
  • Pip
  • 2 posts
Hello,

I am trying to write a program for pollling of two push buttons. My main requirements are : the two buttons should be pressed together for 1s. For simple button polling, I wrote this
while(1)
{
  if(BUTTON_1 & BUTTON_2)
     LED_ON();
}
But how can I add 1s condition?

Please suggest ?

Thnxs
Anita

Edited by Jaan, 08 September 2009 - 12:22 PM.
Please use code tags when you are posting your codes!


#2
Aereshaa

Aereshaa

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 790 posts
do this:
timer = 0;
while(0){
if(button1 && button2)timer = time(0);//begin count
else timer = 0;//the user stopped pressing!
if(timer+1 == time(0))led_on();//user pressed for one second.
}

Watches: Nanoha, Haruhi, AzuDai. Listens to: E-Type, Dj Melodie, Nightcore.
"When people are wrong they need to be corrected. And then when they can't accept it, an argument ensues." - MeTh0Dz

#3
anita_p

anita_p

    Newbie

  • Members
  • Pip
  • 2 posts
Thank you for the suggestion and example code.

But what if I dont have timer?

#4
theonejb

theonejb

    Learning Programmer

  • Members
  • PipPipPip
  • 52 posts
As a very inefficient and rather lame technique, calibrate a for loop, like so:


while(1)

{

  if(BUTTON_1 & BUTTON_2) {

    for(int i = 0; i < ONESECINT; i++);

    if(BUTTON_1 & BUTTON_2)

       LED_ON();

  }

}


Try different values for ONESECINT until you have a good approximate 1s interval.

#5
dargueta

dargueta

    Writes binary right handed and hex left handed

  • Moderators
  • 4,716 posts
That won't always work. It will definitely vary from machine to machine, and even on the same machine when different programs are running, the same programs are doing different things, etc.

anita_p, you definitely should have time.h, because it's part of the standard C library. If not, you have a serious problem.

Edited by dargueta, 08 September 2009 - 05:48 PM.

sudo rm -rf /

#6
dcs

dcs

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 775 posts

dargueta said:

anita_p, you definitely should have timer.h, because it's part of the standard C library. If not, you have a serious problem.
Which standard are you using? :p

[edit]Alright. I need to try to quit nitpicking for a while.

[edit=2]To the OP: what platform is this on?

#7
dargueta

dargueta

    Writes binary right handed and hex left handed

  • Moderators
  • 4,716 posts
Sorry, I meant time.h (note the lack of r). Edited in previous post.
sudo rm -rf /

#8
kiddies

kiddies

    Programmer

  • Members
  • PipPipPipPip
  • 130 posts
hey man...give me a complete c code from it...i wanna lear n try....thanks before

#9
dargueta

dargueta

    Writes binary right handed and hex left handed

  • Moderators
  • 4,716 posts
I'd say it's pretty platform-dependent - what system do you want it for? I would imagine that you might be able to use ncurses for this, which would make it portable. However, I've never used the library myself, so I don't know if they support key event capture. Anyone else know?
sudo rm -rf /