Hello,
I have 2 problems. First, on my private PC, I tried to run Arduino, but the port section was grayed out. I found out that I need the CH340 driver, so I downloaded it, but I still cannot select a port. Nevertheless, I am now using my school laptop instead.
Now to my main problem: I have a project, but the code doesn’t compile, and I get the exact error code. Regarding the functions: I use the funduino cube and a servo. The program uses a temperature sensor (DHT) to measure the temperature every 5 seconds. Depending on the measured temperature, different LEDs light up: the green LED indicates temperatures below 25°C, the yellow LED lights up for temperatures between 25°C and 29°C, and the red LED activates when the temperature reaches 30°C or higher. At 30°C or above, additional actions are triggered: the servo motor starts rotating 360° a warning message or danger symbol is displayed on the OLED screen, and a buzzer begins buzzing continuously. The buzzer stops automatically when the temperature drops below 30°C, and the LED behavior adjusts to reflect the new temperature range. And the button acts as a emergency button:
Now to my code: (with chatgpt) first the error code:
Arduino: 1.8.19 (Windows 10), Board: "Arduino Nano, ATmega328P (Old Bootloader)"
exit status 1
Error compiling for Arduino Nano board.
#include <Wire.h>
#include <DHT.h>
#include <SSD1306Ascii.h>
#include <SSD1306AsciiWire.h>
#include <Servo.h>
#define DHTPIN A3 // DHT11 Sensor Pin
#define DHTTYPE DHT11
#define LED_GREEN 10 // Green LED
#define LED_YELLOW 11 // Yellow LED
#define LED_RED 12 // Red LED
#define SPEAKER 7 // Speaker
#define BUTTON 9 // Emergency Stop Button
SSD1306AsciiWire display; // SSD1306Ascii Display with Wire (I2C)
DHT dht(A3, DHT11);
Servo fanServo;
bool emergencyMode = false;
void setup() {
pinMode(LED_GREEN, OUTPUT);
pinMode(LED_YELLOW, OUTPUT);
pinMode(LED_RED, OUTPUT);
pinMode(SPEAKER, OUTPUT);
pinMode(BUTTON, INPUT_PULLUP); // Button pin as input with pullup
dht.begin();
fanServo.attach(A1); // Attach servo to pin A1
fanServo.write(0); // Set servo to starting position
// Initialize display
Wire.begin();
display.begin(SSD1306_128X64, 0x3C); // Initialize SSD1306 display
display.setFont(System5x7); // Standard font
display.clear();
display.display();
}
void loop() {
static unsigned long lastReadTime = 0;
// Check button
if (digitalRead(BUTTON) == LOW) { // Button pressed
activateEmergencyMode(); // Activate emergency stop mode
return; // Skip everything else
}
if (emergencyMode) {
// Activate emergency mode
activateEmergencyMode();
} else {
// Regular mode
if (millis() - lastReadTime >= 5000) { // Read temperature every 5 seconds
lastReadTime = millis();
float temp = dht.readTemperature();
// Check temperature
if (temp >= 30) {
activateDangerState(temp);
} else if (temp >= 25) {
activateWarningState(temp);
} else {
activateNormalState(temp);
}
}
}
}
// Functions
void activateDangerState(float temp) {
setLEDState(HIGH, LOW, LOW);
digitalWrite(SPEAKER, HIGH);
fanServo.write(90); // Turn servo to 90°
displayWarning(temp, "Danger!");
}
void activateWarningState(float temp) {
setLEDState(LOW, HIGH, LOW);
digitalWrite(SPEAKER, LOW);
fanServo.write(0); // Reset servo to starting position (0°)
displayWarning(temp, "Caution");
}
void activateNormalState(float temp) {
setLEDState(LOW, LOW, HIGH);
digitalWrite(SPEAKER, LOW);
fanServo.write(0); // Reset servo to starting position (0°)
displayWarning(temp, "Normal");
}
void activateEmergencyMode() {
// Emergency stop: Turn everything off except the red LED
setLEDState(HIGH, LOW, LOW); // Only red LED on
digitalWrite(SPEAKER, LOW); // Turn speaker off
fanServo.write(0); // Reset servo to starting position
displayMessage("EMERGENCY!");
}
void setLEDState(bool red, bool yellow, bool green) {
digitalWrite(LED_RED, red);
digitalWrite(LED_YELLOW, yellow);
digitalWrite(LED_GREEN, green);
}
void displayWarning(float temp, const char *message) {
display.clear();
display.setCursor(0, 0);
display.print("Temp: ");
display.print(temp);
display.println(" C");
display.println(message);
display.display();
}
void displayMessage(const char *message) {
display.clear();
display.setCursor(0, 0);
display.setTextSize(2); // Larger font for emergency messages
display.println(message);
display.display();
}
Thanks for everyones help!

