Go Down

Topic: Dumb Question - Global variable not holding value on change (Read 1 time) previous topic - next topic


This is not correct function as I understand it a global variable should maintain it's value until the loop restarts

It would be more accurate to say that it retains its value until something changes it.  loop() restarting has nothing to do with it.

So, eXtime is not declared global after all and is behaving as one would expect considering that it is declared and initialised as the first thing in loop()
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.


Yes I am aware that the variable is inside the loop function now, it is however outside the nested while statements.  So once the loop is restarted it will, be reset to zero that is fine.

I made that change when I realized that the data is getting corrupted.  However it changes nothing significant in the behavior of the code, please see the output from the serial monitor attached to the full code post.

Inside the if statement the variable is set to 6 immediately upon exit is corrupted.


Mar 29, 2013, 12:58 am Last Edit: Mar 29, 2013, 01:01 am by PeterH Reason: 1

please see the output from the serial monitor attached to the full code post.

Inside the if statement the variable is set to 6 immediately upon exit is corrupted.

This is the output you're referring to?

Code: [Select]

1st: 2
2nd: 58
3rd: 203
4th: 61
6 in loop xtime val

So is your whole question actually about the two garbage characters printed at the end, and not about what's happening to eXtime?

What happens after this point - is the Arduino still working? Does it print anything else after this? If so, what?

The code in Get_card() looks horrible, and suppressAll() is a disaster waiting to happen. I wouldn't be at all surprised if you have simply run out of memory due to recursive calls to suppressAll blowing your stack space.
I only provide help via the forum - please do not contact me for private consultancy.

Nick Gammon

This is an extremely bad idea:

Code: [Select]

void suppressAll()                                //suppresses the "null result" from being printed if no RFID tag is present
  if(mySerial.available() > 0)

Get rid of the recursion and then we can talk.

In any case just reading and discarding serial data is rarely a good idea.

Nick Gammon

I don't understand what Get_card is doing but perhaps you should read this:



Thanks for your reply Nick,

I used the RFID reader code as I found it, with very little change. 

I will clean it up in the am and get rid of the recursive call.

I generally try to clean up my code once it is working, but had done so yet with this, I had not considered the impact of the recursive call.


Just wanted to say thanks to all and particularly Nick, the recursive call, and another bad section of code were generating the problem.  I removed that call as well as another similar piece of code and works as expected.  The card reading code needs some clean up to read the card correctly but that is pretty minor.

Thanks again for your help.

Nick Gammon

Thanks, and glad it's working. I must give credit to PeterH who initially spotted the problem with the recursion. I just made it more obvious what the problem was. :)

Go Up