Mysterious Button Press behavior

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!

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

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

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?

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

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.

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

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:

  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.

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

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.

maybee you need to debounce the switches / buttons.

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

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.

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:

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:

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.