Pages: [1]   Go Down
Author Topic: Strange behavior when using Serial  (Read 386 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 3
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hallo,

I am experiencing some strange behavior which I would like some help to understand.

I am playing around with reading the input from a button, but as the button does not perfectly fit into my prototype board it sometimes falls out. When it does, the state of the pin its connected to changes from HIGH to LOW, and back again for an indefinite amount of time, but only if im also writing a serial message. For it to happen, the wire needs to be plugged into the board, but not to anything on the other side (so the problem is not the button popping in an out, the problem can be replicated without the button)
To make it even more strange, it only happens when i have my hands close to the board!

Please see the attach photos.
Code:
void setup()

  // Configure the board 
  pinMode(8, OUTPUT);   
  pinMode(9, OUTPUT); 
  pinMode(4, INPUT);   
  Serial.begin(9600);

  digitalWrite(8, LOW); 
}

void loop()
{
  boolean buttonPressed = false;
  if(digitalRead(4) == LOW)
  {   
    digitalWrite(8, HIGH); 
     buttonPressed = true;
  }
  else
  {
    digitalWrite(8, LOW); 
     Serial.println("button NOT pressed");
  }
 
  if(buttonPressed)
  {
    Serial.println("button pressed");
    delay(250);
    Serial.flush();
  }
}


* snapshotA.jpg (21.13 KB, 640x480 - viewed 4 times.)

* snapshotB.jpg (23.07 KB, 640x480 - viewed 4 times.)
Logged

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

When the switch is not connected to the pin, the pin is floating. It is neither HIGH nor LOW. The function of the pull-up or pull-down resistor is to make sure that there is SOMETHING connected atall times. You are using a pull-up or pull-down resistor, aren't you? Apparently not.

Quote
but only if im also writing a serial message.
If you aren't able to see the output on the serial monitor, because the Arduino is not connected to the PC, how do you know that the pin is floating?

Code:
    Serial.flush();
If you are not reading from the serial buffer, why do you care what data is in the buffer? Do you have a clue what Serial.flush() does? Hint: It has nothing to do with outgoing data.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 3
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Im guessing the problem was a floating pin then because nothing is connected. When the button is connected there is a resistor, but the problem is that the button falls out from time to time as it does not fit the prototype board perfectly.

Im guessing moving my hands made some of the current/voltage on my body influence the floating pin?

When I posted I had no idea what flush did, i only found it in the documentation and decided to give it a go smiley-wink
Logged

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

Quote
When I posted I had no idea what flush did, i only found it in the documentation and decided to give it a go
Do you know now what it does? Did you remove it?

Quote
Im guessing the problem was a floating pin then because nothing is connected. When the button is connected there is a resistor, but the problem is that the button falls out from time to time as it does not fit the prototype board perfectly.
So, you don't have pull-up or pull-down resistors? The built-in pull-up resistors rarely fall out.

Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 3
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I think it clears the incoming serial buffer?

This is my setup: http://arduino.cc/en/Tutorial/Button
Dont know if its a push up or push down. I have a fair bit of experience when it comes to programming, but im a beginner when it comes to electronics.
Logged

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

Quote
I think it clears the incoming serial buffer?
It does, dumping random amounts of data that have not been read yet. Rarely a good thing to do.
Logged

Pages: [1]   Go Up
Jump to: