Pages: 1 [2]   Go Down
Author Topic: Dumb Question - Global variable not holding value on change  (Read 1562 times)
0 Members and 1 Guest are viewing this topic.
East Anglia (UK)
Offline Offline
Faraday Member
**
Karma: 109
Posts: 4069
May all of your blinks be without delay()
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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()
Logged

Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

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

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

UK
Offline Offline
Shannon Member
****
Karma: 222
Posts: 12551
-
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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.
« Last Edit: March 28, 2013, 07:01:49 pm by PeterH » Logged

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

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 474
Posts: 18696
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

This is an extremely bad idea:

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

Get rid of the recursion and then we can talk.

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

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 474
Posts: 18696
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

http://www.gammon.com.au/serial
Logged

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

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

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

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

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 474
Posts: 18696
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Pages: 1 [2]   Go Up
Jump to: