Pages: [1]   Go Down
Author Topic: Mysterious Button Press behavior  (Read 825 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 12
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

The setup: 3 buttons/ moment switches that graph (in Processing) how many times any of the buttons have been pressed.

The problem: 2 out of 3 buttons are not incrementing the bar graph on the first tap of the button unless it is tapped twice. Instead, if I tap a button once, the bar graph fluctuates between 0 and 1 when it's supposed to stay at 1 because the button has been tapped once. If I tap the same button again, the fluctuation stops and remains at 2, then increments upon button presses just fine after that.

At first I thought this may be  a debouncing issue, but a ripple signal doesn't occur. The fluctuation remains between 0 and 1.  

What's going on??? Please help!

Quote
int yesButton = 10;              //digital input for the yes button
int noButton = 11;               //digital input for the no button
int maybeButton = 12;         //digital input for the maybe button

int yesValue = 0;            //reading from the yes button
int noValue = 0;             //reading from the no button  
int maybeValue = 0;        //reading from the maybe button



  void setup(){
    //configure the serial connection
    Serial.begin(9600);

    //configure the digital inputs
    pinMode(yesButton, INPUT);
    pinMode(noButton, INPUT);
    pinMode(maybeButton, INPUT);
     }

  void loop() {
    //read the digital sensors:
    if (digitalRead(yesButton) == HIGH) {
    yesValue = digitalRead(yesButton);
    delay(1000);
    } else {
   yesValue = digitalRead(yesButton);
    }
  
    if (digitalRead(noButton) == HIGH){
    noValue = digitalRead(noButton);
    delay(1000);
    } else {
    noValue = digitalRead(noButton);
    }
    
    if (digitalRead(maybeButton) == HIGH) {
    maybeValue = digitalRead(maybeButton);
    delay(1000);
    } else {
    maybeValue = digitalRead(maybeButton);
    }
 
    //print the results:
    Serial.print(yesValue, DEC);
    Serial.print(",");
    Serial.print(noValue, DEC);
    Serial.print(",");
    //print the last sensor value with a prinln()
    //so each reading prints on a line by itself:
    Serial.println(maybeValue, DEC);
  }
  


Logged

Norway
Offline Offline
Sr. Member
****
Karma: 0
Posts: 344
Just dip it in mercury and power it up
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

You must try pullup resistors, or pulldown resistors to ground (depending on wireing)
I remember seeing strange behavior on buttons when not used.

Here is some tutorials
http://www.ladyada.net/learn/arduino/lesson5.html
http://roguescience.org/wordpress/?page_id=11
« Last Edit: March 15, 2010, 01:56:13 pm by dsg0812 » Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 12
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks for pointing me to a direction. It seems after rewiring the circuit with either pulldown or pullup resistors, I still get that floating input signal upon the first button press (but disappears after the 2nd press). Dealing with pullup/down resistors has been a bit of a hairy process. Is there a way to code around it instead? i.e. enabling pullup resistors in arduino or the button library?  

 Am I on the right track here?

Quote
int yesButton = 10;              //digital input for the yes button
int noButton = 11;               //digital input for the no button
int maybeButton = 12;            //digital input for the maybe button

int yesValue = 0;            //reading from the yes button - button state
int noValue = 0;             //reading from the no button  -  button state
int maybeValue = 0;          //reading from the maybe button - button state

//debounce
//int buttonState;

  void setup(){
    //configure the serial connection
    Serial.begin(9600);//Serial.begin(9600);
    //configure the digital inputs
    pinMode(yesButton, INPUT);
  
    pinMode(noButton, INPUT);
    
    pinMode(maybeButton, INPUT);
  
   //Enable pull up resistors in Arduino
   digitalWrite(yesButton, HIGH);
   digitalWrite(noButton, HIGH);
   digitalWrite(maybeButton, HIGH);
  
  
     }

  void loop() {
    //read the digital sensors:
    if (digitalRead(yesButton) == HIGH) {
    yesValue = digitalRead(yesButton);
  
    delay(500); //  delay(750); //100  delay for 3 seconds. 1000 miliseconds = 1 second
    } else {
   yesValue = digitalRead(yesButton);
  
    }
  
    if (digitalRead(noButton) == HIGH){
    noValue = digitalRead(noButton);
  
    delay(500);// delay(750); //100
    } else {
    noValue = digitalRead(noButton);
 
    }
    
    if (digitalRead(maybeButton) == HIGH) {
    maybeValue = digitalRead(maybeButton);
  
    delay(500);//  delay(750); //100
    } else {
    maybeValue = digitalRead(maybeButton);
    
    }
 
    //print the results:
    Serial.print(yesValue, DEC);
    Serial.print(",");
    Serial.print(noValue, DEC);
    Serial.print(",");
    //print the last sensor value with a prinln()
    //so each reading prints on a line by itself:
    Serial.println(maybeValue, DEC);
  }
  
  

« Last Edit: March 16, 2010, 02:09:02 pm by linux_user » Logged

Manchester (England England)
Online Online
Brattain Member
*****
Karma: 626
Posts: 34129
Solder is electric glue
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
It seems after rewiring the circuit with either pulldown or pullup resistors, I still get that floating input signal upon the first button press

So you have not wired it up correctly.
Pull up:-
 Resistor to input other end to 5V. Switch from input other end to ground.
Logged

Norway
Offline Offline
Sr. Member
****
Karma: 0
Posts: 344
Just dip it in mercury and power it up
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Then it is the intresting part of using delay()
that bit might actually freeze the arduino from getting to notice your button push, try to code like blink without delay sketch.

DAvid
Logged

0
Offline Offline
Shannon Member
****
Karma: 206
Posts: 12169
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Dealing with pullup/down resistors has been a bit of a hairy process. Is there a way to code around it instead? i.e. enabling pullup resistors in arduino or the button library?  

I think all you have to do is force the pin high despite changing to input mode - assuming pinMode() does the obvious thing:
Code:
 pinMode (pin, INPUT) ;
  digitalWrite (pin, HIGH) ;

I change to input mode first to avoid creating a power supply short should the button happen to be pressed while pin is high in output mode.  Anyway I think this works.  The pull-ups are FETs on the chip that are very very approximately 10k.
Logged

[ I won't respond to messages, use the forum please ]

Manchester (England England)
Online Online
Brattain Member
*****
Karma: 626
Posts: 34129
Solder is electric glue
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

This will work only if your input button is connected between the input and ground.

Quote
The pull-ups are FETs on the chip that are very very approximately 10k.
The data sheet says they are between a minimum value of 20K and a maximum value of 50K.
 
So 10K is only between 100% and 500% out.
Logged

Copenhagen / Denmark
Offline Offline
Edison Member
*
Karma: 6
Posts: 2360
Do it !
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

maybee you need to debounce the switches / buttons.
Logged

Cumming, GA
Offline Offline
Edison Member
*
Karma: 20
Posts: 1655
Ultimate DIY: Arduino
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

LIMOR has some nice code you might want to borrow from:

Code:
int switchstate;
  
  reading = digitalRead(inPin);
  
  // If the switch changed, due to bounce or pressing...
  if (reading != previous) {
    // reset the debouncing timer
    time = millis();
  }
    
  if ((millis() - time) > debounce) {
     // whatever the switch is at, its been there for a long time
     // so lets settle on it!
     switchstate = reading;

     // Now invert the output on the pin13 LED
    if (switchstate == HIGH)
      LEDstate = LOW;
    else
      LEDstate = HIGH;
  }
  digitalWrite(outPin, LEDstate);

  // Save the last reading so we keep a running tally
  previous = reading;

Note:  No real delays, just a way to determine pressed state.  I suspect that your delays are defeating your Key Press samples.
« Last Edit: March 17, 2010, 07:18:13 am by pwillard » Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 12
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

SOLVED!

Thank you to everyone who helped me debug this, after redesigning many configurations of arduino code and breadboard wiring setup, I found the solution to this bug elsewhere: the Processing code that graphs and listens to the arduino's serial port for input.

The problem was here:

Quote
void serialEvent(Serial myPort){

//Split the string at the commas and convert the sections into integers:
int sensors[] = int(split(myString, ','));

//If all sensor strings are received, use them:
if (sensors.length == 3)
   if (sensors[0] > 0) {
yesValue = yesButton;
yesButton++;
   }
yesButtonGraph();
}

//....and so forth for No and Maybe buttons @ sensor[1] and sensor [2] respectively.

}


The solution:

Quote
void serialEvent(Serial myPort){

//Split the string at the commas and convert the sections into integers:
int sensors[] = int(split(myString, ','));

if (sensors. length = 3)
   if (sensors[0] > 0) {
yesValue = yesButton;
yesButton++;
yesButtonGraph();
   }
}

The culprit was the misplaced function yesButtonGraph() that should have been inside the curly brace.

On that note, does anybody recommend any useful debuggers (IDE) with Arduino/Processing? Using a debugger probably would've saved a lot more time.

Thanks again.
« Last Edit: March 17, 2010, 03:36:31 pm by linux_user » Logged

Pages: [1]   Go Up
Jump to: