How can i make this program work without using the serial monitor?

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)); 
    }
  }
}

A full serial buffer won't harm your code because the serial buffer is fixed and excess is discarded.

Anyway, you could even define a symbol to turn serial output on or off, like:

// Use "false" to disable serial printouts
bool isDebug = true;

then enclose the prints inside an if():

  // Print humidity to serial monitor
  if (isDebug) {
    Serial.print("Humidity: ");
    Serial.print(humidity);
    Serial.println("%");
  }

This means you could change "isDebug" value to "false" just before uploading the "production" version while you will keep it to "true" for your debug purposes.

There are also many other ways to do that, ranging from using a specific compiler directive, up to using a specific EEPROM byte to tell the code if keep serial output or not (e.g. reading it to "isDebug" variable instead of that fixed starting value like before).

1 Like

What type Arduino are you using?

Generally the Arduino has no way of telling if it is connected to a USB port or not. The data will be send out the Serial port whether or not there is anything on the other end to receive it, so your code will not run any differently. The exception to this would be a board that has the USB interface integrated into the processor itself, such as the Leonardo board, in which case you usually see "while (!Serial)" in Setup() to make sure the code does not send any Serial data before the USB connection is established.

1 Like

It worked thank you very much for your help, I've been stressing about this problem for few days now thank you really!

I'm using Arduino Uno but it's alright now I've fixed the problem thank you anyway!

And if you decide to connect the PC and open the serial port and after that disconnect it, you have a good chance of bringing your board to a near grinding halt if you keep on printing.

1 Like

Your problem not a problem is solved.

You could, in cases like this, fearlessly experiment. Perhaps with a bit of extra information.

The serial buffer holds N bytes. Research: how big is the serial buffer?

How long would it take to fill it up? Analysis: what gets printed and how often? Rough estimates will do.

Me and my friends play the game for ten times that period? Experiment: see what happens… does this come to a grinding halt right about on schedule?

It's not called Computer Science just because it sounds cool. :wink:

a7

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.