Very, very strange bug!

Hello. I have a strange bug and I just can't find it.

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


int time = 10;
int interval = 1000;
unsigned long previousMillis = 0;
boolean startButton = false;
bool ledOnce = true;

void loop(){
  startButton = digitalRead(12);
  unsigned long currentMillis = millis();

  if(startButton == HIGH){
    ledOnce = false;
  }
  
  if(ledOnce == false){          
    if(currentMillis - previousMillis > interval){
      time++;
      previousMillis = currentMillis;
      Serial.println(time);
    }
  }
 
}

For each iteration, time should increase by one( 11, 12, 13...). However, time starts at 0 and goes 1..2..3..4... Actually, no matter which value I give time when declaring the variable, it will start at 0.

Does anyone know why?

Please help,
Thanks in advance!

PS: I just noticed that this is not in the "Programming Questions" section, sorry for that. Could an admin move this thread perhaps? Thanks.

I think you need to assign the initial value of time in the setup () function. Otherwise each time the Arduino is reset, the value of time will revert to zero, as will those other variables, I expect.

Paul

Otherwise each time the Arduino is reset, the value of time will revert to zero, as will those other variables, I expect.

Why?

Global variables will get properly initialized even if Arduino is reset.
It is a very strange behavior indeed. My best suggestion, for the moment (neither of them make total sense to me but are worth trying)

  1. change your variable "startButton" to type int and initialize it to LOW. It is just a syntactical issue but worth trying.
  2. Try to rename the "time" variable to some other name and see if that happens.
  3. Try printing the value of time variable in setup() and see if it is 10.

PaulRB:
I think you need to assign the initial value of time in the setup () function. Otherwise each time the Arduino is reset, the value of time will revert to zero, as will those other variables, I expect.

Paul

Hi and thanks for your answer.
Well, I've never had to do this before, however it did work in this case. The question remains, why?

Just took your code, loaded it onto an Arduino and it started counting at 11 and went up from there.

What was the problem again??

Ok guys, I think I found the reason. Actually, the code I wrote on the forums is a short version of some code that I'm using. I had declared time along with some other variables, like this:

int time, x, y = 10;

I don't think that this works in Arduino. I re-wrote the code like this:

int time = 10;
int x = 10;
int y = 10;

Now it all works.

I just tested the code and it worked as expected. The only thing I changed was to switch pin 12 to INPUT_PULLUP instead of INPUT. As I understand it, either you have to use pinMode(INPUT_PULLUP), or you have to do a digitalWrite(HIGH) to the pin (same effect, just newer vs. older syntax), or you have to use an external pullup resistor. I coudln't get the button press to register consistently using your code, but with regard to the variable "time", it works as you describe: starts at 11 and counts up after the initial press.

sina92:
Ok guys, I think I found the reason. Actually, the code I wrote on the forums is a short version of some code that I'm using. I had declared time along with some other variables, like this:
...
Now it all works.

This is why you should usually post the exact code you are having problems with. Sometimes the exact code is too long and you must post a snippet, but you should test the snippet before posting to be 100% sure it still has the problem.

(PS: Don't take this too harshly. I only know this because I've done it myself too many times to count.)

AWOL:

Otherwise each time the Arduino is reset, the value of time will revert to zero, as will those other variables, I expect.

Why?

My thinking was that maybe the initial value was being set at program download but then lost/zeroed on reset. I'm glad I was wrong, it would have been a bit disappointing if it had been true.

sina92:
int time, x, y = 10;

That defines three variables and initialises one of them. It's not Arduino-specific, this is just C++.

If you want to declare them like that and initialise them all, you need to initialise them all.

int time = 10, x = 10, y = 10;