07-28-2025 09:11 PM
Hello,
I'm running into an issue when running the attached VI. Seem to not be able to get this VI to function. Arduino code was uploaded successfully, but there seems to be a timing issue when running the VI in Labview. Any help is appreciated. Thanks.
Arduino Code:
#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_AHTX0.h>
// Relay output pins
const int heaterRelayPin = 7;
const int fanRelayPin = 8;
// AHT20 sensor object
Adafruit_AHTX0 aht;
// Control variables
float setTemp = 22.0; // °C
float currentTemp = 0.0;
const float deadBand = 2.0; // °C
// Relay states for status messaging
bool heaterOn = false;
bool fanOn = false;
// Function prototypes
void checkSerialCommands();
void readTemperature();
void controlHVAC();
void sendTemperature();
void sendSetpointStatus();
void setup() {
Serial.begin(9600);
while (!Serial) { /* wait for Serial on Leonardo/Micro */ }
Wire.begin();
pinMode(heaterRelayPin, OUTPUT);
pinMode(fanRelayPin, OUTPUT);
digitalWrite(heaterRelayPin, LOW);
digitalWrite(fanRelayPin, LOW);
if (!aht.begin()) {
Serial.println("STATUS:ERROR - AHT20 sensor not found");
while (1) delay(10);
}
Serial.println("STATUS:HVAC Controller Ready");
sendSetpointStatus();
}
void loop() {
checkSerialCommands();
readTemperature();
controlHVAC();
sendTemperature();
delay(2000);
}
void checkSerialCommands() {
while (Serial.available()) {
String line = Serial.readStringUntil('\n');
line.trim();
if (line.startsWith("SET TEMP:")) {
float val = line.substring(9).toFloat();
if (val > 0 && val < 50) {
setTemp = val;
sendSetpointStatus();
} else {
Serial.println("STATUS:ERROR - Invalid setpoint value");
}
} else {
Serial.print("STATUS:ERROR - Unknown command: ");
Serial.println(line);
}
}
}
void readTemperature() {
sensors_event_t humidityEvent, tempEvent;
aht.getEvent(&humidityEvent, &tempEvent);
currentTemp = tempEvent.temperature;
}
void controlHVAC() {
// Too cold → heater ON, fan OFF
if (currentTemp < setTemp - deadBand) {
if (!heaterOn) {
digitalWrite(heaterRelayPin, HIGH);
heaterOn = true;
Serial.println("STATUS:Heater ON");
}
if (fanOn) {
digitalWrite(fanRelayPin, LOW);
fanOn = false;
Serial.println("STATUS:Fan OFF");
}
// Too hot → fan ON, heater OFF
} else if (currentTemp > setTemp + deadBand) {
if (!fanOn) {
digitalWrite(fanRelayPin, HIGH);
fanOn = true;
Serial.println("STATUS:Fan ON");
}
if (heaterOn) {
digitalWrite(heaterRelayPin, LOW);
heaterOn = false;
Serial.println("STATUS:Heater OFF");
}
// Within deadband → both OFF
} else {
if (heaterOn) {
digitalWrite(heaterRelayPin, LOW);
heaterOn = false;
Serial.println("STATUS:Heater OFF");
}
if (fanOn) {
digitalWrite(fanRelayPin, LOW);
fanOn = false;
Serial.println("STATUS:Fan OFF");
}
}
}
void sendTemperature() {
Serial.print("TEMP:");
Serial.print(currentTemp, 2);
Serial.println();
}
void sendSetpointStatus() {
Serial.print("STATUS:Setpoint updated to ");
Serial.print(setTemp, 2);
Serial.println();
}
07-28-2025 09:15 PM
The error occurs after inputting setpoint while running the VI.
07-28-2025 09:16 PM
07-29-2025 04:00 PM
There are a few issues with that code.
I think the error is associated with the fact that you cannot Write and Read in parallell to the Arduino. You have to execute commands to the Arduino in series.
Next, I'm assuming your Send Setpoint is a button. Its value is never updated in the while loop, so you might set the setpoint once on first execution. If the control was inside the while loop it would be checked every itteration.
Those time delays are not executing in any beneficial way. The error needs to run through the sequence with the time delay.
If you attache the VI, and not an image of the block diagram, then someone might edit things to show you a better way to do things.
Craig
07-29-2025 11:13 PM
Thanks Craig. VI file is attached on the initial post, directly under the code.
07-30-2025 10:49 AM
Sorry, missed the attached VI.
I've created a simple state machine from the code in your VI.
Watch how it runs in Highlight Execution mode and then can modify as needed.
Craig