How to make a sound file play instantly when a button is pressed & held

Over the last couple of weeks I've put together a sketch using an Arduino Uno, which i’ll swap out later for the smaller Nano. The sketch controls Leds and their sequence using states, a small servo motor & sound effects controlled by 2 momentary buttons

It basically has three stages.

  1. No buttons pressed, servo doesn't move and leds blink slowly
  2. Button A pressed, servo moves 35 degrees and leds blink slightly faster
  3. Button B pressed, servo moves 83 degrees and leds blink really fast

The sketch works but i'm having an issue with the sound effects

[code]
/*
    ===================================================================================================
    ==================================== BB-8 PADAWAN DOME ============================================
    ======================================  Version 0.07  =============================================
    ===================================================================================================

    Version History
    0.01 - Original Sketch by DavidScott
    0.02 - r0n_dL added annotations & variable for PIN_sound_BUSY as suggested by DavidScott
    0.03 - r0n_dL reassigned pins to align with Padawan control system
         - Changed SoftwareSerial pins (RX/TX) 10/9 to 8/4
    0.04 - Change pinMode for PIN_trigger to INPUT_PULLUP to resolve issue using with Pro Micro
    0.05 - Added support for the BY8301-16P and BY8001-16P sound modules
    0.06 - Using an updated version of MP3FLASH16P (MP3Flash16Pv2) which now removes static serial
           mappings out of the .cpp file
         - Add support to select between using 3 different sound modules by changing 1 variable
    0.07 - Changed hard coded serial port in library

    r0n_dL IMPORTANT NOTE & RECOGNITON: This sketch was written by DavidScott. Thanks for a great base!

    // DavidScott's original comment below

    // This example plays a random file (001 to 010) forever
    // It uses the playFileAndWait() command so no extra code is needed, it will play another file as soon as
    // the previous one finishes. If it doesn't work, try reversing the RX/TX wires as that's probably going to be
    // the most common mistake. Also check that the player's BUSY line is connected to Arduino pin 3
    // Finally check that the player has a speaker connected as well as ground and VCC

    r0n_dL NOTES:  Basic sketch provided by DavidScott to give sounds and synchronized voice lights for
                      scale BB-8 Domes

    REQUIRED Libaries to make this sketch work:

    MP3Flash16Pv2 Library
    https://github.com/r0ndL/MP3Flash16Pv2

    BY8x0116P Library
    https://github.com/r0ndL/BY8x0116Pv2


*/

// SET AUDIO OPTIONS...

// NOTE:  This sketch currently supports 3 different sound modules (MP3-FLASH-16P, BY8001-16P, BY8301-16P)

#define AUDIO1  2   // 1=MP3-FLASH-16P
// 2=BY8001-16P or BY8301-16P


// INCLUDE LIBS AND DECLARE VARIABLES...

#include "AltSoftSerial.h"
AltSoftSerial MP3Serial; // SoftwareSerial port assignments (RX, TX) to communicate with sound module

#if (AUDIO1==2)
//settings for BY8001-16P or BY8301-16P module...

#include "BY8x0116Pv2.h"
BY8x0116Pv2 myPlayer(MP3Serial); // Use SoftwareSerial as the serial port

#else
//settings for MP3-FLASH-16P...

#include "MP3Flash16Pv2.h"
MP3Flash16Pv2 myPlayer(MP3Serial); // Use SoftwareSerial as the serial port


#endif


// ARDUINO PIN ASSIGNMENTS...
// Default Pin Assignments - BASIC WIRING in NOTES

#define PIN_sound_BUSY    A3  //Connect to BUSY pin on sound module


// ADDITIONAL SOUND OPTIONS...

#define number_of_sounds  3

//
//
//

#include<ServoTimer2.h>

// RED LEDS wired to buttonA & B press
int ledPinRA =  A0;      // the number of the LED pin
int ledPinRB =  A1;      // the number of the LED pin


ServoTimer2 myservo; // create servo object to control servo CONNECT UNO TX PIN 9, RX PIN 8

int buttonApin = 2;
int buttonBpin = 4;

int pos = 0; // set initial servo position to 0
int buttonAstate = 0; // variable for reading pushbutton status
int buttonBstate = 0;

/*
  by noiasca
  https://forum.arduino.cc/index.php?topic=666044
*/

const uint16_t intervalSLOW[] {1000, 1000, 1000, 1000, 1000, 1000, 1000, 100};  // time to wait in each intervall
const uint16_t intervalMEDIUM[] {400, 400, 400, 400, 400, 400, 400, 100};
const uint16_t intervalFAST[] {100, 100, 100, 100, 100, 100, 100, 100};


const byte ledPinA = 5;
const byte ledPinB = 6;
const byte ledPinC = 7;
const byte ledPinD = A4;
const byte ledPinE = A5;
const byte ledPinF = 10;
const byte ledPinG = 11;

void handleLedsSLOW()
{

  static uint32_t previousMillis = 0;
  static byte state = 7;
  if (millis() - previousMillis >= intervalSLOW[state])
  {
    // it's time for next state
    state++;
    state = state % 8;
    Serial.print(F("state=")); Serial.println(state);

    // act according state
    switch (state)
    {
      case 0:
        digitalWrite(ledPinA, HIGH);
        digitalWrite(ledPinB, LOW);
        digitalWrite(ledPinC, LOW);
        digitalWrite(ledPinD, LOW);
        digitalWrite(ledPinE, LOW);
        digitalWrite(ledPinF, LOW);
        digitalWrite(ledPinG, LOW);
        break;
      case 1:
        digitalWrite(ledPinA, LOW);
        digitalWrite(ledPinB, HIGH);
        digitalWrite(ledPinC, LOW);
        digitalWrite(ledPinD, LOW);
        digitalWrite(ledPinE, LOW);
        digitalWrite(ledPinF, LOW);
        digitalWrite(ledPinG, LOW);
        break;
      case 2:
        digitalWrite(ledPinA, LOW);
        digitalWrite(ledPinB, LOW);
        digitalWrite(ledPinC, HIGH);
        digitalWrite(ledPinD, LOW);
        digitalWrite(ledPinE, LOW);
        digitalWrite(ledPinF, LOW);
        digitalWrite(ledPinG, LOW);
        break;
      case 3:
        digitalWrite(ledPinA, LOW);
        digitalWrite(ledPinB, LOW);
        digitalWrite(ledPinC, LOW);
        digitalWrite(ledPinD, HIGH);
        digitalWrite(ledPinE, LOW);
        digitalWrite(ledPinF, LOW);
        digitalWrite(ledPinG, LOW);
        break;
      case 4:
        digitalWrite(ledPinA, LOW);
        digitalWrite(ledPinB, LOW);
        digitalWrite(ledPinC, LOW);
        digitalWrite(ledPinD, LOW);
        digitalWrite(ledPinE, HIGH);
        digitalWrite(ledPinF, LOW);
        digitalWrite(ledPinG, LOW);
        break;
      case 5:
        digitalWrite(ledPinA, LOW);
        digitalWrite(ledPinB, LOW);
        digitalWrite(ledPinC, LOW);
        digitalWrite(ledPinD, LOW);
        digitalWrite(ledPinE, LOW);
        digitalWrite(ledPinF, HIGH);
        digitalWrite(ledPinG, LOW);
        break;
      case 6:
        digitalWrite(ledPinA, LOW);
        digitalWrite(ledPinB, LOW);
        digitalWrite(ledPinC, LOW);
        digitalWrite(ledPinD, LOW);
        digitalWrite(ledPinE, LOW);
        digitalWrite(ledPinF, LOW);
        digitalWrite(ledPinG, HIGH);
        break;
      case 7:
        digitalWrite(ledPinA, LOW);
        digitalWrite(ledPinB, LOW);
        digitalWrite(ledPinC, LOW);
        digitalWrite(ledPinD, LOW);
        digitalWrite(ledPinE, LOW);
        digitalWrite(ledPinF, LOW);
        digitalWrite(ledPinG, LOW);
        break;
    }
    previousMillis = millis();
  }
}

