Pages: [1]   Go Down
Author Topic: Very, very strange bug!  (Read 482 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 27
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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


Code:
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.
« Last Edit: September 19, 2013, 12:32:45 pm by sina92 » Logged

West Yorkshire, UK
Offline Offline
Edison Member
*
Karma: 50
Posts: 1451
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 309
Posts: 26534
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Albuquerque, NM
Offline Offline
Newbie
*
Karma: 2
Posts: 46
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 27
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
Logged

New River, Arizona
Offline Offline
God Member
*****
Karma: 19
Posts: 936
Arduino rocks
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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??
Logged

Trying to keep my house under control http://www.desert-home.com/

Offline Offline
Newbie
*
Karma: 0
Posts: 27
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Offline Offline
God Member
*****
Karma: 17
Posts: 522
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Offline Offline
God Member
*****
Karma: 17
Posts: 522
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.)
Logged

West Yorkshire, UK
Offline Offline
Edison Member
*
Karma: 50
Posts: 1451
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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.
Logged

UK
Offline Offline
Shannon Member
****
Karma: 223
Posts: 12630
-
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.

Code:
int time = 10, x = 10, y = 10;
Logged

I only provide help via the forum - please do not contact me for private consultancy.

Pages: [1]   Go Up
Jump to: