confused with first codes

hello,

I just received my boards. I am doing some of the tutorials, but something puzzles me. I am talking about the ‘serial write’ example that comes with the Arduino_003 particulary. This one :

int ledPin = 13;   // select the pin for the LED
int buttonPin = 7; // select the pin for the button
int val = 0;       // variable to store the data from the serial port

void setup() {
  pinMode(ledPin,OUTPUT);   // declare the LED's pin as output
  pinMode(buttonPin, INPUT);   // delcare the button pin as input
  beginSerial(19200);        // connect to the serial port
}

void loop () {
  // read the button and store the value
  val = digitalRead(buttonPin);

  // if the button is at HIGH, turn the LED on, off otherwise
  if (val == HIGH) {
    printString("HIGH");
    digitalWrite(ledPin, HIGH);
  } else {
    printString("LOW");
    digitalWrite(ledPin, LOW);
  }

  printNewline();
  delay(100);      // convenient to use delays when sending stuff back to the comp.
}

As I understand the code, the LED should blink only when the button is pressed, shouldn’t it ? But it keeps blinking (even if I don’t plug anything on the board but the LED). Is this the normal behaviour ? What am i missing ?

Thanks in advance,

Julien.

more confusion…

I gave other tests. I get these strange results only when I try to read some digital in (I haven’t tried with analog yet). I mean, I tried a simple code posted here that print ‘hello’ through the serial port, and it works fine. I also tried a code to read a button and send ‘LOW’ ‘HIGH’ to Max. It works fine as well, but sometimes the board sends ‘HIGH’ when it shouldn’t. So I assume the problem is not caused by the code.

Moreover, the problem doesn’t seem to be cause by my wiring, since, I get the same errors when my button is not plugged on the board.

Could there be a problem with my USB powering or something like this ? Sorry if it sounds stupid, but I have no experiences with boards, and am far to much anxious (maybe paranoid) since my project have to be finished next week. I’d just like to understand what I am missing or if there’s something wrong.

Thanks in advance.

If you remove the button, you will read (relatively) random data from the pin. Unconnected pins have nothing to keep them at a steady 0 or 5 volts, so they tend to jump around (e.g. if you tap on it with your finger).

How did you wire the button? There’s a schematic here:
http://wiring.org.co/reference/electronics/button.html
(but note that the pin will be HIGH normally and go LOW when you press the button).

Thanks for your answer.

If you remove the button, you will read (relatively) random data from the pin. Unconnected pins have nothing to keep them at a steady 0 or 5 volts, so they tend to jump around (e.g. if you tap on it with your finger).

Ah ok. So I worried for nothing… Good to know. :wink:

How did you wire the button? There’s a schematic here:
http://wiring.org.co/reference/electronics/button.html
(but note that the pin will be HIGH normally and go LOW when you press the button).

The button seems to be correctly wired. I just gave another test and it works. I sometimes get jumps of states without pressing the button or not, but I’ll try with other cables. These are really “thin” so maybe it leads to bad connections, and then errors ? At least, that’s the more probable reason I see.

Thanks again.
Julien.