#define LED_ALERT PA5
#define LED_NORMAL PA6
const float THRESHOLD_HIGH = 40.0;
float currentTemp = 25.0;
bool tempRising = true;
int messageCount = 0;
void setup() {
Serial.begin(9600);
pinMode(LED_ALERT, OUTPUT);
pinMode(LED_NORMAL, OUTPUT);
digitalWrite(LED_ALERT, LOW);
digitalWrite(LED_NORMAL, LOW);
}
void loop() {
messageCount++;
// Simulate temperature wave 20C to 55C
if (tempRising)
{
currentTemp += 1.5;
if (currentTemp >= 55.0) tempRising = false;
}
else {
currentTemp -= 1.5;
if (currentTemp <= 20.0) tempRising = true;
}
// Determine alert status
bool isAlert = (currentTemp > THRESHOLD_HIGH);
if (isAlert == true) {
digitalWrite(LED_ALERT, HIGH); // RED on
digitalWrite(LED_NORMAL, LOW); // GREEN off
} else {
digitalWrite(LED_ALERT, LOW); // RED off
digitalWrite(LED_NORMAL, HIGH); // GREEN on
}
// Send UART message
Serial.print("[MSG ");
if (messageCount < 10) Serial.print("0");
Serial.print(messageCount);
Serial.print("] ");
Serial.print("TEMP: ");
Serial.print(currentTemp, 1);
Serial.print(" C | STATUS: ");
if (isAlert) {
Serial.println("*** WARNING — OVER TEMPERATURE *** | LED: RED ON");
} else {
Serial.println("Normal | LED: GREEN ON");
}
delay(800);
}Loading
st-nucleo-c031c6
st-nucleo-c031c6