I am a beginner when it comes to coding. I have worked with systems where I need to teach myself enough to get by and generally, I devote time to a self-teaching effort before I ask for help. In this case I have several hours invested and so hopefully someone knowledgeable will feel that I have suffered enough in silence and may be willing to help. ![]()
I built the Arduino nano/DFRobotDFPlayer mini centered "fireplace sound project" described here and it works as expected:
Brief description of my variation for the project . . .
I intend to have the fireplace sound system play when the gas valve opens and stop playing when the gas valve shuts down. The gas valve is energized when 1.6 VDC is applied and the "trigger voltage quickly drops to 0.3 VDC to hold the valve open. When the voltage drops to "0" the valve closes and the flame goes out.
I built the system with the intention to use the gas valve voltage change to turn the sound system on or off. My thought is that a series 10K Ohm resistor to Pin A0 set as analog input will be the interface point.
I have run into two issues:
I've been able to monitor the input to the A0 pin and it floats high when unterminated and so it will need to be pulled down. This is a lesser concern at the moment.
I have experimented with the code and I have used AI programming tools available online to attempt to generate code that I could try and then analyze, as well as searched here and elsewhere and so far . . . no joy.
Here is the latest code:
#include "Arduino.h"
#include "SoftwareSerial.h"
#include "DFRobotDFPlayerMini.h"
SoftwareSerial mySoftwareSerial(10, 11); // RX, TX
DFRobotDFPlayerMini myDFPlayer;
// Define the analog input pin
#define ANALOG_PIN A0
float voltage;
// Define the threshold voltage
float threshold = 0.3;
void setup() {
mySoftwareSerial.begin(9600);
// Initialize serial communication
Serial.begin(9600);
Serial.println();
Serial.println(F("Initializing ..."));
if (!myDFPlayer.begin(mySoftwareSerial)) {
Serial.println(F("Unable to begin: recheck the connection or insert an SD card "));
while (true);
}
Serial.println(F("DFPlayer Mini online."));
delay (1000);
// Set the volume to maximum
myDFPlayer.volume(30);
// Print a message to indicate setup is complete
Serial.println("Setup complete.");
myDFPlayer.enableLoopAll();
}
void loop() {
// Read the analog input
float analogValue = analogRead(ANALOG_PIN);
// Convert the sensor value to voltage
float voltage = analogValue * (5.0 / 1023.0);
//Print the voltage to serial monitor
Serial.println(voltage);
// Delay for 1000 milliseconds
delay(1000);
// Check if the voltage is greater than the threshold
if (voltage > threshold) {
//Play all continuously
myDFPlayer.play();
}else{
//Stop playing
myDFPlayer.stop();
}
}
It took me a while to realize that the "delay" in the loop equally affected the voltage monitoring and the play function. Originally it was set for 100 msec and there was no sound at all regardless of A0 input state.
Once I began to play with delay time, increasing it, I discovered that I would get no sound when A0 was low, but sound would play in snippets when A0 was high. 1 second, 10 seconds or any other delay setting provided a screen print of the polled voltage rate on A0 but also determined the maximum play time.
I.E. when delay was set to 5 seconds, voltage measurements would occur or pole every five seconds and snippets of audio would play for a duration of 5 seconds, stopping and restarting at the next pole/or voltage measurement, so long as A0 was > 0.3 VDC which was nearly continuously.
I went down the rabbit hole of trying two sequential loops after learning that Arduino nano does not support multiple loops only to discover that any delay statement effects all sub-loops and I was back to "square one."
I have exhausted a number of approaches.
Assistance will be gratefully appreciated.
Thanks in advance for any help I may receive.