void handleLedsMEDIUM()
{

  static uint32_t previousMillis = 0;
  static byte state = 7;
  if (millis() - previousMillis >= intervalMEDIUM[state])
  {
    // it's time for next state
    state++;
    state = state % 8;
    Serial.print(F("state=")); Serial.println(state);

    // act according state
    switch (state)
    {
      case 0:
        digitalWrite(ledPinA, HIGH);
        digitalWrite(ledPinB, LOW);
        digitalWrite(ledPinC, LOW);
        digitalWrite(ledPinD, LOW);
        digitalWrite(ledPinE, LOW);
        digitalWrite(ledPinF, LOW);
        digitalWrite(ledPinG, LOW);
        break;
      case 1:
        digitalWrite(ledPinA, LOW);
        digitalWrite(ledPinB, HIGH);
        digitalWrite(ledPinC, LOW);
        digitalWrite(ledPinD, LOW);
        digitalWrite(ledPinE, LOW);
        digitalWrite(ledPinF, LOW);
        digitalWrite(ledPinG, LOW);
        break;
      case 2:
        digitalWrite(ledPinA, LOW);
        digitalWrite(ledPinB, LOW);
        digitalWrite(ledPinC, HIGH);
        digitalWrite(ledPinD, LOW);
        digitalWrite(ledPinE, LOW);
        digitalWrite(ledPinF, LOW);
        digitalWrite(ledPinG, LOW);
        break;
      case 3:
        digitalWrite(ledPinA, LOW);
        digitalWrite(ledPinB, LOW);
        digitalWrite(ledPinC, LOW);
        digitalWrite(ledPinD, HIGH);
        digitalWrite(ledPinE, LOW);
        digitalWrite(ledPinF, LOW);
        digitalWrite(ledPinG, LOW);
        break;
      case 4:
        digitalWrite(ledPinA, LOW);
        digitalWrite(ledPinB, LOW);
        digitalWrite(ledPinC, LOW);
        digitalWrite(ledPinD, LOW);
        digitalWrite(ledPinE, HIGH);
        digitalWrite(ledPinF, LOW);
        digitalWrite(ledPinG, LOW);
        break;
      case 5:
        digitalWrite(ledPinA, LOW);
        digitalWrite(ledPinB, LOW);
        digitalWrite(ledPinC, LOW);
        digitalWrite(ledPinD, LOW);
        digitalWrite(ledPinE, LOW);
        digitalWrite(ledPinF, HIGH);
        digitalWrite(ledPinG, LOW);
        break;
      case 6:
        digitalWrite(ledPinA, LOW);
        digitalWrite(ledPinB, LOW);
        digitalWrite(ledPinC, LOW);
        digitalWrite(ledPinD, LOW);
        digitalWrite(ledPinE, LOW);
        digitalWrite(ledPinF, LOW);
        digitalWrite(ledPinG, HIGH);
        break;
      case 7:
        digitalWrite(ledPinA, LOW);
        digitalWrite(ledPinB, LOW);
        digitalWrite(ledPinC, LOW);
        digitalWrite(ledPinD, LOW);
        digitalWrite(ledPinE, LOW);
        digitalWrite(ledPinF, LOW);
        digitalWrite(ledPinG, LOW);
        break;
    }
    previousMillis = millis();
  }
}

void handleLedsFAST()
{

  static uint32_t previousMillis = 0;
  static byte state = 7;
  if (millis() - previousMillis >= intervalFAST[state])
  {
    // it's time for next state
    state++;
    state = state % 8;
    Serial.print(F("state=")); Serial.println(state);

    // act according state
    switch (state)
    {
      case 0:
        digitalWrite(ledPinA, HIGH);
        digitalWrite(ledPinB, LOW);
        digitalWrite(ledPinC, LOW);
        digitalWrite(ledPinD, LOW);
        digitalWrite(ledPinE, LOW);
        digitalWrite(ledPinF, LOW);
        digitalWrite(ledPinG, LOW);
        break;
      case 1:
        digitalWrite(ledPinA, LOW);
        digitalWrite(ledPinB, HIGH);
        digitalWrite(ledPinC, LOW);
        digitalWrite(ledPinD, LOW);
        digitalWrite(ledPinE, LOW);
        digitalWrite(ledPinF, LOW);
        digitalWrite(ledPinG, LOW);
        break;
      case 2:
        digitalWrite(ledPinA, LOW);
        digitalWrite(ledPinB, LOW);
        digitalWrite(ledPinC, HIGH);
        digitalWrite(ledPinD, LOW);
        digitalWrite(ledPinE, LOW);
        digitalWrite(ledPinF, LOW);
        digitalWrite(ledPinG, LOW);
        break;
      case 3:
        digitalWrite(ledPinA, LOW);
        digitalWrite(ledPinB, LOW);
        digitalWrite(ledPinC, LOW);
        digitalWrite(ledPinD, HIGH);
        digitalWrite(ledPinE, LOW);
        digitalWrite(ledPinF, LOW);
        digitalWrite(ledPinG, LOW);
        break;
      case 4:
        digitalWrite(ledPinA, LOW);
        digitalWrite(ledPinB, LOW);
        digitalWrite(ledPinC, LOW);
        digitalWrite(ledPinD, LOW);
        digitalWrite(ledPinE, HIGH);
        digitalWrite(ledPinF, LOW);
        digitalWrite(ledPinG, LOW);
        break;
      case 5:
        digitalWrite(ledPinA, LOW);
        digitalWrite(ledPinB, LOW);
        digitalWrite(ledPinC, LOW);
        digitalWrite(ledPinD, LOW);
        digitalWrite(ledPinE, LOW);
        digitalWrite(ledPinF, HIGH);
        digitalWrite(ledPinG, LOW);
        break;
      case 6:
        digitalWrite(ledPinA, LOW);
        digitalWrite(ledPinB, LOW);
        digitalWrite(ledPinC, LOW);
        digitalWrite(ledPinD, LOW);
        digitalWrite(ledPinE, LOW);
        digitalWrite(ledPinF, LOW);
        digitalWrite(ledPinG, HIGH);
        break;
      case 7:
        digitalWrite(ledPinA, LOW);
        digitalWrite(ledPinB, LOW);
        digitalWrite(ledPinC, LOW);
        digitalWrite(ledPinD, LOW);
        digitalWrite(ledPinE, LOW);
        digitalWrite(ledPinF, LOW);
        digitalWrite(ledPinG, LOW);
        break;
    }
    previousMillis = millis();
  }

}



void setup() {
  myservo.attach(3);
  Serial.begin(9600);
  pinMode(3, OUTPUT); // initialize the servo pin as an output
  pinMode(buttonApin, INPUT_PULLUP); // initialize the button A pin as an input
  pinMode(buttonBpin, INPUT_PULLUP); // initialize the button B pin as an input
  pinMode(ledPinA, OUTPUT); // initialize the LED1 pin as an output
  pinMode(ledPinB, OUTPUT); // initialize the LED2 pin as an output
  pinMode(ledPinC, OUTPUT); // initialize the LED3 pin as an output
  pinMode(ledPinD, OUTPUT); // initialize the LED4 pin as an output
  pinMode(ledPinE, OUTPUT); // initialize the LED5 pin as an output
  pinMode(ledPinF, OUTPUT); // initialize the LED6 pin as an output
  pinMode(ledPinG, OUTPUT); // initialize the LED7 pin as an output

  pinMode(12, OUTPUT);    // sets the digital pin 12 as output for 2 SLIDE switch LEDS

  pinMode(ledPinRA, OUTPUT); // initialize the LED pin as an output
  pinMode(ledPinRB, OUTPUT); // initialize the LED pin as an output

  MP3Serial.begin(9600);
  myPlayer.init(PIN_sound_BUSY);      // Init the player with the MP3 BUSY pin connected to Arduino pin defined
}

void loop() {

  digitalWrite(12, HIGH); // sets the digital pin 12 on

  buttonAstate = digitalRead(buttonApin); // read the state of button A value
  buttonBstate = digitalRead(buttonBpin); // read the state of button B value

  // servo position
  if ((buttonAstate == HIGH) && (buttonBstate == HIGH)) {
    myservo.write(750);

    if (!myPlayer.isBusy()) {
      myPlayer.playFile(1, 10);
      //delay(100);
    }

    // LED controls
    handleLedsSLOW();  // checks if something is todo with the LEDs
    //do other important stuff here


    // check if the pushbutton is pressed. If it is, the buttonState is HIGH:
    if (digitalRead(buttonAstate) == HIGH) {
      // turn LED on:
      digitalWrite(ledPinRA, LOW);
    } else {
      // turn LED off:
      digitalWrite(ledPinRA, LOW);
    }


    // check if the pushbutton is pressed. If it is, the buttonState is HIGH:
    if (digitalRead(buttonBstate) == HIGH) {
      // turn LED on:
      digitalWrite(ledPinRB, LOW);
    } else {
      // turn LED off:
      digitalWrite(ledPinRB, LOW);
    }


  }

  else if (buttonAstate == LOW) { // check if button A is pressed
    myservo.write(1042); // if it is rotate servo to 35 degrees

    if (!myPlayer.isBusy()) {
      myPlayer.playFile(2, 10);
      //delay(100);
    }

    handleLedsMEDIUM();  // checks if something is todo with the LEDs
    //do other important stuff here

    // check if the pushbutton is pressed. If it is, the buttonState is HIGH:
    if (digitalRead(buttonAstate) == HIGH) {
      // turn LED on:
      digitalWrite(ledPinRA, HIGH);
    } else {
      // turn LED off:
      digitalWrite(ledPinRA, LOW);
    }

  }

  else {
    (buttonBstate == LOW)  // check if button B is pressed
    ; myservo.write(1442); // if it is rotate servo to 83 degrees

    if (!myPlayer.isBusy()) {
      myPlayer.playFile(3, 10);
      //delay(100);
    }

    handleLedsFAST();  // checks if something is todo with the LEDs
    //do other important stuff here

    // check if the pushbutton is pressed. If it is, the buttonState is HIGH:
    if (digitalRead(buttonBstate) == HIGH) {
      // turn LED on:
      digitalWrite(ledPinRB, HIGH);
    } else {
      // turn LED off:
      digitalWrite(ledPinRB, LOW);
    }
  }

  delay(100);                // waits for a second

}
[/code]

