Pages: [1]   Go Down
Author Topic: Code for button press detection  (Read 436 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Jr. Member
**
Karma: 0
Posts: 69
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

is this code allright?

Code:
int  button()

  int y=digitalRead(8);
  if(y==HIGH)
  {             
    y=LOW ;
    delay(1000);

    y=digitalRead(8);
    if(y==LOW)
      return 1;
    else                   
      return 2 ;     
  }   

  else return 0;


}
it doesnt seem to work  :/  
it should return 2 if its a long press
1 if it is a short press
and 0 if it doesnt detect  any button
« Last Edit: January 21, 2013, 11:53:23 am by harshvardhan » Logged

Atlanta
Offline Offline
Full Member
***
Karma: 4
Posts: 128
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Normally, button presses go from HIGH to LOW (switch contact to gnd) - Looks like you're running LOW to HIGH

Logged


California
Offline Offline
Faraday Member
**
Karma: 88
Posts: 3382
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Start by fixing your indentation: Put each curly brace on its own line and use the Tools > Auto Format function in the Arduino IDE. Then, manually copy and paste the code from the IDE to the forums (don't use the Copy to Forum function) surrounding it with CODE tags.
Logged

East Anglia (UK)
Offline Offline
Faraday Member
**
Karma: 114
Posts: 4257
May all of your blinks be without delay()
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

In what way does it not work ?
Have you tried putting some Serial.prints in there to see what the value of y is at various points ?
Logged

Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

Offline Offline
Jr. Member
**
Karma: 0
Posts: 69
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

yea, it returns erratic value
when i put my hand close to the wire, it returns different values



Code:
int  button()

  int y=digitalRead(8);
  if(y==HIGH)
  {             
    y=LOW ;
    delay(1000);

    y=digitalRead(8);
    if(y==LOW)
      return 1;
    else                   
      return 2 ;     
  }   

  else return 0;


}
Logged

Offline Offline
Jr. Member
**
Karma: 0
Posts: 69
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Normally, button presses go from HIGH to LOW (switch contact to gnd) - Looks like you're running LOW to HIGH



could u please ellaborate on that
and yes, my switch normally passes no current (atleast thats how i planned it to be), if i press, the circuit is completed
Logged

California
Offline Offline
Faraday Member
**
Karma: 88
Posts: 3382
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Your input pin is floating then. You need to enable the internal pull-up resistor so that when the switch is open, the input will read HIGH. You also need to modify your circuit so that pressing the button will connect the input pin to GND.
« Last Edit: January 21, 2013, 11:56:51 am by Arrch » Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 614
Posts: 49363
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

What is connected to pin 8? A complete diagram, please.

What you have sounds a lot like a floating pin condition, caused by not using proper pullup or pulldown resistors.
Logged

Pages: [1]   Go Up
Jump to: