I'm building a climate control that opens and closes servos to introduce warm or cold air. I'm connecting it to a nextion display and so far it's going well but I'm stuck trying to understand how to pass variables between functions. I've set up a few global variables but one doesn't seem to be updating. frontcabintemp. I can get it to print out of the section where it is populated with a DHT11 but I can not use the variable value in any of the other functions.
#include <DHT.h> // temp sensor libraries
#define DHTPIN 7 // Digital pin connected to the DHT sensor
#define DHTTYPE DHT11 // Needed to define the type of sensor being used - DHT11
DHT dht(DHTPIN, DHTTYPE);
// This section is for the servo
#include <Servo.h> // servo library
Servo frontheatservo; // create servo object to control the front heat servo
Servo frontcoolservo; // create servo object to control the front cool servo
//Servo backheatservo; // create servo object to control the back heat servo
//Servo backcoolservo; // create servo object to control the back cool servo
// setting global variables
String endChar = String(char(0xff)) + String(char(0xff)) + String(char(0xff)); // Nextion display expects 0xFF three times when sending. This automates that addition.
int frontsettemp = 72; // default front cabin temp
int frontheatservoval;
int frontcoolservoval;
int frontcabintemp;
void setup() {
Serial.begin(9600);
Serial1.begin(9600); // serial connection between the mega and the display
dht.begin();
frontheatservo.attach(9); // attaches the servo on pin 9 to the servo object
frontcoolservo.attach(10); // attaches the servo on pin 10 to the servo object
}
// start of main loop
void loop() {
delay(500);
Serial1.print("page0.n0.val=" + String(frontsettemp) + endChar); // this prints the frontsettemp to the touch display
measurefronttemp(); // this calls the measurefronttemp function
grabscreendata(); // this calls the screen reader
movefrontservos(); // this calls the move front servos section
}
// this section reads the front cabin temp from a DHT11
void measurefronttemp() {
// Read temperature as Fahrenheit (isFahrenheit = true)
int frontcabintemp = dht.readTemperature(true);
// Check if any reads failed and exit early (to try again).
if (isnan(frontcabintemp)) {
Serial.println(F("Failed to read from DHT sensor!"));
return;
}
Serial1.print("page0.n1.val=" + String(frontcabintemp) + endChar); // this writes the frontcabintemp to the screen.
}
// this section creates an empty string variable and grabs the text from the display out of the serial1 channel
void grabscreendata() {
if (Serial1.available()) {
String data_from_display = "";
delay(30);
while (Serial1.available()) {
data_from_display += char(Serial1.read());
}
Serial.println(data_from_display);
sendData(data_from_display);
}
}
// this section increases or decreases the frontsettemp based on what's returned from the dispaly in grabscreendata
void sendData(String data_from_display) {
if (data_from_display == "frontsettemp_increase") {
frontsettemp = frontsettemp + 1;
}
if (data_from_display == "frontsettemp_decrease") {
frontsettemp = frontsettemp - 1;
}
}
void movefrontservos() {
Serial.println("------------------------");
Serial.println(frontcabintemp); // this will not populate not matter what I do?
Serial.println(frontsettemp);
// this is the front heating side
if ((frontsettemp >= frontcabintemp + 20) && (frontsettemp <= frontcabintemp + 100)) {
frontheatservoval = 180;
frontheatservo.write(frontheatservoval);
frontcoolservo.write(0);
}
else if ((frontsettemp >= frontcabintemp + 10) && (frontsettemp <= frontcabintemp + 19.99)) {
frontheatservoval = 90;
frontheatservo.write(frontheatservoval);
frontcoolservo.write(0); {
}
}
else if ((frontsettemp >= frontcabintemp + 5) && (frontsettemp <= frontcabintemp + 9.99)) {
frontheatservoval = 20;
frontheatservo.write(frontheatservoval);
frontcoolservo.write(0); {
}
}
else if ((frontsettemp >= frontcabintemp + 1) && (frontsettemp <= frontcabintemp + 4.99)) {
frontheatservoval = 10;
frontheatservo.write(frontheatservoval);
frontcoolservo.write(0); {
}
}
else if ((frontsettemp >= frontcabintemp) && (frontsettemp <= frontcabintemp + .99)) {
frontheatservoval = 0;
frontheatservo.write(frontheatservoval);
frontcoolservo.write(0); {
}
}
else if ((frontsettemp <= frontcabintemp) && (frontsettemp >= frontcabintemp - .99)) {
frontcoolservoval = 0;
frontcoolservo.write(frontcoolservoval);
frontheatservo.write(0); {
}
}
else if ((frontsettemp <= frontcabintemp - 1) && (frontsettemp >= frontcabintemp - 4.99)) {
frontcoolservoval = 10;
frontcoolservo.write(frontcoolservoval);
frontheatservo.write(0); {
}
}
else if ((frontsettemp <= frontcabintemp - 5) && (frontsettemp >= frontcabintemp - 9.99)) {
frontcoolservoval = 20;
frontcoolservo.write(frontcoolservoval);
frontheatservo.write(0); {
}
}
else if ((frontsettemp <= frontcabintemp - 10) && (frontsettemp >= frontcabintemp - 19.99)) {
frontcoolservoval = 90;
frontcoolservo.write(frontcoolservoval);
frontheatservo.write(0); {
}
}
else if ((frontsettemp <= frontcabintemp - 20) && (frontsettemp >= frontcabintemp - 100)) {
frontcoolservoval = 180;
frontcoolservo.write(frontcoolservoval);
frontheatservo.write(0); {
}
}
}