Random Number?!?

#define LESEN 2 


  void setup()
{
  pinMode(LESEN, INPUT);
  Serial.begin(9600);
}

double counter = 0;

void loop()
{
  Serial.println(counter);
  do { 
    counter = counter + 0.1;
  } while (digitalRead(2) == HIGH);
}

Hey what's up guys I need your help. So I got this code and when I press the button the variable counter just starts counting from an absolutely random number. Like sometimes it will be 1400 and other times 4000 or 6231 or whatever. Why does this happen? I initialize counter = 0 so why does he jump as soon as I press the pushbutton.

I also have another question completly unrelated to the first one. So the program runs line by line in the "void loop" section right? Why does it not do the loop exclusivly if you know what I mean. Like in my logic it has to print 0 then when I press the button count up (in increments of 0.1) and when I stop pressing print another line. But instead it just keeps printing out lines all the time (when I press the button). Why?

I vaguely recall that print() doesn't like doubles, but I could be completely wrong.

There's a mention of it on the Hobby Electronics website with a proposed solution using the dtostrf() function like this:

static float f_val = 123.6794;
static char outstr[15];

void setup() {
  dtostrf(f_val,7, 3, outstr);

  Serial.begin(9600);
  Serial.println(outstr);
}

void loop(){
}

Hey, thanks for the reply. I don't think that the double variable is the reason since the same thing happens when I use int. As soon as I press the pushbutton counter is set to an completly random variable over and over again

your input is floating, meaning it has no defined state if you haven't connected anything.

you either need an external pull down resistors to ground the input and wait until you have connected your pin to VCC ( == HIGH)
or
you use the built in INPUT_PULLUP and wait until you have connected the PIN to GND (== LOW).

See also SEVERAL tutorials - how to connect a button with the Arduino.
Just one example: https://www.arduino.cc/en/Tutorial/BuiltInExamples/DigitalReadSerial

If this post doesn't answer your question, please post a schematic of your setup AND a really picture where we can see each and every wiring to give better advices.

by the way, there is a very active German forum section for your next question also...

noiasca:
your input is floating, meaning it has no defined state if you haven't connected anything.

you either need an external pull down resistors to ground the input and wait until you have connected your pin to VCC ( == HIGH)
or
you use the built in INPUT_PULLUP and wait until you have connected the PIN to GND (== LOW).

See also SEVERAL tutorials - how to connect a button with the Arduino.
Just one example: https://www.arduino.cc/en/Tutorial/BuiltInExamples/DigitalReadSerial

If this post doesn't answer your question, please post a schematic of your setup AND a really picture where we can see each and every wiring to give better advices.

by the way, there is a very active German forum section for your next question also...

I am an idiot, thank you. I had a pull-up resistor and wrote the code the wrong way. Thank you very much!

I recommend you to read the common mistake when using the button.
Secondly, for counting, you need to detect button state change instead of checking the current state.

markd833:
I vaguely recall that print() doesn’t like doubles, but I could be completely wrong.

You are.
doubles are the same as floats on AVRs.

markd833:
I vaguely recall that print() doesn't like doubles, but I could be completely wrong.

You're thinking of 'printf()'

Ah! That'll be it! :smiley: