Pages: [1]   Go Down
Author Topic: Read a variable one time(snapshot) that changes all the time  (Read 257 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Jr. Member
**
Karma: 0
Posts: 68
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi,

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:


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);
     lcd.print(setpointRamp);
    
 }
  
   if (setpointRamp == setpoint) {
//Do stuff
}

Any help would be greatly appreciated. Thanks!

EDIT:Fixed snippet


« Last Edit: January 17, 2013, 04:32:45 am by noobdude » Logged

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

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

What is this "it" of which you speak?
Logged

Manchester (England England)
Offline Offline
Brattain Member
*****
Karma: 602
Posts: 33379
Solder is electric glue
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Offline Offline
Jr. Member
**
Karma: 0
Posts: 68
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

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

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









« Last Edit: January 17, 2013, 04:30:39 am by noobdude » Logged

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

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

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

Pittsburgh, PA, USA
Offline Offline
Faraday Member
**
Karma: 96
Posts: 4769
I learn a bit every time I visit the forum.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.

Logged

I find it harder to express logic in English than in Code.
Sometimes an example says more than many times as many words.

Offline Offline
Jr. Member
**
Karma: 0
Posts: 68
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Pages: [1]   Go Up
Jump to: