Serial.Print help needed.

Hi,

I've had my arduino for a few weeks now and have had relative success with some circuits. I decided to try talking with the arduino. Basically what I want to do is have it print "Hello World!" when I press a button and a LED lights up. The hardware is fine, its the software thats the problem. Never been much of a C based programmer. I want it to print "Hello World!"just the once but I dont want to have to reset the arduino every time so I cant use void setup(). Any way heres my code:

/*
  DigitalReadSerial
  Reads a digital input on pin 2, prints "HELLO WORLD!", lights up an LED
*/

const int buttonPin = 2;
const int ledPin = 11;

int buttonValue = 0;

void setup() 
{
  Serial.begin(9600);
  pinMode(ledPin, OUTPUT);
  pinMode(buttonPin, INPUT);
}

void loop() 
{
  int buttonValue = digitalRead(buttonPin);
  while(buttonValue == HIGH)
      {
        Serial.println("HELLO WORLD!");
        delay(0.001);
        digitalWrite(ledPin, HIGH);
        delay(500);
        digitalWrite(ledPin, LOW);
        break;
      }
}
        delay(0.001);

Did you look at the documentation for the delay function? It takes an unsigned long argument. What kind of delay is 1 microsecond, anyway?

You've defined the button pin as an input, but not activated the internal pull-up resistor. So, you must be using an external pull-up or pull-down resistor, right? The == HIGH part of the if statement implies a pull-down resistor.

Basically what I want to do is have it print "Hello World!" when I press a button and a LED lights up.

And what does it actually do? And, how does that differ from what you want?

What do you mean then just once? Just once every time you pres the button or just once when the button is pressed the first time and no more.

Grumpy_Mike: What do you mean then just once? Just once every time you pres the button or just once when the button is pressed the first time and no more.

just once when the button is pressed but will still allow me to press it again.

just once when the button is pressed

So, why are you using a while loop?

PaulS:

just once when the button is pressed

So, why are you using a while loop?

Lack of knowledge of C based languages really, just working off the little I know.

So remove the while loop but keep the contents of it (correct the delay) and put this at the start of the loop:- while(buttonValue == LOW) { } // do nothing while the button is not pressed.

Wouldn't that just result in the loop carrying out all the time and not when I press it. I probably should have mentioned that sometimes it will print "Hello World" once and other times it will do it twice or three times in quick succession with me only pressing the button once.

Wouldn't that just result in the loop carrying out all the time and not when I press it

No that holds the loop until you press the button.

I probably should have mentioned

Yes you should. That is called contact bounce. Just have a delay before going out to look at the button again.

Alright, thanks guys. All is well now.

Grumpy_Mike:
Yes you should. That is called contact bounce. Just have a delay before going out to look at the button again.

Here is a graphic:

That was from a different project (buttons connected to a port expander) but the interesting thing is about 20 bounces when the button is pressed. The “high” logic level was before the button was pressed, and the low logic level (which it eventually settled down to) was the button-down state.

You can see that waiting around 10 milliseconds would be needed to let the bounces settle.