wanting to stop button hold from looping Serial.Write

Hi folks, I've tried numerous libraries and strings of code with no luck.

I'm running a nano into a WT5001-28p mp3 player board which relies on the Serial.Write to send packets to control it.

I've got it running the 3 audio tracks correctly and firing when buttons are pressed, but I need the reload sequence (track3) to only play once even when the button is held for an infinite amount of time. (to imitate the cartridge being loaded) BUT can be overwritten by other audio commands. i.e fire, charging up etc.

please see below the (fairly short code) of the complete sketch.:

const int buttonPinFire = 7;   // FIRE
const int buttonPinBlast = 8;
const int buttonPinBang = 9;

// variables will change:
int buttonState = 0;         // variable for reading the pushbutton status

void setup() {

   // initialize the button pin as a input:
pinMode(buttonPinFire, INPUT);
pinMode(buttonPinBlast, INPUT);

// initialize serial communication:
   Serial.begin(9600);
   

// this is initiation sound on startup (track2 on SD card)  
   Serial.write(0x7E);
   Serial.write(0x03);
   Serial.write(0xA7);
   Serial.write(0x19); //  volume max
   Serial.write(0x7E);
   
   // start sound
   Serial.write(0x7E);
   Serial.write(0x04);
   Serial.write(0xA0); // A0 for SD card
   Serial.write((byte)0x00);
   Serial.write(0x02); // track number
   Serial.write(0x7E);
   delay(3500);
   
   }
   void loop()
   
{
                     
//this is the fire sequence audio (track 1 on SD card) pin7
   Serial.write(0x7E);
   Serial.write(0x02); //STOP Sound
   Serial.write(0xA4);
   Serial.write(0x7E); 


 buttonState = digitalRead(buttonPinFire);
if (buttonState == HIGH) {

   Serial.write(0x7E);
   Serial.write(0x04);
   Serial.write(0xA0); // A0 for SD card
   Serial.write((byte)0x00);
   Serial.write(0x01); // track number
   Serial.write(0x7E);
delay(1800);
}
// this is the reload sequence audio (track 3 on SD card) pin8 

   Serial.write(0x7E);
   Serial.write(0x02); //STOP Sound
   Serial.write(0xA4);
   Serial.write(0x7E); 

 buttonState = digitalRead(buttonPinBlast);
if (buttonState == HIGH) {

   Serial.write(0x7E);
   Serial.write(0x04);
   Serial.write(0xA0); // A0 for SD card
   Serial.write((byte)0x00);
   Serial.write(0x03); // track number
   Serial.write(0x7E);
delay(1800);

}
         }

It's the last section of code in the sketch. any insight as to what I need to be doing would be greatly appreciated.

WastedProps

WastedProps: Hi folks, I've tried numerous libraries and strings of code with no luck.

No surprise, because that would be the same as buying a lottery ticket and expecting to win ;)

But have a look at the state change example. Or, if you want easy and clean, use the Bounce2 library :)

septillion: No surprise, because that would be the same as buying a lottery ticket and expecting to win ;)

Ha ha, I attempted to use debounce, but I kept having various errors, so I gave up thinking I was looking at the wrong possible solution.

I'll look at the debounce2 library tomorrow when I have time after work, I'm VERY new to arduino and the fact that I'm jumping into Serial outputting makes me feel like i'm very much in the deep end (though I'm sure in the big scheme of things that isn't true)

without looking into it tonight, will it still let me do override commands without it thinking that the button is being pushed at the same time.?

WastedProps: I'll look at the debounce2 library

Bounce2 ;)

And Serial outputting isn't that deep in. That's just touching the surface because Arduino made that very very simple with the Serial class (aka Serial.print() ) and the Serial Monitor.

And I have no idea what you mean by the last part. The problem you have now is that you have no difference between a button that IS pressed and a button that BECOMES pressed :) If you only want to act once you need to act on the button becoming pressed ;) Bounce2 is just an easy library for that including debouncing (a buttons is not ideal and will not instantaneous just move between HIGH and LOW).

septillion: Bounce2 ;)

Whoops, you know what I meant...

septillion: And I have no idea what you mean by the last part.

I've put together a flowchart of what I'm trying to achieve... when the energy cell is installed, I want it to play the reload track only once and be able to have the other audio play as normal

|471x500

I hope this makes sense

Yeah, just only trigger it when you you detect a falling switch and done. I assume the other tracks are triggered by other buttons?

septillion: I assume the other tracks are triggered by other buttons?

Correct, the trigger will have a two way sense, so one sound for trigger high, and one for trigger release.

This is a mud-map style layout of my circuit. (wires aren't designated to location of pins, didn't have time)

|369x500

No need for resistors in line with the button or reed :) And connect them to GND instead of Vcc (at least I guess a red wire means Vcc) so you don't need an external pull up resistor.