I have a BY8301-16p module that I have loaded the 3 sound files onto, 01.mp3, 02.mp3 & 03.mp3. I want 1 sound file to play & loop for each of the 3 stages listed above.
Sound 1 plays when the Nano is switched on and the LEDS sequence, that part works as it should.
For stages 2 & 3, the leds sequence & the servo moves just as they should while the corresponding button is pressed and held down.
The probelm is the sound file doesnt instantly change, it waits until its finished playing then changes to the correct track and loops while the button is pressed & held.
I tried removing the ! from

 if (!myPlayer.isBusy()) {
      myPlayer.playFile(3, 10);

& that does change the track instantly, but just causes the sound to play for a split second before looping, only when i release the button does it play that sound.

What do i need to change to make the track start as soon as the button is pressed, and play on loop for as long as the button is held down

Any help would be greatly appreciated by this Arduino newbie.

The library you are using appears to have a stopPlay() function. Try calling that when required to change the file that is playing

NOTE : I have no experience of the library

Thanks UKHeliBob, tried the following but couldn't get it to work, any ideas on where i'm going wrong? The sound did stop when i pressed the momentary switch but only played the next sound when i released the button rather than play it on loop while it is pressed & stop on the release of the button

[code]
void loop() {

  digitalWrite(12, HIGH); // sets the digital pin 12 on

  buttonAstate = digitalRead(buttonApin); // read the state of button A value
  buttonBstate = digitalRead(buttonBpin); // read the state of button B value

  // servo position
  if ((buttonAstate == HIGH) && (buttonBstate == HIGH)) {

    myservo.write(750);

    if (!myPlayer.isBusy()) {
      //delay(100)
      myPlayer.stopPlay()
      ;myPlayer.playFile(1, 15);
      //delay(100);
    }

    // LED controls
    handleLedsSLOW();  // checks if something is todo with the LEDs
    //do other important stuff here


    // check if the pushbutton is pressed. If it is, the buttonState is HIGH:
    if (digitalRead(buttonAstate) == HIGH) {
      // turn LED on:
      digitalWrite(ledPinRA, LOW);
    } else {
      // turn LED off:
      digitalWrite(ledPinRA, LOW);
    }


    // check if the pushbutton is pressed. If it is, the buttonState is HIGH:
    if (digitalRead(buttonBstate) == HIGH) {
      // turn LED on:
      digitalWrite(ledPinRB, LOW);
    } else {
      // turn LED off:
      digitalWrite(ledPinRB, LOW);
    }


  }

  else if (buttonAstate == LOW) { // check if button A is pressed
    myservo.write(1042); // if it is rotate servo to 35 degrees

    if (!myPlayer.isBusy()) {
      //delay(100)
      ;myPlayer.stopPlay()
      ;myPlayer.playFile(2, 15);
      //delay(100);
    }

    handleLedsMEDIUM();  // checks if something is todo with the LEDs
    //do other important stuff here

    // check if the pushbutton is pressed. If it is, the buttonState is HIGH:
    if (digitalRead(buttonAstate) == HIGH) {
      // turn LED on:
      digitalWrite(ledPinRA, HIGH);
    } else {
      // turn LED off:
      digitalWrite(ledPinRA, LOW);
    }

  }

  else {
    (buttonBstate == LOW)  // check if button B is pressed
    ;myservo.write(1442); // if it is rotate servo to 83 degrees

    if (!myPlayer.isBusy()) {
      //delay(100)
      ;myPlayer.stopPlay()
      ;myPlayer.playFile(3, 15);
      //delay(100);
    }

    handleLedsFAST();  // checks if something is todo with the LEDs
    //do other important stuff here

    // check if the pushbutton is pressed. If it is, the buttonState is HIGH:
    if (digitalRead(buttonBstate) == HIGH) {
      // turn LED on:
      digitalWrite(ledPinRB, HIGH);
    } else {
      // turn LED off:
      digitalWrite(ledPinRB, LOW);
    }
  }

  delay(100);                // waits for a second

}
[/code]

also tried this but i couldnt get any sound to play, so at least I know the stopPlay code works

[code]
void loop() {

  digitalWrite(12, HIGH); // sets the digital pin 12 on

  buttonAstate = digitalRead(buttonApin); // read the state of button A value
  buttonBstate = digitalRead(buttonBpin); // read the state of button B value

  // servo position
  if ((buttonAstate == HIGH) && (buttonBstate == HIGH)) {

    myservo.write(750);
    ; myPlayer.stopPlay();

    if (!myPlayer.isBusy()) {
      //delay(100)
      ; myPlayer.playFile(1, 15);
      //delay(100);
    }

    // LED controls
    handleLedsSLOW();  // checks if something is todo with the LEDs
    //do other important stuff here


    // check if the pushbutton is pressed. If it is, the buttonState is HIGH:
    if (digitalRead(buttonAstate) == HIGH) {
      // turn LED on:
      digitalWrite(ledPinRA, LOW);
    } else {
      // turn LED off:
      digitalWrite(ledPinRA, LOW);
    }


    // check if the pushbutton is pressed. If it is, the buttonState is HIGH:
    if (digitalRead(buttonBstate) == HIGH) {
      // turn LED on:
      digitalWrite(ledPinRB, LOW);
    } else {
      // turn LED off:
      digitalWrite(ledPinRB, LOW);
    }


  }

  else if (buttonAstate == LOW) { // check if button A is pressed
    myservo.write(1042); // if it is rotate servo to 35 degrees
    ; myPlayer.stopPlay();

    if (!myPlayer.isBusy()) {
      //delay(100)
      ; myPlayer.playFile(2, 15);
      //delay(100);
    }

    handleLedsMEDIUM();  // checks if something is todo with the LEDs
    //do other important stuff here

    // check if the pushbutton is pressed. If it is, the buttonState is HIGH:
    if (digitalRead(buttonAstate) == HIGH) {
      // turn LED on:
      digitalWrite(ledPinRA, HIGH);
    } else {
      // turn LED off:
      digitalWrite(ledPinRA, LOW);
    }

  }

  else {
    (buttonBstate == LOW)  // check if button B is pressed
    ; myservo.write(1442); // if it is rotate servo to 83 degrees
    ; myPlayer.stopPlay();

    if (!myPlayer.isBusy()) {
      //delay(100)
      ; myPlayer.playFile(3, 15);
      //delay(100);
    }

    handleLedsFAST();  // checks if something is todo with the LEDs
    //do other important stuff here

    // check if the pushbutton is pressed. If it is, the buttonState is HIGH:
    if (digitalRead(buttonBstate) == HIGH) {
      // turn LED on:
      digitalWrite(ledPinRB, HIGH);
    } else {
      // turn LED off:
      digitalWrite(ledPinRB, LOW);
    }
  }

  delay(100);                // waits for a second

}
[/code]

As I say, I have no experience of this library nor any MP3 hardware to try it on

Why are you testing whether the player is busy or not when reacting to a button press ?

From your comment sounds like i shouldn't be doing that? Honestly the real reason its there is that it worked to start the module playing the sound so i copied it into the next part of the sketch hoping it would work there. I'm still a complete novice at writing sketches, i tend to muddle my way through looking at existing sketches and trying to figure out by trial and error how they work, or managing to find a good tutorial and adapt them to what i need. Can you point me to any good tutorials on what you think i should be using in the sketch or give me a pointer as to what might work better? :slight_smile:

The sketch seems to check whether a file is not being played, which does not seem to make any sense when what you want to do on detecting a button press is to start a file playing anyway

What should happen if file1 is being played and the file1 button is pressed again ? Should it be ignored or should the same file be restarted ? It makes a big difference as to how you structure your sketch

Either way, it would be better if you detected when the buttons became pressed rather than when the are pressed to prevent the same code being repeated until you release the button which is what is happening now

Thank you, your comments are spot on and i think i know what to do now

So far the sketch is working as I want it, except for the sound effect playback not starting instantly.
I think this might be the answer

  1. Remove the check module busy line in the sketch, as you rightly pointed out, I don't need to check if sound is being played for what i want the sketch to do.
  2. Within each of the 3 parts of the sketch, tell the module to play a specific track within the loop part of the code
  3. At the start of each of the 3 stages create a flag, an instruction that happens only once within a loop, telling the by8301 module to stop playing.
    My understanding of the flag function is that when i start one of the stages in the sketch this will activate the flagged "stop playing", then the requested track will start on a loop along with Servo & led Controls, for as long as the button is pressed, but then ignore the flagged "stop playing" when it loops and just keep looping the requested track etc. until the button is released.
    Then it'll go to another stage of the sketch and repeat the same actions there but with different variables.

Have i got that right?

I am slightly lost as to what you want to do but it sounds like you are making things more complicated than they need to be

Please describe in English what you want the sketch to do. Something like :

  • When first started the sketch should move the servo to a fixed postion
  • When buttonA becomes pressed play songA and output some servo commands
  • When buttonB becomes pressed play songB and output some different servo commands

Is that correct ?

Sorry you get so used to knowing what you want your own sketch to do you think everyone else will understand.

  • When first started the sketch doesn't move the servo, but plays sound 01 and sequences the slow speed version of the LEDs
  • When buttonA becomes pressed the servo moves to 1st position, plays sound 02 and sequences the medium speed version of the LEDs
  • When buttonB becomes pressed the servo moves to 2nd position, plays sound 03 and sequences the fast speed version of the LEDs

All the actions start and loop as they should when i press & hold, or release, the buttons, except for the sound, it wont start playing the newly requested track until it finishes playing the current track. That's why I thought I needed to add a flag to stop the track playing at the beginning of each of the 3 stages to allow the requested to instantly play? Is there a simpler way to do it?

Do you understand the difference between detecting when a button becomes pressed and when it is pressed ?

Part of your code does something like this

while the button is pressed
  stop the file currently playing
  play a file
end while

can you see that the file will not play for long until the button is released because it is constantly being stopped ? Does that sound familiar ?

Now contrast that with

if the button has become pressed
  stop the file currently playing
  play a file
end if

Can you see that the file will now play and the stop will not occur until the button is released and pressed again ?

Take a look at the StateChangeDetection example in the IDE

1 Like

Thank you for your help with this it really is appreciated.

can you see that the file will not play for long until the button is released because it is constantly being stopped ? Does that sound familiar ?

That makes a lot of sense. Explains why the sound wouldn't start until i released the button.
So if i understand this part of the code is causing the "While the button is pressed"

else if (buttonAstate == LOW) { // check if button A is pressed

Is this just stating that the button is pressed? & i should be checking to see if the button state has changed?
Something along the lines of this?

 // compare the buttonAState to its previous state
  if (buttonAState != lastButtonAState) {
    // if the state has changed, perform the following actions
    if (buttonAState == HIGH) {
      // if the current state is HIGH then the button went from off to on:
      myservo.write(1042); // if it is rotate servo to 35 degrees
 {
      //delay(250) //may be needed to stop clicking sound
      ; myPlayer.playFile(2, 10);
      //delay(100); //may be needed to stop clicking sound
    }

    handleLedsMEDIUM();  // checks if something is todo with the LEDs
    //do other important stuff here
 {
      // turn LED on:
      digitalWrite(ledPinRA, HIGH);
 } 
else {
      // if the current state is LOW then the button went from on to off:
      digitalWrite(ledPinRA, LOW);
    }
    // Delay a little bit to avoid bouncing
    delay(50);
  }
  // save the current state as the last state, for next time through the loop
  lastButtonAState = buttonAState;

I haven't verified the above or checked all of the brackets etc but is it heading in the right direction?

The principle in your code snippet of detecting a change of state looks OK but I am not sure of the details of what you want to do. How exactly are the button inputs wired and have you got pulldown resistors in place to keep the inputs at a known state at all times ?

Here is the snippet Auto Formatted in the IDE with each { and } on its own line which shows the code blocks more clearly

// compare the buttonAState to its previous state
if (buttonAState != lastButtonAState)
{
  // if the state has changed, perform the following actions
  if (buttonAState == HIGH)
  {
    // if the current state is HIGH then the button went from off to on:
    myservo.write(1042); // if it is rotate servo to 35 degrees
    {
      //delay(250) //may be needed to stop clicking sound
      ;
      myPlayer.playFile(2, 10);
      //delay(100); //may be needed to stop clicking sound
    } //WHAT IS THE PURPOSE OF THIS ?
    handleLedsMEDIUM();  // checks if something is todo with the LEDs
    //do other important stuff here
    { //WHAT IS THE PURPOSE OF THIS ?
      // turn LED on:
      digitalWrite(ledPinRA, HIGH);
    }
    else
    {
      // if the current state is LOW then the button went from on to off:
      digitalWrite(ledPinRA, LOW);
    }
    // Delay a little bit to avoid bouncing
    delay(50);
  }
  // save the current state as the last state, for next time through the loop
  lastButtonAState = buttonAState;

You appear to have superfluous braces which confuses the logic of what the code is supposed to do. See my added comments. All of the code dependant on the button becoming pressed simply needs to go in one pair of { and } but you have them scattered almost at random and as the code is incomplete it is impossible to follow the logic

1 Like

Here is my general wiring diagram from Tinkercad, it doesn't show the connection to the BY8301 sound module as Tinkercad doesn't have that it as part of it library. I also had to move pins 8 & 9 to A4 & A5 as AltSoftSerial.h needs them for TX & RX to communicate with the BY8301 module

I didnt use Pull Down resistors as i thought that i'd read that using INPUT_PULLUP

pinMode(buttonApin, INPUT_PULLUP);

used the internal Arduino pull down resistors, do i still need to add them to reduce bounce?

what i want the sketch to do is the following
at start up when neither button is being pressed the following actions happen at the same time (might also need to stop current track from playing to allow the requested track to play, wont know for sure until i test this version for the sketch)
myservo.write(750); // servo to stay at 0 degrees, no rotation
myPlayer.playFile(1, 10); //sound module plays specified track at specified volume
handleLedsSLOW(); //the specified LED sequence plays, 7 LEDS flash one after the other in sequence at a specific speed
digitalWrite(ledPinRA, LOW); // LED turns off for Button A (Is this needed now if i'm checking the state of the button, wont it switch off when i release the button?)
digitalWrite(ledPinRB, LOW); // LED turns off for Button B (Is this needed now if i'm checking the state of the button, wont it switch off when i release the button?)

buttonA has become pressed the following actions happen at the same time (might also need to stop current track from playing to allow the requested track to play, wont know for sure until i test this version for the sketch)
myservo.write(1042); // rotate servo to 35 degrees
myPlayer.playFile(2, 10); //sound module plays specified track at specified volume
handleLedsMEDIUM(); //the specified LED sequence plays, 7 LEDS flash one after the other in sequence at a specific speed
digitalWrite(ledPinRA, HIGH); // An LED turns on to show the button is pressed

when buttonB has become pressed the following actions happen at the same time (might also need to stop current track from playing to allow the requested track to play, wont know for sure until i test this version for the sketch)
myservo.write(1442); // rotate servo to 83 degrees
myPlayer.playFile(3, 10); //sound module plays specified track at specified volume
handleLedsFAST(); //the specified LED sequence plays, 7 LEDS flash one after the other in sequence at a specific speed
digitalWrite(ledPinRB, HIGH); // An LED turns on to show the button is pressed

I'll look at moving them all into one pair of {} as per your advice tonight

Using INPUT_PULLUP is much simpler than using external resistors, but you must wire the switches to take the input to GND when closed and test for LOW in the sketch to determine that a switch is closed. Your latest code snippet does the opposite

if (buttonAState != lastButtonAState)
{
  // if the state has changed, perform the following actions
  if (buttonAState == HIGH)
  {
    // if the current state is HIGH then the button went from off to on:
1 Like

Thank you again for your help with this. There's a lot to take in and start experimenting with, I'll give it a go tonight and see how I get on with making these changes and report back

I look forward to seeing the improvements. Don't stress over it. Take it slowly and you will get there

1 Like

Ah okay, just read up again on input_pullup and it inverts the high/low setting so that open circuit is HIGH and closed is LOW. I'll keep that in mind when I update the sketch

I wish i didnt stress over this LOL Unfortunatly I am :frowning:

Every time I think I've got it it just doesn't quite work the way I thought it would.

I have changed the sketch & tried to neaten it up a bit but I cant get it to compile now due to my lack of Arduino coding skills.

Could someone please have look at it and point in the right direction, any help would be fantastic.

I added the following to my sketch, "int lastbuttonAstate = 0; // previous state of the buttonA" &
"int lastbuttonBstate = 0; // previous state of the buttonB" to try and match the arduino example StateChangeDetection.
Have i named them incorrectly should i just have used this "int lastButtonState = 0; // previous state of the button" and that would save the state to each of the buttons?

Also not sure ive got the state detection correct for the 3 stages

[code]
/*
    ===================================================================================================
    ==================================== BB-8 PADAWAN DOME ============================================
    ======================================  Version 0.07  =============================================
    ===================================================================================================

    Version History
    0.01 - Original Sketch by DavidScott
    0.02 - r0n_dL added annotations & variable for PIN_sound_BUSY as suggested by DavidScott
    0.03 - r0n_dL reassigned pins to align with Padawan control system
         - Changed SoftwareSerial pins (RX/TX) 10/9 to 8/4
    0.04 - Change pinMode for PIN_trigger to INPUT_PULLUP to resolve issue using with Pro Micro
    0.05 - Added support for the BY8301-16P and BY8001-16P sound modules
    0.06 - Using an updated version of MP3FLASH16P (MP3Flash16Pv2) which now removes static serial
           mappings out of the .cpp file
         - Add support to select between using 3 different sound modules by changing 1 variable
    0.07 - Changed hard coded serial port in library

    r0n_dL IMPORTANT NOTE & RECOGNITON: This sketch was written by DavidScott. Thanks for a great base!

    If it doesn't work, try reversing the RX/TX wires as that's probably going to be
    // the most common mistake. Also check that the player's BUSY line is connected to Arduino pin 3
    // Finally check that the player has a speaker connected as well as ground and VCC

    r0n_dL NOTES:  Basic sketch provided by DavidScott to give sounds and synchronized voice lights for
                      scale BB-8 Domes

    REQUIRED Libaries to make this sketch work:

    MP3Flash16Pv2 Library
    https://github.com/r0ndL/MP3Flash16Pv2

    BY8x0116P Library
    https://github.com/r0ndL/BY8x0116Pv2


*/

// SET AUDIO OPTIONS...

// NOTE:  This sketch currently supports 3 different sound modules (MP3-FLASH-16P, BY8001-16P, BY8301-16P)

#define AUDIO1  2   // 1=MP3-FLASH-16P
// 2=BY8001-16P or BY8301-16P


// INCLUDE LIBS AND DECLARE VARIABLES...

#include "AltSoftSerial.h"
AltSoftSerial MP3Serial; // SoftwareSerial port assignments (RX, TX) to communicate with sound module

#if (AUDIO1==2)
//settings for BY8001-16P or BY8301-16P module...

#include "BY8x0116Pv2.h"
BY8x0116Pv2 myPlayer(MP3Serial); // Use SoftwareSerial as the serial port

#else
//settings for MP3-FLASH-16P...

#include "MP3Flash16Pv2.h"
MP3Flash16Pv2 myPlayer(MP3Serial); // Use SoftwareSerial as the serial port


#endif


// ARDUINO PIN ASSIGNMENTS...
// Default Pin Assignments - BASIC WIRING in NOTES

#define PIN_sound_BUSY    A3  //Connect to BUSY pin on sound module


// ADDITIONAL SOUND OPTIONS...

#define number_of_sounds  3

//
//
//

#include<ServoTimer2.h>

// RED LEDS wired to buttonA & B press
int ledPinRA =  A0;      // the number of the LED pin
int ledPinRB =  A1;      // the number of the LED pin


ServoTimer2 myservo; // create servo object to control servo CONNECT UNO TX PIN 9, RX PIN 8

int buttonApin = 2;
int buttonBpin = 4;

// Variables will change:
int pos = 0; // set initial servo position to 0
int buttonAstate = 0; // variable for reading pushbutton status
int lastbuttonAstate = 0;     // previous state of the buttonA

int buttonBstate = 0;
int lastbuttonBstate = 0;     // previous state of the buttonB

/*
  by noiasca
  https://forum.arduino.cc/index.php?topic=666044
*/

const uint16_t intervalSLOW[] {1000, 1000, 1000, 1000, 1000, 1000, 1000, 100};  // time to wait in each intervall
const uint16_t intervalMEDIUM[] {400, 400, 400, 400, 400, 400, 400, 100};
const uint16_t intervalFAST[] {100, 100, 100, 100, 100, 100, 100, 100};


const byte ledPinA = 5;
const byte ledPinB = 6;
const byte ledPinC = 7;
const byte ledPinD = A4;
const byte ledPinE = A5;
const byte ledPinF = 10;
const byte ledPinG = 11;

void handleLedsSLOW()
{

  static uint32_t previousMillis = 0;
  static byte state = 7;
  if (millis() - previousMillis >= intervalSLOW[state])
  {
    // it's time for next state
    state++;
    state = state % 8;
    Serial.print(F("state=")); Serial.println(state);

    // act according state
    switch (state)
    {
      case 0:
        digitalWrite(ledPinA, HIGH);
        digitalWrite(ledPinB, LOW);
        digitalWrite(ledPinC, LOW);
        digitalWrite(ledPinD, LOW);
        digitalWrite(ledPinE, LOW);
        digitalWrite(ledPinF, LOW);
        digitalWrite(ledPinG, LOW);
        break;
      case 1:
        digitalWrite(ledPinA, LOW);
        digitalWrite(ledPinB, HIGH);
        digitalWrite(ledPinC, LOW);
        digitalWrite(ledPinD, LOW);
        digitalWrite(ledPinE, LOW);
        digitalWrite(ledPinF, LOW);
        digitalWrite(ledPinG, LOW);
        break;
      case 2:
        digitalWrite(ledPinA, LOW);
        digitalWrite(ledPinB, LOW);
        digitalWrite(ledPinC, HIGH);
        digitalWrite(ledPinD, LOW);
        digitalWrite(ledPinE, LOW);
        digitalWrite(ledPinF, LOW);
        digitalWrite(ledPinG, LOW);
        break;
      case 3:
        digitalWrite(ledPinA, LOW);
        digitalWrite(ledPinB, LOW);
        digitalWrite(ledPinC, LOW);
        digitalWrite(ledPinD, HIGH);
        digitalWrite(ledPinE, LOW);
        digitalWrite(ledPinF, LOW);
        digitalWrite(ledPinG, LOW);
        break;
      case 4:
        digitalWrite(ledPinA, LOW);
        digitalWrite(ledPinB, LOW);
        digitalWrite(ledPinC, LOW);
        digitalWrite(ledPinD, LOW);
        digitalWrite(ledPinE, HIGH);
        digitalWrite(ledPinF, LOW);
        digitalWrite(ledPinG, LOW);
        break;
      case 5:
        digitalWrite(ledPinA, LOW);
        digitalWrite(ledPinB, LOW);
        digitalWrite(ledPinC, LOW);
        digitalWrite(ledPinD, LOW);
        digitalWrite(ledPinE, LOW);
        digitalWrite(ledPinF, HIGH);
        digitalWrite(ledPinG, LOW);
        break;
      case 6:
        digitalWrite(ledPinA, LOW);
        digitalWrite(ledPinB, LOW);
        digitalWrite(ledPinC, LOW);
        digitalWrite(ledPinD, LOW);
        digitalWrite(ledPinE, LOW);
        digitalWrite(ledPinF, LOW);
        digitalWrite(ledPinG, HIGH);
        break;
      case 7:
        digitalWrite(ledPinA, LOW);
        digitalWrite(ledPinB, LOW);
        digitalWrite(ledPinC, LOW);
        digitalWrite(ledPinD, LOW);
        digitalWrite(ledPinE, LOW);
        digitalWrite(ledPinF, LOW);
        digitalWrite(ledPinG, LOW);
        break;
    }
    previousMillis = millis();
  }
}

void handleLedsMEDIUM()
{

  static uint32_t previousMillis = 0;
  static byte state = 7;
  if (millis() - previousMillis >= intervalMEDIUM[state])
  {
    // it's time for next state
    state++;
    state = state % 8;
    Serial.print(F("state=")); Serial.println(state);

    // act according state
    switch (state)
    {
      case 0:
        digitalWrite(ledPinA, HIGH);
        digitalWrite(ledPinB, LOW);
        digitalWrite(ledPinC, LOW);
        digitalWrite(ledPinD, LOW);
        digitalWrite(ledPinE, LOW);
        digitalWrite(ledPinF, LOW);
        digitalWrite(ledPinG, LOW);
        break;
      case 1:
        digitalWrite(ledPinA, LOW);
        digitalWrite(ledPinB, HIGH);
        digitalWrite(ledPinC, LOW);
        digitalWrite(ledPinD, LOW);
        digitalWrite(ledPinE, LOW);
        digitalWrite(ledPinF, LOW);
        digitalWrite(ledPinG, LOW);
        break;
      case 2:
        digitalWrite(ledPinA, LOW);
        digitalWrite(ledPinB, LOW);
        digitalWrite(ledPinC, HIGH);
        digitalWrite(ledPinD, LOW);
        digitalWrite(ledPinE, LOW);
        digitalWrite(ledPinF, LOW);
        digitalWrite(ledPinG, LOW);
        break;
      case 3:
        digitalWrite(ledPinA, LOW);
        digitalWrite(ledPinB, LOW);
        digitalWrite(ledPinC, LOW);
        digitalWrite(ledPinD, HIGH);
        digitalWrite(ledPinE, LOW);
        digitalWrite(ledPinF, LOW);
        digitalWrite(ledPinG, LOW);
        break;
      case 4:
        digitalWrite(ledPinA, LOW);
        digitalWrite(ledPinB, LOW);
        digitalWrite(ledPinC, LOW);
        digitalWrite(ledPinD, LOW);
        digitalWrite(ledPinE, HIGH);
        digitalWrite(ledPinF, LOW);
        digitalWrite(ledPinG, LOW);
        break;
      case 5:
        digitalWrite(ledPinA, LOW);
        digitalWrite(ledPinB, LOW);
        digitalWrite(ledPinC, LOW);
        digitalWrite(ledPinD, LOW);
        digitalWrite(ledPinE, LOW);
        digitalWrite(ledPinF, HIGH);
        digitalWrite(ledPinG, LOW);
        break;
      case 6:
        digitalWrite(ledPinA, LOW);
        digitalWrite(ledPinB, LOW);
        digitalWrite(ledPinC, LOW);
        digitalWrite(ledPinD, LOW);
        digitalWrite(ledPinE, LOW);
        digitalWrite(ledPinF, LOW);
        digitalWrite(ledPinG, HIGH);
        break;
      case 7:
        digitalWrite(ledPinA, LOW);
        digitalWrite(ledPinB, LOW);
        digitalWrite(ledPinC, LOW);
        digitalWrite(ledPinD, LOW);
        digitalWrite(ledPinE, LOW);
        digitalWrite(ledPinF, LOW);
        digitalWrite(ledPinG, LOW);
        break;
    }
    previousMillis = millis();
  }
}

void handleLedsFAST()
{

  static uint32_t previousMillis = 0;
  static byte state = 7;
  if (millis() - previousMillis >= intervalFAST[state])
  {
    // it's time for next state
    state++;
    state = state % 8;
    Serial.print(F("state=")); Serial.println(state);

    // act according state
    switch (state)
    {
      case 0:
        digitalWrite(ledPinA, HIGH);
        digitalWrite(ledPinB, LOW);
        digitalWrite(ledPinC, LOW);
        digitalWrite(ledPinD, LOW);
        digitalWrite(ledPinE, LOW);
        digitalWrite(ledPinF, LOW);
        digitalWrite(ledPinG, LOW);
        break;
      case 1:
        digitalWrite(ledPinA, LOW);
        digitalWrite(ledPinB, HIGH);
        digitalWrite(ledPinC, LOW);
        digitalWrite(ledPinD, LOW);
        digitalWrite(ledPinE, LOW);
        digitalWrite(ledPinF, LOW);
        digitalWrite(ledPinG, LOW);
        break;
      case 2:
        digitalWrite(ledPinA, LOW);
        digitalWrite(ledPinB, LOW);
        digitalWrite(ledPinC, HIGH);
        digitalWrite(ledPinD, LOW);
        digitalWrite(ledPinE, LOW);
        digitalWrite(ledPinF, LOW);
        digitalWrite(ledPinG, LOW);
        break;
      case 3:
        digitalWrite(ledPinA, LOW);
        digitalWrite(ledPinB, LOW);
        digitalWrite(ledPinC, LOW);
        digitalWrite(ledPinD, HIGH);
        digitalWrite(ledPinE, LOW);
        digitalWrite(ledPinF, LOW);
        digitalWrite(ledPinG, LOW);
        break;
      case 4:
        digitalWrite(ledPinA, LOW);
        digitalWrite(ledPinB, LOW);
        digitalWrite(ledPinC, LOW);
        digitalWrite(ledPinD, LOW);
        digitalWrite(ledPinE, HIGH);
        digitalWrite(ledPinF, LOW);
        digitalWrite(ledPinG, LOW);
        break;
      case 5:
        digitalWrite(ledPinA, LOW);
        digitalWrite(ledPinB, LOW);
        digitalWrite(ledPinC, LOW);
        digitalWrite(ledPinD, LOW);
        digitalWrite(ledPinE, LOW);
        digitalWrite(ledPinF, HIGH);
        digitalWrite(ledPinG, LOW);
        break;
      case 6:
        digitalWrite(ledPinA, LOW);
        digitalWrite(ledPinB, LOW);
        digitalWrite(ledPinC, LOW);
        digitalWrite(ledPinD, LOW);
        digitalWrite(ledPinE, LOW);
        digitalWrite(ledPinF, LOW);
        digitalWrite(ledPinG, HIGH);
        break;
      case 7:
        digitalWrite(ledPinA, LOW);
        digitalWrite(ledPinB, LOW);
        digitalWrite(ledPinC, LOW);
        digitalWrite(ledPinD, LOW);
        digitalWrite(ledPinE, LOW);
        digitalWrite(ledPinF, LOW);
        digitalWrite(ledPinG, LOW);
        break;
    }
    previousMillis = millis();
  }

}



void setup() {
  myservo.attach(3);
  Serial.begin(9600);
  pinMode(3, OUTPUT); // initialize the servo pin as an output
  pinMode(buttonApin, INPUT_PULLUP); // initialize the button A pin as an input
  pinMode(buttonBpin, INPUT_PULLUP); // initialize the button B pin as an input
  pinMode(ledPinA, OUTPUT); // initialize the LED1 pin as an output
  pinMode(ledPinB, OUTPUT); // initialize the LED2 pin as an output
  pinMode(ledPinC, OUTPUT); // initialize the LED3 pin as an output
  pinMode(ledPinD, OUTPUT); // initialize the LED4 pin as an output
  pinMode(ledPinE, OUTPUT); // initialize the LED5 pin as an output
  pinMode(ledPinF, OUTPUT); // initialize the LED6 pin as an output
  pinMode(ledPinG, OUTPUT); // initialize the LED7 pin as an output

  pinMode(12, OUTPUT);    // sets the digital pin 12 as output for 2 SLIDE switch LEDS

  pinMode(ledPinRA, OUTPUT); // initialize the LED pin as an output
  pinMode(ledPinRB, OUTPUT); // initialize the LED pin as an output

  MP3Serial.begin(9600);
  myPlayer.init(PIN_sound_BUSY);      // Init the player with the MP3 BUSY pin connected to Arduino pin defined
}

void loop() {

  digitalWrite(12, HIGH); // sets the digital pin 12 on

  buttonAstate = digitalRead(buttonApin); // read the state of button A value
  buttonBstate = digitalRead(buttonBpin); // read the state of button B value

  // servo position
  if ((buttonAstate == HIGH ) && (buttonBstate == HIGH )) {
    myservo.write(750); // SEVRO parked at 0 deg

    myPlayer.playFile(1, 10); // play track 1 at volume 10

    // LED controls
    handleLedsSLOW();  // start the LED sequence

    digitalWrite(ledPinRA, LOW); //  LED off

    digitalWrite(ledPinRB, LOW); // LED off
  }


  else if (buttonAstate != lastbuttonAstate) { // check if button A is pressed
    myservo.write(1042); // rotate servo to 35 deg

    myPlayer.playFile(2, 10); // play track 2 at volume 10

    handleLedsMEDIUM();  // start the LED sequence

    digitalWrite(ledPinRA, HIGH); //  LED on
  } else {
    // turn LED off:
    digitalWrite(ledPinRA, LOW); //  LED off
  }
}
lastbuttonAstate = buttonAstate;




else {
  (buttonBstate != lastbuttonBstate);  // check if button B is pressed
  myservo.write(1442); // rotate servo to 83 deg

  myPlayer.playFile(3, 10); // play track 3 at volume 10

  handleLedsFAST();  // checks if something is todo with the LEDs

  digitalWrite(ledPinRB, HIGH); // start the LED sequence
} else {
  // turn LED off:
  digitalWrite(ledPinRB, LOW); //  LED off
}
}
lastbuttonBstate = buttonBstate;

delay(100);                // waits for a second

}
[/code]

The is the error message

Arduino: 1.8.15 (Windows Store 1.8.49.0) (Windows 10), Board: "Arduino Nano, ATmega328P"





















sketch_pke_meter_StateChangeDetection_thinned:457:1: error: 'lastbuttonAstate' does not name a type

 lastbuttonAstate = buttonAstate;

 ^~~~~~~~~~~~~~~~

sketch_pke_meter_StateChangeDetection_thinned:462:1: error: expected unqualified-id before 'else'

 else {

 ^~~~

sketch_pke_meter_StateChangeDetection_thinned:471:3: error: expected unqualified-id before 'else'

 } else {

   ^~~~

sketch_pke_meter_StateChangeDetection_thinned:475:1: error: expected declaration before '}' token

 }

 ^

exit status 1

'lastbuttonAstate' does not name a type



This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.

I got the following code to compile but now the servos is constantly moving, the light sequences are running fast then slow, , an LED that should only come on when the buttons pressed is on constantly & the sound is just ticking as its constantly restarting....HELP!

[code]
/*

*/

// SET AUDIO OPTIONS...

// NOTE:  This sketch currently supports 3 different sound modules (MP3-FLASH-16P, BY8001-16P, BY8301-16P)

#define AUDIO1  2   // 1=MP3-FLASH-16P
// 2=BY8001-16P or BY8301-16P
//--------------------------------------------------------------------

// INCLUDE LIBS AND DECLARE VARIABLES...

#include "AltSoftSerial.h"
AltSoftSerial MP3Serial; // SoftwareSerial port assignments (RX, TX) to communicate with sound module

#if (AUDIO1==2)
//settings for BY8001-16P or BY8301-16P module...

#include "BY8x0116Pv2.h"
BY8x0116Pv2 myPlayer(MP3Serial); // Use SoftwareSerial as the serial port

#else
//settings for MP3-FLASH-16P...

#include "MP3Flash16Pv2.h"
MP3Flash16Pv2 myPlayer(MP3Serial); // Use SoftwareSerial as the serial port

#endif

// ARDUINO PIN ASSIGNMENTS...
// Default Pin Assignments - BASIC WIRING in NOTES
#define PIN_sound_BUSY    A3  //Connect to BUSY pin on sound module

// ADDITIONAL SOUND OPTIONS...
#define number_of_sounds  3

//---------------------------------------------------------------------

#include<ServoTimer2.h>

//---------------------------------------------------------------------
// RED LEDS wired to buttonA & B press
int ledPinRA =  A0;      // the number of the LED pin
int ledPinRB =  A1;      // the number of the LED pin

//---------------------------------------------------------------------
ServoTimer2 myservo; // create servo object to control servo CONNECT UNO TX PIN 9, RX PIN 8

//---------------------------------------------------------------------
int buttonApin = 2;
int buttonBpin = 4;

//---------------------------------------------------------------------

// Variables will change:
int pos = 0; // set initial servo position to 0
int buttonAstate = 0; // variable for reading pushbutton status
int buttonBstate = 0;
int lastButtonState = 0;     // previous state of the button

//---------------------------------------------------------------------

/*
  by noiasca
  https://forum.arduino.cc/index.php?topic=666044
*/

const uint16_t intervalSLOW[] {1000, 1000, 1000, 1000, 1000, 1000, 1000, 100};  // time to wait in each intervall
const uint16_t intervalMEDIUM[] {400, 400, 400, 400, 400, 400, 400, 100};
const uint16_t intervalFAST[] {100, 100, 100, 100, 100, 100, 100, 100};


const byte ledPinA = 5;
const byte ledPinB = 6;
const byte ledPinC = 7;
const byte ledPinD = A4;
const byte ledPinE = A5;
const byte ledPinF = 10;
const byte ledPinG = 11;

void handleLedsSLOW()
{

  static uint32_t previousMillis = 0;
  static byte state = 7;
  if (millis() - previousMillis >= intervalSLOW[state])
  {
    // it's time for next state
    state++;
    state = state % 8;
    Serial.print(F("state=")); Serial.println(state);

    // act according state
    switch (state)
    {
      case 0:
        digitalWrite(ledPinA, HIGH);
        digitalWrite(ledPinB, LOW);
        digitalWrite(ledPinC, LOW);
        digitalWrite(ledPinD, LOW);
        digitalWrite(ledPinE, LOW);
        digitalWrite(ledPinF, LOW);
        digitalWrite(ledPinG, LOW);
        break;
      case 1:
        digitalWrite(ledPinA, LOW);
        digitalWrite(ledPinB, HIGH);
        digitalWrite(ledPinC, LOW);
        digitalWrite(ledPinD, LOW);
        digitalWrite(ledPinE, LOW);
        digitalWrite(ledPinF, LOW);
        digitalWrite(ledPinG, LOW);
        break;
      case 2:
        digitalWrite(ledPinA, LOW);
        digitalWrite(ledPinB, LOW);
        digitalWrite(ledPinC, HIGH);
        digitalWrite(ledPinD, LOW);
        digitalWrite(ledPinE, LOW);
        digitalWrite(ledPinF, LOW);
        digitalWrite(ledPinG, LOW);
        break;
      case 3:
        digitalWrite(ledPinA, LOW);
        digitalWrite(ledPinB, LOW);
        digitalWrite(ledPinC, LOW);
        digitalWrite(ledPinD, HIGH);
        digitalWrite(ledPinE, LOW);
        digitalWrite(ledPinF, LOW);
        digitalWrite(ledPinG, LOW);
        break;
      case 4:
        digitalWrite(ledPinA, LOW);
        digitalWrite(ledPinB, LOW);
        digitalWrite(ledPinC, LOW);
        digitalWrite(ledPinD, LOW);
        digitalWrite(ledPinE, HIGH);
        digitalWrite(ledPinF, LOW);
        digitalWrite(ledPinG, LOW);
        break;
      case 5:
        digitalWrite(ledPinA, LOW);
        digitalWrite(ledPinB, LOW);
        digitalWrite(ledPinC, LOW);
        digitalWrite(ledPinD, LOW);
        digitalWrite(ledPinE, LOW);
        digitalWrite(ledPinF, HIGH);
        digitalWrite(ledPinG, LOW);
        break;
      case 6:
        digitalWrite(ledPinA, LOW);
        digitalWrite(ledPinB, LOW);
        digitalWrite(ledPinC, LOW);
        digitalWrite(ledPinD, LOW);
        digitalWrite(ledPinE, LOW);
        digitalWrite(ledPinF, LOW);
        digitalWrite(ledPinG, HIGH);
        break;
      case 7:
        digitalWrite(ledPinA, LOW);
        digitalWrite(ledPinB, LOW);
        digitalWrite(ledPinC, LOW);
        digitalWrite(ledPinD, LOW);
        digitalWrite(ledPinE, LOW);
        digitalWrite(ledPinF, LOW);
        digitalWrite(ledPinG, LOW);
        break;
    }
    previousMillis = millis();
  }
}

void handleLedsMEDIUM()
{

  static uint32_t previousMillis = 0;
  static byte state = 7;
  if (millis() - previousMillis >= intervalMEDIUM[state])
  {
    // it's time for next state
    state++;
    state = state % 8;
    Serial.print(F("state=")); Serial.println(state);

    // act according state
    switch (state)
    {
      case 0:
        digitalWrite(ledPinA, HIGH);
        digitalWrite(ledPinB, LOW);
        digitalWrite(ledPinC, LOW);
        digitalWrite(ledPinD, LOW);
        digitalWrite(ledPinE, LOW);
        digitalWrite(ledPinF, LOW);
        digitalWrite(ledPinG, LOW);
        break;
      case 1:
        digitalWrite(ledPinA, LOW);
        digitalWrite(ledPinB, HIGH);
        digitalWrite(ledPinC, LOW);
        digitalWrite(ledPinD, LOW);
        digitalWrite(ledPinE, LOW);
        digitalWrite(ledPinF, LOW);
        digitalWrite(ledPinG, LOW);
        break;
      case 2:
        digitalWrite(ledPinA, LOW);
        digitalWrite(ledPinB, LOW);
        digitalWrite(ledPinC, HIGH);
        digitalWrite(ledPinD, LOW);
        digitalWrite(ledPinE, LOW);
        digitalWrite(ledPinF, LOW);
        digitalWrite(ledPinG, LOW);
        break;
      case 3:
        digitalWrite(ledPinA, LOW);
        digitalWrite(ledPinB, LOW);
        digitalWrite(ledPinC, LOW);
        digitalWrite(ledPinD, HIGH);
        digitalWrite(ledPinE, LOW);
        digitalWrite(ledPinF, LOW);
        digitalWrite(ledPinG, LOW);
        break;
      case 4:
        digitalWrite(ledPinA, LOW);
        digitalWrite(ledPinB, LOW);
        digitalWrite(ledPinC, LOW);
        digitalWrite(ledPinD, LOW);
        digitalWrite(ledPinE, HIGH);
        digitalWrite(ledPinF, LOW);
        digitalWrite(ledPinG, LOW);
        break;
      case 5:
        digitalWrite(ledPinA, LOW);
        digitalWrite(ledPinB, LOW);
        digitalWrite(ledPinC, LOW);
        digitalWrite(ledPinD, LOW);
        digitalWrite(ledPinE, LOW);
        digitalWrite(ledPinF, HIGH);
        digitalWrite(ledPinG, LOW);
        break;
      case 6:
        digitalWrite(ledPinA, LOW);
        digitalWrite(ledPinB, LOW);
        digitalWrite(ledPinC, LOW);
        digitalWrite(ledPinD, LOW);
        digitalWrite(ledPinE, LOW);
        digitalWrite(ledPinF, LOW);
        digitalWrite(ledPinG, HIGH);
        break;
      case 7:
        digitalWrite(ledPinA, LOW);
        digitalWrite(ledPinB, LOW);
        digitalWrite(ledPinC, LOW);
        digitalWrite(ledPinD, LOW);
        digitalWrite(ledPinE, LOW);
        digitalWrite(ledPinF, LOW);
        digitalWrite(ledPinG, LOW);
        break;
    }
    previousMillis = millis();
  }
}

void handleLedsFAST()
{

  static uint32_t previousMillis = 0;
  static byte state = 7;
  if (millis() - previousMillis >= intervalFAST[state])
  {
    // it's time for next state
    state++;
    state = state % 8;
    Serial.print(F("state=")); Serial.println(state);

    // act according state
    switch (state)
    {
      case 0:
        digitalWrite(ledPinA, HIGH);
        digitalWrite(ledPinB, LOW);
        digitalWrite(ledPinC, LOW);
        digitalWrite(ledPinD, LOW);
        digitalWrite(ledPinE, LOW);
        digitalWrite(ledPinF, LOW);
        digitalWrite(ledPinG, LOW);
        break;
      case 1:
        digitalWrite(ledPinA, LOW);
        digitalWrite(ledPinB, HIGH);
        digitalWrite(ledPinC, LOW);
        digitalWrite(ledPinD, LOW);
        digitalWrite(ledPinE, LOW);
        digitalWrite(ledPinF, LOW);
        digitalWrite(ledPinG, LOW);
        break;
      case 2:
        digitalWrite(ledPinA, LOW);
        digitalWrite(ledPinB, LOW);
        digitalWrite(ledPinC, HIGH);
        digitalWrite(ledPinD, LOW);
        digitalWrite(ledPinE, LOW);
        digitalWrite(ledPinF, LOW);
        digitalWrite(ledPinG, LOW);
        break;
      case 3:
        digitalWrite(ledPinA, LOW);
        digitalWrite(ledPinB, LOW);
        digitalWrite(ledPinC, LOW);
        digitalWrite(ledPinD, HIGH);
        digitalWrite(ledPinE, LOW);
        digitalWrite(ledPinF, LOW);
        digitalWrite(ledPinG, LOW);
        break;
      case 4:
        digitalWrite(ledPinA, LOW);
        digitalWrite(ledPinB, LOW);
        digitalWrite(ledPinC, LOW);
        digitalWrite(ledPinD, LOW);
        digitalWrite(ledPinE, HIGH);
        digitalWrite(ledPinF, LOW);
        digitalWrite(ledPinG, LOW);
        break;
      case 5:
        digitalWrite(ledPinA, LOW);
        digitalWrite(ledPinB, LOW);
        digitalWrite(ledPinC, LOW);
        digitalWrite(ledPinD, LOW);
        digitalWrite(ledPinE, LOW);
        digitalWrite(ledPinF, HIGH);
        digitalWrite(ledPinG, LOW);
        break;
      case 6:
        digitalWrite(ledPinA, LOW);
        digitalWrite(ledPinB, LOW);
        digitalWrite(ledPinC, LOW);
        digitalWrite(ledPinD, LOW);
        digitalWrite(ledPinE, LOW);
        digitalWrite(ledPinF, LOW);
        digitalWrite(ledPinG, HIGH);
        break;
      case 7:
        digitalWrite(ledPinA, LOW);
        digitalWrite(ledPinB, LOW);
        digitalWrite(ledPinC, LOW);
        digitalWrite(ledPinD, LOW);
        digitalWrite(ledPinE, LOW);
        digitalWrite(ledPinF, LOW);
        digitalWrite(ledPinG, LOW);
        break;
    }
    previousMillis = millis();
  }

}



void setup() {
  myservo.attach(3);
  Serial.begin(9600);
  pinMode(3, OUTPUT); // initialize the servo pin as an output
  pinMode(buttonApin, INPUT_PULLUP); // initialize the button A pin as an input
  pinMode(buttonBpin, INPUT_PULLUP); // initialize the button B pin as an input
  pinMode(ledPinA, OUTPUT); // initialize the LED1 pin as an output
  pinMode(ledPinB, OUTPUT); // initialize the LED2 pin as an output
  pinMode(ledPinC, OUTPUT); // initialize the LED3 pin as an output
  pinMode(ledPinD, OUTPUT); // initialize the LED4 pin as an output
  pinMode(ledPinE, OUTPUT); // initialize the LED5 pin as an output
  pinMode(ledPinF, OUTPUT); // initialize the LED6 pin as an output
  pinMode(ledPinG, OUTPUT); // initialize the LED7 pin as an output

  pinMode(12, OUTPUT);    // sets the digital pin 12 as output for 2 SLIDE switch LEDS

  pinMode(ledPinRA, OUTPUT); // initialize the LED pin as an output
  pinMode(ledPinRB, OUTPUT); // initialize the LED pin as an output

  MP3Serial.begin(9600);
  myPlayer.init(PIN_sound_BUSY);      // Init the player with the MP3 BUSY pin connected to Arduino pin defined
}

void loop() {

  digitalWrite(12, HIGH); // sets the digital pin 12 on

  buttonAstate = digitalRead(buttonApin); // read the state of button A value
  buttonBstate = digitalRead(buttonBpin); // read the state of button B value

  // servo position
  if ((buttonAstate == HIGH ) && (buttonBstate == HIGH )) {
    myservo.write(750); // SEVRO parked at 0 deg

    myPlayer.playFile(1, 10); // play track 1 at volume 10

    handleLedsSLOW();  // start the LED sequence

    digitalWrite(ledPinRA, LOW); //  LED off

    digitalWrite(ledPinRB, LOW); // LED off
  }
  lastButtonState = (buttonAstate) && (buttonBstate);

  //---------------------------------------------------------------

  if (buttonAstate != lastButtonState) { // check if button A is pressed
    myservo.write(1042); // rotate servo to 35 deg

    myPlayer.playFile(2, 10); // play track 2 at volume 10

    handleLedsMEDIUM();  // start the LED sequence

    digitalWrite(ledPinRA, HIGH); //  LED on
  }
  lastButtonState = buttonAstate;

  //----------------------------------------------------------------


  if (buttonBstate != lastButtonState); { // check if button B is pressed
    myservo.write(1442); // rotate servo to 83 deg

    myPlayer.playFile(3, 10); // play track 3 at volume 10

    handleLedsFAST();  // checks if something is todo with the LEDs

    digitalWrite(ledPinRB, HIGH); // start the LED sequence
  }
  lastButtonState = buttonBstate;

  delay(100);                // waits for a second

}
[/code]

I am late to the party.
What does Digital Pin 12 do?
pinMode(12, OUTPUT); // sets the digital pin 12 as output for 2 SLIDE switch LEDS

This is 0.1 second.
delay(100); // waits for a second

I have never used this player, but I see that every tenth of a second you are telling it to play track 1, volume 10. Wouldn't that restart?

I am not following your logic- how is this supposed to work?
if (buttonAstate != lastButtonState) { // check if button A is pressed