I'm new to this!!!!! making a giant cuckoo clock with rotating movement and a little guy popping out with clarinet. I've did the rt to rx hookup- gnd of the 20t the the nano- powered the nano with the usb and powered the 20t with a 12 volt supply- micro switch to pin2 and gnd.
this is my first sketch:
#include <SoftwareSerial.h>
#define RX_PIN 10 // Arduino RX (connect to DY-HV20T TX)
#define TX_PIN 11 // Arduino TX (connect to DY-HV20T RX)
#define BUTTON_PIN 2 // Button input pin
SoftwareSerial audioSerial(RX_PIN, TX_PIN);
const int totalTracks = 8;
int currentTrack = 1;
bool lastButtonState = HIGH;
unsigned long lastDebounceTime = 0;
const unsigned long debounceDelay = 50;
void setup() {
Serial.begin(9600);
audioSerial.begin(9600);
pinMode(BUTTON_PIN, INPUT_PULLUP);
delay(2000);
setVolume(30); // high volume
Serial.println("System ready. Waiting for button press...");
}
void loop() {
bool buttonState = !digitalRead(BUTTON_PIN); // Invert reading for NC button
if (buttonState != lastButtonState) {
lastDebounceTime = millis();
lastButtonState = buttonState;
}
if ((millis() - lastDebounceTime) > debounceDelay && buttonState == HIGH) {
playTrack(currentTrack);
currentTrack++;
if (currentTrack > totalTracks) {
currentTrack = 1;
}
while (!digitalRead(BUTTON_PIN)); // Wait for release
delay(100);
}
}
void playTrack(int trackNumber) {
Serial.print("Playing track number: ");
Serial.println(trackNumber);
audioSerial.write((uint8_t)0xAA); // start byte
audioSerial.write((uint8_t)0x07); // play command
audioSerial.write((uint8_t)0x02);
audioSerial.write((uint8_t)0x00);
audioSerial.write((uint8_t)trackNumber);
audioSerial.write((uint8_t)((0xAA + 0x07 + 0x02 + 0x00 + trackNumber) & 0xFF));
delay(1000); // wait after sending command
}
void setVolume(uint8_t volume) {
Serial.print("Setting volume to: ");
Serial.println(volume);
audioSerial.print("VOL ");
audioSerial.print(volume);
audioSerial.print("\r\n");
}
my second sketch:
#include <SoftwareSerial.h>
// Define the SoftwareSerial pins for communication with the DY-HV20T
#define DY_RX 10 // Connects to the DY-HV20T's TX pin
#define DY_TX 11 // Connects to the DY-HV20T's RX pin
SoftwareSerial dySerial(DY_RX, DY_TX);
// Define the button pin
#define BUTTON_PIN 2
// Button debouncing variables
long lastDebounceTime = 0;
long debounceDelay = 50; // milliseconds
// Command variables for the DY-HV20T
byte nextSongCommand[] = {0xAA, 0x06, 0x00, 0xB0}; // Correct command found in a DY-HV20T datasheet
void setup() {
// Start the Serial Monitor for debugging
Serial.begin(9600);
while (!Serial);
Serial.println("Arduino Ready");
// Start the software serial connection to the DY-HV20T
dySerial.begin(9600);
// Configure the button pin with the internal pull-up resistor
// This means the pin will be HIGH by default and LOW when the button is pressed.
pinMode(BUTTON_PIN, INPUT_PULLUP);
}
void loop() {
// Read the button state
int buttonState = digitalRead(BUTTON_PIN);
// Check for a button press (LOW signal due to INPUT_PULLUP) and debounce
if (buttonState == LOW) {
if ((millis() - lastDebounceTime) > debounceDelay) {
// Send the "next" command to the DY-HV20T
dySerial.write(nextSongCommand, sizeof(nextSongCommand));
Serial.println("Next song command sent.");
lastDebounceTime = millis(); // Reset the debounce timer
}
}
}
which is the correct sketch or are they both wrong? you help would be extremely appreciated.


