Using nano to activated up to 8 songs on a dyhv20t after using a micro switch

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.

I did not use a resistor on my tx pin.

Why not start by launching small projects and gain experience?

I depends on the schematics You didn’t post.

Members reading and using this link usually get more precise answers: How to get the best out of this forum - Development Tools / IDE 1.x - Arduino Forum

Welcome!

Request for Detailed Information Hardware & Software

I’m not sure I can help without seeing exactly what you have in front of you. To assist you effectively, please provide the following:

Addressing Your Issue:

You can spend weeks spinning your wheels, or you might get lucky and solve your problem quickly. To avoid unnecessary delays, it’s crucial to provide an annotated schematic of your circuit as you have it wired, showing all connections, including power, ground, and power supplies. I recommend it be in English, you can translate before posting if needed.

Why Detailed Information Matters:

Annotated Schematics: These are essential because they show exactly how your circuit is set up. Without them, it's difficult for anyone to understand what you’ve done, which makes troubleshooting nearly impossible. Fritzing diagrams or unclear pictures are not enough.

Technical Information: Many modules look similar and may even have the same name, but they can function differently. This is why we always ask for links to detailed technical information, not just sales pages like those on Amazon, which often lack the specifics we need. Be sure to tell us which Arduino you are using and what you have selected in the tools menu.

Show All Connections: It’s important to include every connection, especially power, ground and power sources in your schematic. Missing these details makes it hard to determine if a setup issue might be causing your problem.

Posting Errors: I post them using code tags. I simply select the complete printout enter a ctl C to capture then in code tags I paste it. Screen shots are normally not readable.

My Process:

When I see a question, I spend a moment assessing it. If it’s missing critical information, I might ask for it. However, if it's repeatedly lacking important details, I may assume the questioner is not serious and move on to another query.

What You Need to Consider:

We don’t know your skill level or what resources you have available. If you’re missing key technical details or seem unprepared, it may indicate that you need to spend more time learning the basics before starting your project.

Providing the right information upfront will help you get the best possible assistance and avoid the frustration of running into dead ends. Let us help you by sharing what you have clearly and completely!

Try reading this: How to get the best out of this forum , it appears you have not read it at this point.

here is the code I used: #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");
}

annotated schematic:

Your code is a word salad! The labels on your ‘schematic’ for the most part are not readable. Try black on white paper.

Before posting your code and schematic, please review the forum guidelines and use code tags to ensure clarity. Additionally, include links to the technical information for your hardware and provide a preliminary schematic that shows all power, ground, and power sources.
For more guidance, please refer to this link:
• How to Get the Best Out of This Forum
Important Notes:
• Breadboard diagrams (often referred to as "frizzes") are not considered proper schematics in this context.
• Wiring diagrams are helpful for assembly but are not effective for debugging.
• Screenshots are discouraged because they are often hard to read. Please provide text-based code and clear, high-quality images of schematics.
Providing the correct information will help others understand your setup and offer better assistance.

Here is a photo of my nano project:

my second attempt at the annotated schematic:

I realize my code is word salad. I copied the code from the guy that does Monster Lab. I have never coded for an Arduino Nano ( just learned how to spell Arduino --and say it!) I have been working on this project since January -- a 12 foot tall by 8 foot wide GIANT Cuckoo clock that will be used during the Midwest Band and Orchestra clinic In December. I have hundreds of hours in this thing and I and this is the last piece of the puzzle. ( by the way I'm not making a dam dime off of this---I'm trying to help my daughter)There will be a turntable rotating with six 12 inch tall nutcracker figures holding musical instruments and a clarinet payer that pops out at the top. the ideas to have band march's playing every hour on the hour. This is where the DY-HV20 T and the Arduino Nano come into play. Now with that said to be honest this will be a one off for me regarding the programing . I'm 71 years old and have done more creating from a frame off restoration of a 1952 five window GMC pickup to building both of my home from scratch. I taught shop and placed students out in the workforce for 37 years -- oh and my Super mileage team won back to back Shell Eco marathons back in 2012 and 2013, All this to say I know stuff and have been there and done that! I also know if I don't know something to go and ask for help. When I ask for help I that means I've tried everything I know to solve the problem. So can you help me or should I Punt.

And I did read the guidelines, got on Arduinao IDE, went to "Tools" and auto formatted, and then copied and pasted the code to the forum.

further info: The green light is on on the DY-HV20T and the other green light is on the Nano. When I reset the Nano the yellow light flickers but when I touch the micro switch no light blinks on the Nano and no sound from the 20T. I have tried off brand Nanos and when I touch the micro switch the red light flickers but no sound. Now you know everything I know and don't know.

Just print the value being read from the BUTTON_PIN and pause for a few seconds.

I never looked at it, you did not post using code tags and it is very difficult to follow. Putting it in code tabs helps a lot. The schematic is now readable and the code is great, thanks.

Very good for a first sketch.

I think @Paul_KD7HB found your code problem and all looks OK. When you get this finished post a picture, it sounds like a real cool project. If anything else comes up feel free to ask.

I wonder what your next project will be?

HAHA------- I'm wondering the same thing----but my wife isn't! Redo the bathroom and put a water feature by the front porch.
Didn't tell you but this will be on display at McCormick Place from Dec17th to the 19th at the end of the concourse. Only about 18,000 people attend this. conference.

thanks for your help -- Joe -

So can I just copy and paste the code,and then upload to the nano? Remember, "I know just enough to be dangerous". Someday , if you remind me, I'll tell you a story tied to that phrase---funny story about the guy that built the Global Flyer and the Voyager that circumvented the earth. True story too.

how do I get the value from the Button Pin and and insert it into my code--where do I insert it?

Make a global int variable. Digital read the pin into that variable. Print the variable and then delay so you can read the printed value.

Temporarily replace the lines of your code that I copied for you.

"Just print the value being read from the BUTTON_PIN and pause for a few seconds".Don't understand what this means. I know it is in the coding but it's Greek to me.

Then time to learn.

I'm learning--feels like I'm drinking from a fire hose. I've been trying to learn by using AI but when you don't know what you don't know it gets perplexing and confusing. Iv'e been working on this last part of my project for 3 weeks trying to get that dam DY-HV20T module to take it's cue from the Nano. Programing is not my strong suit to say the least. I'll send you what I sent Gilshultz that explains my background and frustration.
Joe_KD9DBP
I realize my code is word salad. I copied the code from the guy that does Monster Lab. I have never coded for an Arduino Nano ( just learned how to spell Arduino --and say it!) I have been working on this project since January -- a 12 foot tall by 8 foot wide GIANT Cuckoo clock that will be used during the Midwest Band and Orchestra clinic In December. I have hundreds of hours in this thing and I and this is the last piece of the puzzle. ( by the way I'm not making a dam dime off of this---I'm trying to help my daughter)There will be a turntable rotating with six 12 inch tall nutcracker figures holding musical instruments and a clarinet payer that pops out at the top. the ideas to have band march's playing every hour on the hour. This is where the DY-HV20 T and the Arduino Nano come into play. Now with that said to be honest this will be a one off for me regarding the programing . I'm 71 years old and have done more creating from a frame off restoration of a 1952 five window GMC pickup to building both of my home from scratch. I taught shop and placed students out in the workforce for 37 years -- oh and my Super mileage team won back to back Shell Eco marathons back in 2012 and 2013, All this to say I know stuff and have been there and done that! I also know if I don't know something to go and ask for help. When I ask for help I that means I've tried everything I know to solve the problem. So can you help me or should I Punt.