Go Down

Topic: Read a variable one time(snapshot) that changes all the time (Read 593 times) previous topic - next topic


Jan 17, 2013, 09:44 am Last Edit: Jan 17, 2013, 10:32 am by noobdude Reason: 1

I'm trying to take a snapshot of the value of a variable that is changing all the time. I need to use the current value as a starting point for another process. The problem I'm having is it keeps reading the variable that's always changing and it messes everything up. Hope that makes sense. Don't mind if code isn't 100% I'm just typing it and copying some really quick for example.

Code: [Select]

int currentTemp = thermocouple.readCelsius();
int startTemp;//This is what needs to be the snapshot........
int counter;
int setpoint = 120;
int setpointRamp;
byte rampRate = 1;

long currentMillis = millis();
long previousMillis;

startTemp = currentTemp;//This is from my post edit I forgot to add this line first time. Need snapshot here.
if(currentMillis - previousMillis > 1000) {//seconds counter
    previousMillis = currentMillis;
    counter = counter + rampRate;
setpointRamp = startTemp + counter;
    lcd.setCursor(8, 2);
  if (setpointRamp == setpoint) {
//Do stuff

Any help would be greatly appreciated. Thanks!

EDIT:Fixed snippet

Nick Gammon

...  it keeps reading the variable that's always changing and it messes everything up

What is this "it" of which you speak?
Please post technical questions on the forum, not by personal message. Thanks!

More info:


Only update it when you want to not all the time.
Code snippets are not much use as we can't see what you are doing but basically if you can't stop taking the reading then make a copy and only update it when you need to.


Jan 17, 2013, 10:28 am Last Edit: Jan 17, 2013, 10:30 am by noobdude Reason: 1
Nick, I forgot to add the line startTemp = currentTemp. startTemp is 'it'. That line messes everything up since currentTemp is being updated every loop. My wording is a little ghetto though yeah I'll work on that. I got a chuckle from that. :)

Mike, that's exactly what I want to do is copy the variable value but only at a certain time. How can I do that? I need to do this up to 9 different times so has to be in loop. I've been sitting at PC for awhile and get a little zoned out. I'm sure its simple. I'm still very new at this too I'm sure you could tell. :)

I can post all my code but its alot longer..


what I want to do is copy the variable value but only at a certain time. How can I do that?

Decide the conditions under which you want to copy the variable. Write some code that determines whether those conditions have been met. If they are met, take the copy.
I only provide help via the forum - please do not contact me for private consultancy.


Do you mean the pin value is different every time?
I'm confused, my variables only change when my code changes them but sometimes those pins got minds of their own.

Nick Gammon on multitasking Arduinos:
1) http://gammon.com.au/blink
2) http://gammon.com.au/serial
3) http://gammon.com.au/interrupts


I just need to know when to take a break..... I was reading the variable from within the if statement. Noticed what happened within 30 seconds this morning. 

Go Up

Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

via Egeo 16
Torino, 10131