Serial print pin status

Hi,
today I feel more stupid than usual! I cannot get this done!

I am trying to print on the serial a message if a digital pin is high and I am failing badly.
Basically what I would like to achieve is the serial to send out a simple message "1" if pin 2 is HIGH.
I have a microswitch with 5V on a open contact, on the other side I have connected it directly to pin2 and in parallel to ground trough a 10K resistor.

What am I doing wrong?

const int buttonPin = 2;  
int buttonState = 0;  

void setup() {
pinMode(buttonPin, INPUT);     
}

void loop(){
  buttonState = digitalRead(buttonPin);

  if (buttonState == HIGH) {     
Serial.println ("1");
  } 
  else {
  }
}

Help

Your description of your circuit is confusing. But one thing I notice in your script is you never initialize the serial port. You need a Serial.begin(); line in your setup().

You are absolutely correct.
I did not copy the line "Serial.begin(9600);" in the code of the post but is there in the sketch.
The problem I am getting with this code is that the serial keeps printing the message independently from the status of the switch.

const int buttonPin = 2;  
int buttonState = 0;  

void setup() {
pinMode(buttonPin, INPUT);     
}

void loop(){
Serial.begin(9600);
buttonState = digitalRead(buttonPin);

  if (buttonState == HIGH) {     
Serial.println ("1");
  } 
  else {
  }
}
/code]

Sounds like you have a floating input - you'll need a pulldown resistor on your switch.

Ditch the resistor, enable the internal pull-up resistor, connect one pin of the switch to the pin and the other to the ground. I would also add some sort of delay so you aren't printing so much, so quickly. That's probably why it seems to not stop printing; it's trying to catch up with what you've told it to print.

Also in Arrch's (nicer) scenario, you'll need to change your test to look for LOW.

Don't put Serial.begin in your loop.

wildbill:
Also in Arrch's (nicer) scenario, you'll need to change your test to look for LOW.

Unless, of course, the switch is a NC type... :wink: But his switch is probably a NO type.

Quick switch primer for those who need it:
Most push button switches are NO type. This stands for Normally Open, meaning in the normal, unactivated state the switch contacts are open. When you press the button (activate it), the contacts close.

Some push button switches are NC type (commonly used for emergency stop buttons). This stands for Normally Closed, meaning in the normal, unactivated state the switch contacts are closed. When you press the button (activate it), the contacts open.

These terms are also used for relay contacts, where the normal state is with the relay coil off.

Hi,
Please note that the switch in the circuit is NO, so there is no current flowing unless i push it.
There is a pullup resistor connected betwen the pin and ground. It is a classic 10 k.
Despite all the above the serial keeps printing the message continuosly.

So you've made changes, but you are not going to show us what the new code looks like?

Why are you using an external pullup instead of adding a single line of code to enable the internal pullup?

@Arrch, i have not changed code. I only added the serial connection line in the post.
Do you thing the internal pullup resistor would solve the problem?

felicemassaro:
@Arrch, i have not changed code. I only added the serial connection line in the post.
Do you thing the internal pullup resistor would solve the problem?

So you didn't add a delay like I suggested? I guess you didn't try "all of the above'.

It takes about 10 seconds to make the change to the internal pullup, and at the very least, simplifies your circuit.

@Arrch, thanks for the suggestions. I will implement the changes and then let you know how is it goes.
Thanks again

@Arrch, I got uour point about the internal pullup.
http://www.cmiyc.com/tutorials/arduino-pull-ups/

I have people for dinner now but tomorrow I will change the setup and code.
Thanks again.

I have people for dinner now

Uh-oh. Cannibal alert!

The rather long-winded code can be neatened to one line (makes commenting out
debug statements a lot easier):

buttonState = digitalRead(buttonPin);

  if (buttonState == HIGH) {     
Serial.println ("1");
  } 
  else {
  }

becomes:

  if (digitalRead (buttonPin)) Serial.println ("1") ;

If you are interesting in low:

  if (!digitalRead (buttonPin)) Serial.println ("0") ;

or more generally:

  Serial.println (digitalRead (buttonPin)) ;

Do you know, I do believe Hannibal Lector ended up in Florence at the end of Silence of the Lambs?

:fearful:

Hi,
Thanks for the suggestion, I will implement for sure the shortest code, but right now, and despite the use of the internal pullup resistor (digitalWrite(1, HIGH);)as suggested by Arrch, the problem I have is that serial keeps printing continuously the message "1" even if the button is not pressed on a NO contact. I shall say that even pressing the button the serial just keeps printing the same!
What the ****** am I doing wrong here?

MarkT:
The rather long-winded code can be neatened to one line (makes commenting out
debug statements a lot easier):

buttonState = digitalRead(buttonPin);

if (buttonState == HIGH) {     
Serial.println ("1");
  }
  else {
  }



becomes:


if (digitalRead (buttonPin)) Serial.println ("1") ;




If you are interesting in low:


if (!digitalRead (buttonPin)) Serial.println ("0") ;



or more generally:


Serial.println (digitalRead (buttonPin)) ;

Yes, that is correct. He sliced the stomac of the victim and then with a rope around his neck the guy was pushed down the balcony. All his interiors ended up on the pavement.
Quite disgusting!!!

AWOL:
Do you know, I do believe Hannibal Lector ended up in Florence at the end of Silence of the Lambs?

:fearful:

Time to get out the multimeter and see what is actually happening on the pin. You are using the correct pin, aren't you?

As a first step, I would just replace the button with a wire to ground and just see if you get no response.