How am i able to store a previous value and then compare it with the next value?
For example compare the previous temperature value with the new temperature value to see if it is decreasing or increasing.
How am i able to store a previous value and then compare it with the next value?
For example compare the previous temperature value with the new temperature value to see if it is decreasing or increasing.
You can store a value into a ram variable.
int myOldTemperature;
Look up if( )
Here is a demo code with the use of a couple saved values, one for temperature and one for timing (see the blink without delay example).
const byte temperaturePin = A0;
int temperature;
int lastTemperature;
void setup()
{
Serial.begin(9600);
lastTemperature = analogRead(temperaturePin); // get initial value
}
void loop()
{
static unsigned long lastMillis = 0;
unsigned long interval = 500;
if (millis() - lastMillis >= interval)
{
lastMillis = millis();
temperature = analogRead(temperaturePin);
Serial.print("temperature difference = ");
Serial.println(temperature - lastTemperature);
lastTemperature = temperature;
}
}
Something like this.
int oldTemp;
int newTemp;
. . .
. . .
Serial.println(abs(oldTemp - newTemp));
Appears you might want to spend time with learning:
Arduino links of interest.
How to use this forum:
Getting started:
https://www.arduino.cc/en/Guide
Listing of downloadable 'Arduino PDFs' :
Either Google >>>- - - - > arduino filetype: pdf
Or
https://www.google.ca/search?q=arduino+filetype%3A+pdf&rlz=1C9BKJA_enCA739CA739&oq=arduino+filetype%3A+pdf&aqs=chrome..69i57j69i65.1385j0j7&hl=en-US&sourceid=chrome-mobile&ie=UTF-8
Listing of downloadable 'C++ PDFs' :
Either Google >>>- - - - > C++ filetype: pdf
Or
https://www.google.ca/search?q=c%2B%2B+filetype%3A+pdf&rlz=1C9BKJA_enCA739CA739&oq=c%2B%2B+filetype%3A+pdf&aqs=chrome..69i57.22790j0j7&hl=en-US&sourceid=chrome-mobile&ie=UTF-8
Arduino cheat sheet:
https://dlnmh9ip6v2uc.cloudfront.net/learn/materials/8/Arduino_Cheat_Sheet.pdf
Watch these:
Arduino programming syntax:
https://m.youtube.com/watch?v=CbJHL_P5RJ8
Arduino arithmetic operators:
https://m.youtube.com/watch?v=UUx0_s-ElSs
Arduino control flow:
https://m.youtube.com/watch?v=QpPGGuaGbCA
Arduino data types:
https://m.youtube.com/watch?v=xmZXWMEltEc
Understanding Destructive LC Voltage Spikes:
https://www.pololu.com/docs/0J16/all
OR
https://www.pololu.com/docs/pdf/0J16/destructive_LC_voltage_spikes.pdf
Why decoupling capacitors:
Some things to read
LCD information:
https://learn.adafruit.com/character-lcds?view=all
OR
https://cdn-learn.adafruit.com/downloads/pdf/character-lcds.pdf?timestamp=1573085286
Reading a schematic:
https://learn.sparkfun.com/tutorials/how-to-read-a-schematic
Language Reference:
https://www.arduino.cc/en/Reference/HomePage
Foundations:
https://www.arduino.cc/en/Tutorial/Foundations
How and Why to avoid delay():
http://playground.arduino.cc/Code/AvoidDelay
Demonstration code for several things at the same time.
http://forum.arduino.cc/index.php?topic=223286.0
How to power a project:
https://learn.sparkfun.com/tutorials/how-to-power-a-project/all
Ladyada's Learn Arduino - Lesson #0:
Multitasking:
Part 1:
https://learn.adafruit.com/multi-tasking-the-arduino-part-1?view=all
Part 2:
https://learn.adafruit.com/multi-tasking-the-arduino-part-2?view=all
Part 3:
https://learn.adafruit.com/multi-tasking-the-arduino-part-3?view=all
Sparkfun Tutorials:
https://learn.sparkfun.com/tutorials?page=all
Micro Controllers:
https://learn.adafruit.com/mcus-how-do-they-work?view=all
Useful links:
https://forum.arduino.cc/index.php?topic=384198.0
Arduino programming traps, tips and style guide:
http://www.gammon.com.au/forum/?id=12153
Arduino programming course:
https://startingelectronics.org/software/arduino/learn-to-program-course/
Jeremy Blume:
https://m.youtube.com/playlist?list=PLA567CE235D39FA84
Arduino products:
https://www.arduino.cc/en/Main/Products
Motors/MOSFETs
http://www.gammon.com.au/motors
Making a library
https://www.arduino.cc/en/Hacking/libraryTutorial
Switches:
http://www.gammon.com.au/forum/?id=11955
Soldering FYI
https://m.youtube.com/watch?v=eBIjsZ1dcT0
https://m.youtube.com/watch?v=6PB0u8irn-4
https://m.youtube.com/watch?v=5uiroWBkdFY
https://m.youtube.com/watch?v=lpgMY1JeOAA
https://m.youtube.com/watch?v=_6tpQE7ptqo
https://m.youtube.com/watch?v=L_DIpklxXcI
https://m.youtube.com/watch?v=b15MMzb_GWw
Tips and Traps
http://www.gammon.com.au/forum/?id=12153
Share tips you have come across, 900+ posts:
https://forum.arduino.cc/index.php?topic=445951.0
Debug discussion:
https://forum.arduino.cc/index.php?topic=215334.msg1575801#msg1575801
Frequently Asked Questions:
https://www.arduino.cc/en/main/FAQ#toc10
SMD soldering:
SMD soldering
How to make and post a schematic:
Number 'type's.
You select the 'type' best suited for your variables.
ex:
char myText[ ] = {"Raspberry Pie Smells"};
Oh, and have fun too !
Arduino links of interest.
How to use this forum:
Getting started:
https://www.arduino.cc/en/Guide
Listing of downloadable 'Arduino PDFs' :
Either Google >>>- - - - > arduino filetype: pdf
Or
https://www.google.ca/search?q=arduino+filetype%3A+pdf&rlz=1C9BKJA_enCA739CA739&oq=arduino+filetype%3A+pdf&aqs=chrome..69i57j69i65.1385j0j7&hl=en-US&sourceid=chrome-mobile&ie=UTF-8
Listing of downloadable 'C++ PDFs' :
Either Google >>>- - - - > C++ filetype: pdf
Or
https://www.google.ca/search?q=c%2B%2B+filetype%3A+pdf&rlz=1C9BKJA_enCA739CA739&oq=c%2B%2B+filetype%3A+pdf&aqs=chrome..69i57.22790j0j7&hl=en-US&sourceid=chrome-mobile&ie=UTF-8
Arduino cheat sheet:
https://dlnmh9ip6v2uc.cloudfront.net/learn/materials/8/Arduino_Cheat_Sheet.pdf
Watch these:
Arduino programming syntax:
https://m.youtube.com/watch?v=CbJHL_P5RJ8
Arduino arithmetic operators:
https://m.youtube.com/watch?v=UUx0_s-ElSs
Arduino control flow:
https://m.youtube.com/watch?v=QpPGGuaGbCA
Arduino data types:
https://m.youtube.com/watch?v=xmZXWMEltEc
Understanding Destructive LC Voltage Spikes:
https://www.pololu.com/docs/0J16/all
OR
https://www.pololu.com/docs/pdf/0J16/destructive_LC_voltage_spikes.pdf
Why decoupling capacitors:
Some things to read
LCD information:
https://learn.adafruit.com/character-lcds?view=all
OR
https://cdn-learn.adafruit.com/downloads/pdf/character-lcds.pdf?timestamp=1573085286
Reading a schematic:
https://learn.sparkfun.com/tutorials/how-to-read-a-schematic
Language Reference:
https://www.arduino.cc/en/Reference/HomePage
Foundations:
https://www.arduino.cc/en/Tutorial/Foundations
How and Why to avoid delay():
http://playground.arduino.cc/Code/AvoidDelay
Demonstration code for several things at the same time.
http://forum.arduino.cc/index.php?topic=223286.0
How to power a project:
https://learn.sparkfun.com/tutorials/how-to-power-a-project/all
Ladyada's Learn Arduino - Lesson #0:
Multitasking:
Part 1:
https://learn.adafruit.com/multi-tasking-the-arduino-part-1?view=all
Part 2:
https://learn.adafruit.com/multi-tasking-the-arduino-part-2?view=all
Part 3:
https://learn.adafruit.com/multi-tasking-the-arduino-part-3?view=all
Sparkfun Tutorials:
https://learn.sparkfun.com/tutorials?page=all
Micro Controllers:
https://learn.adafruit.com/mcus-how-do-they-work?view=all
Useful links:
https://forum.arduino.cc/index.php?topic=384198.0
Arduino programming traps, tips and style guide:
http://www.gammon.com.au/forum/?id=12153
Arduino programming course:
https://startingelectronics.org/software/arduino/learn-to-program-course/
Jeremy Blume:
https://m.youtube.com/playlist?list=PLA567CE235D39FA84
Arduino products:
https://www.arduino.cc/en/Main/Products
Motors/MOSFETs
http://www.gammon.com.au/motors
Making a library
https://www.arduino.cc/en/Hacking/libraryTutorial
Switches:
http://www.gammon.com.au/forum/?id=11955
Soldering FYI
https://m.youtube.com/watch?v=eBIjsZ1dcT0
https://m.youtube.com/watch?v=6PB0u8irn-4
https://m.youtube.com/watch?v=5uiroWBkdFY
https://m.youtube.com/watch?v=lpgMY1JeOAA
https://m.youtube.com/watch?v=_6tpQE7ptqo
https://m.youtube.com/watch?v=L_DIpklxXcI
https://m.youtube.com/watch?v=b15MMzb_GWw
Tips and Traps
http://www.gammon.com.au/forum/?id=12153
Share tips you have come across, 900+ posts:
https://forum.arduino.cc/index.php?topic=445951.0
Debug discussion:
https://forum.arduino.cc/index.php?topic=215334.msg1575801#msg1575801
Frequently Asked Questions:
https://www.arduino.cc/en/main/FAQ#toc10
SMD soldering:
SMD soldering
How to make and post a schematic:
Number 'type's.
You select the 'type' best suited for your variables.
ex:
char myText[ ] = {"Raspberry Pie Smells"};
Oh, and have fun too !
Thank you. I have used this code below, but the 'previousValue' does not update on the serial monitor. It displays the first value and then does not change despite me changing the temperature.
// Initialise variables
int Temperature;
int previousValue;
void setup()
{
Serial.begin(9600); // Serial monitor comms
previousValue = map(((analogRead(A0) - 20) * 3.04), 0, 1023, -40, 125);
}
void loop()
{
analogRead(A0);
Temperature = map(((analogRead(A0) - 20) * 3.04), 0, 1023, -40, 125);
Serial.print(previousValue);
Serial.println();
Serial.print(Temperature);
}
Thank you. I have used this code below, but the 'previousValue' does not update on the serial monitor. It displays the first value and then does not change despite me changing the temperature.
// Initialise variables
int Temperature;
int previousValue;
void setup()
{
Serial.begin(9600); // Serial monitor comms
previousValue = map(((analogRead(A0) - 20) * 3.04), 0, 1023, -40, 125);
}
void loop()
{
analogRead(A0);
Temperature = map(((analogRead(A0) - 20) * 3.04), 0, 1023, -40, 125);
Serial.print(previousValue);
Serial.println();
Serial.print(Temperature);
}
[/quote]
// Initialize variables
int Temperature;
int previousValue;
void setup()
{
Serial.begin(9600); // Serial monitor comms
previousValue = map(((analogRead(A0) - 20) * 3.04), 0, 1023, -40, 125);
}
void loop()
{
analogRead(A0);
Temperature = map(((analogRead(A0) - 20) * 3.04), 0, 1023, -40, 125);
Serial.print(previousValue);
Serial.print(\t);
Serial.print(Temperature);
Serial.print(\t);
Serial.println( abs(previousValue - Temperature));
previousValue = Temperature;
}
Thanks for your help LarryD!
How am I then able to print if the temperature is increasing or decreasing? I have tried if, else if and else statement but it hasnt had any effect but I dont understand why.
if (Temperature>previousValue){
Serial.print("Up");
}
else if (Temperature<previousValue){
Serial.print("Down");
//}
else {
Serial.println();
}
// Initialize variables
int Temperature;
int previousValue;
void setup()
{
Serial.begin(9600); // Serial monitor comms
previousValue = map(((analogRead(A0) - 20) * 3.04), 0, 1023, -40, 125);
}
void loop()
{
analogRead(A0);
Temperature = map(((analogRead(A0) - 20) * 3.04), 0, 1023, -40, 125);
Serial.print(previousValue);
Serial.print(\t);
Serial.print(Temperature);
Serial.print(\t);
Serial.println( abs(previousValue - Temperature));
if (Temperature>previousValue)
{
Serial.print("Up");
}
else if (Temperature<previousValue)
{
Serial.print("Down");
}
else
{
Serial.println(“Same”);
}
previousValue = Temperature;
} //END of loop( )
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.