Hello everyone, I'm new to coding and working on a school project where we're building an escape room using a microcontroller. For one puzzle, I've created a candle using cardboard, and used a glue stick to make it look like the flame on the candle and put a small LED inside and programmed it so that it mimics the flickering of the the candle and put the DHT11 inside the candle so that when you blow the candle it reaches the DHT11 aswell.
When you blow on the candle, it triggers a sensor inside, turning off the candle's LED and activating a laser. But here's the problem: I need to power the whole thing with a power supply in the escape room, not my laptop. This means I can't check the code on my laptop using the Serial Monitor. I'm worried that without monitoring, the code might not work as expected due to a full Serial Buffer. I've tried looking for solutions online, but I haven't found anything helpful. Can anyone give me some advice? Thanks a lot!
//This code aims to control an LED, designed to mimic a candle, and a laser. When the candle's flame is blown out (as detected by the DHT sensor inside the candle, with a reading exceeding 30), the LED will turn off, and the laser will turn on. Subsequently, the laser will stay on regardless of the sensor's readings.
#include <DHT.h>
#include <DHT_U.h>
// Pin configuration
int DHTPin = 5; // Digital pin connected to DHT sensor
int DHTType = DHT11; // Type of DHT sensor (DHT11 in this case)
int D = 1000; // Delay time in milliseconds
int LaserPin = 2; // Digital pin connected to the laser module
boolean laserOn = false; // Flag to indicate if the laser is on
void setup() {
pinMode(10, OUTPUT); // Set pin 10 as an output pin
Serial.begin(9600); // Initialize serial communication at 9600 baud
dht.begin(); // Initialize DHT sensor
pinMode(LaserPin, OUTPUT); // Set laser pin as an output
}
// Function to turn on the laser
void turnOnLaser() {
digitalWrite(LaserPin, HIGH); // Turn on the laser by setting its pin high
laserOn = true; // Update flag to indicate laser is on
}
// Function to turn off the laser
void turnOffLaser() {
digitalWrite(LaserPin, LOW); // Turn off the laser by setting its pin low
laserOn = false; // Update flag to indicate laser is off
}
// Main loop function, runs repeatedly
void loop() {
// Read humidity from DHT sensor
float humidity = dht.readHumidity();
// Print humidity to serial monitor
Serial.print("Humidity: ");
Serial.print(humidity);
Serial.println("%");
// Check if humidity is above 30%
if (humidity > 30) {
// If laser is not already on, turn it on and set pin 10 to low
if (!laserOn) {
turnOnLaser();
analogWrite(10, LOW);
}
}
else {
// If laser is on, set pin 10 to low
if (laserOn) {
analogWrite(10, LOW);
}
else {
analogWrite(10, random(110) + 135); // This code is designed to simulate the flickering of a candle flame using a LED.
delay(random(10));
}
}
}