Go Down

Topic: Mysterious Button Press behavior (Read 1 time) previous topic - next topic

linux_user

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);
 }
 




ArduinoM

#1
Mar 15, 2010, 07:53 pm Last Edit: Mar 15, 2010, 07:56 pm by dsg0812 Reason: 1
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

linux_user

#2
Mar 16, 2010, 08:07 pm Last Edit: Mar 16, 2010, 08:09 pm by linux_user Reason: 1
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);
 }
 
 



Grumpy_Mike

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.

ArduinoM

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

MarkT

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: [Select]

 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.
[ I won't respond to messages, use the forum please ]

Grumpy_Mike

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.

MikMo

maybee you need to debounce the switches / buttons.

pwillard

#8
Mar 17, 2010, 01:16 pm Last Edit: Mar 17, 2010, 01:18 pm by pwillard Reason: 1
LIMOR has some nice code you might want to borrow from:

Code: [Select]

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.

linux_user

#9
Mar 17, 2010, 08:17 pm Last Edit: Mar 17, 2010, 09:36 pm by linux_user Reason: 1
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.

Go Up