Lightsaber project help!

So far ive got the led star to light up and toggle its state with a push button. I am trying to work out how to play audio with my project. I want to trigget three sound effects with my adafruit board. One on start up (lightsaber power on), one right after this (ambient hum), and a third to power down when the button state is toggled low (however when the arduino powers on it is in a low state to begin with so I do not want any audio to be played then, only after if has been toggled high first.
Here is my code below, if anyone can help it would be brilliant as I am very new to programming and want to get this project finished ASAP (as my partner is heavily pregnant, id like to finish before the new arrival :slight_smile: )

Joe :slight_smile:

#include <SoftwareSerial.h>
#include "Adafruit_Soundboard.h"

int butPin = 2;
int ledPin = 11;
int led12v = 12;
int ledState = 0;  // led on or off
int lastButState = LOW;

#define SFX_TX 5
#define SFX_RX 6
#define SFX_RST 4

SoftwareSerial ss = SoftwareSerial(SFX_TX, SFX_RX);
Adafruit_Soundboard sfx = Adafruit_Soundboard(&ss, NULL, SFX_RST);



void setup(){
  
  pinMode(butPin, INPUT_PULLUP);
  pinMode(ledPin, OUTPUT);
  pinMode(led12v, OUTPUT);

ss.begin(9600);             //initialize Serial connection
//buttonState = digitalRead(buttonPin);  //read the initial state

digitalWrite(4, LOW);
  pinMode(4, OUTPUT);
  delay(10);
  pinMode(4, INPUT);
  delay(1000); // give a bit of time to 'boot up'
}

void loop(){
  
  
  
  int butState = digitalRead(butPin);  // read a new button state
  
  if(butState != lastButState){   // if the state of the button has changed
    if(butState == LOW) {   // and if the button is pressed
      ledState = 1 - ledState;  // button was pushed, toggle the state of the led]
     
    }
  }
  lastButState = butState;  // save the last button state
  
  digitalWrite(ledPin, ledState);  // write the state to the led
  digitalWrite(led12v, ledState);  // write the state to the led **
   
  delay(100);      //debounce time for button
 
}
 
 void audio()
{ 
  if(ledState == 1){   // and if the button is pressed
  
 sfx.playTrack("T01     WAV");
 sfx.playTrack("T02LATCHWAV");
}
}

Heres my code so far, may need to change some bits, please offera

Haven't I seen this question before, or something very like it ?

Yeah, im just pretty desperate to get this project finished while I can

If anyone can help that would be great :slight_smile:

Can anyone help me with this code? Please?

The audio() function is not actually inside the loop() function it's just that the code is badly formatted. However, as I pointed out in the OPs other thread on the same subject as the audio() function is never called then nothing will play anyway.

I would change the audio() function to take the filename as a parameter and call it with the appropriate filename at the required time. I believe that I asked in the other thread whether calling the audio() function as it is actually played the tracks specified. I don't know whether the audio playback is a blocking function or whether once started the track plays in the background.

Joe - can you please enlighten us with the answers to the questions above ?

Thanks for the advice guys. From what I gather it should be now this:

#include <SoftwareSerial.h>
#include "Adafruit_Soundboard.h"

int butPin = 2;
int ledPin = 11;
int led12v = 12;
int ledState = 0;  // led on or off
int lastButState = LOW;

#define SFX_TX 5
#define SFX_RX 6
#define SFX_RST 4

SoftwareSerial ss = SoftwareSerial(SFX_TX, SFX_RX);
Adafruit_Soundboard sfx = Adafruit_Soundboard(&ss, NULL, SFX_RST);



void setup(){
  
  pinMode(butPin, INPUT_PULLUP);
  pinMode(ledPin, OUTPUT);
  pinMode(led12v, OUTPUT);

ss.begin(9600);             //initialize Serial connection
//buttonState = digitalRead(buttonPin);  //read the initial state

digitalWrite(4, LOW);
  pinMode(4, OUTPUT);
  delay(10);
  pinMode(4, INPUT);
  delay(1000); // give a bit of time to 'boot up'
}


 void hum()
{  
 sfx.playTrack("T01     WAV");
 sfx.playTrack("T02LATCHWAV");
}



void loop(){
  
  
  
  int butState = digitalRead(butPin);  // read a new button state
  
  if(butState != lastButState){   // if the state of the button has changed
    if(butState == LOW) {   // and if the button is pressed
      ledState = 1 - ledState;  // button was pushed, toggle the state of the led]
    }   
     
      if(ledState == 0){   // and if the button is pressed
      hum();
       
    }
  
  lastButState = butState;  // save the last button state
  
  digitalWrite(ledPin, ledState);  // write the state to the led
  digitalWrite(led12v, ledState);  // write the state to the led **
   
  delay(100);      //debounce time for button
 
}
}

Yes of course, sorry UKHeliBob :frowning:

I believe that I asked in the other thread whether calling the audio() function as it is actually played the tracks specified. I don't know whether the audio playback is a blocking function or whether once started the track plays in the background.

I need to check on the audio function as to if it will overlap the tracks or stop one from playing when triggered. As soon as I try it shortly will let you know. Im using an adafruit sound board but havent use this one before. Also they claim by allocating each audio file with a certain name it can make the audio file loop. Again I need to experiment with this and will let you know as soon as I find out. If not I will have to take Delta_G's advice of calling each file from its own function (void thingy :P).

Sorry to be a pain guys, Bet your sick of me already :open_mouth:

Right it turns out that I need to loop the hum effect in the code. Would a while loop be best for this? Also the start up sfx plays instantly however the led is delayed by a second as it has to check its state first

Its incredibly crude but heres a schematic ive quickly knocked up

Would a while loop be best for this?

What's wrong with using the loop() function ?

If I can get the audio board to handle the looping then there will be no delay between it stopping and starting again. Just looking on the website atm, might be able to get it going by changing the file name. Will have a play about with that first.
But I still have the problem of getting the audio to play on cue in my code. The LED comes on too late, after playing the startup sfx. And still working on getting it to the hum and powerdown sfx correctly.

No looks like im going to have to loop the hum sfx in void loop.
Any advice how to do this, still playing around with code to get sounds & lights to start at correct times

Any advice how to do this,

Set a boolean variable, let's name it playHum, to true when you want the hum to play then in loop()

if (playHum)
  {
     //code here to play the hum or a call to a function to play it
  }

When you want the hum to stop set playHum to false.

Ive got a feeling ive once again gone wrong :confused:

#include <SoftwareSerial.h>
#include "Adafruit_Soundboard.h"

int butPin = 2;
int ledPin = 11;
int led12v = 12;
int ledState = 0;  // led on or off
int lastButState = LOW;

boolean playHum = false;      

#define SFX_TX 5
#define SFX_RX 6
#define SFX_RST 4

SoftwareSerial ss = SoftwareSerial(SFX_TX, SFX_RX);
Adafruit_Soundboard sfx = Adafruit_Soundboard(&ss, NULL, SFX_RST);



void setup(){
  
  pinMode(butPin, INPUT_PULLUP);
  pinMode(ledPin, OUTPUT);
  pinMode(led12v, OUTPUT);

ss.begin(9600);             //initialize Serial connection
//buttonState = digitalRead(buttonPin);  //read the initial state

digitalWrite(4, LOW);
  pinMode(4, OUTPUT);
  delay(10);
  pinMode(4, INPUT);
  delay(1000); // give a bit of time to 'boot up'
}


void powerup()
{
 sfx.playTrack("T01     WAV");
}

 void hum()
{  
 sfx.playTrack("T02HOLD WAV");
}

void powerdown()
{
 sfx.playTrack("T06     WAV");
} 
  
void loop(){
  
  int butState = digitalRead(butPin);  // read a new button state
  
  if(butState != lastButState){   // if the state of the button has changed
    if(butState == LOW) {   // and if the button is pressed
      ledState = 1 - ledState;  // button was pushed, toggle the state of the led]
    }   
     
      if(ledState == 0){   // and if the button is pressed
      powerup();
    }
    
    
    if (ledState == 0) 
    {
      delay(2500);
       boolean(playHum==true); 
    }
    
    if (playHum == true) 
    {
    hum();
      }    
    
  
   if(ledState == 1){   // and if the button is pressed again
      powerdown();    
    }
  
  lastButState = butState;  // save the last button state
  
  digitalWrite(ledPin, ledState);  // write the state to the led
  digitalWrite(led12v, ledState);  // write the state to the led **
   
  delay(100);      //debounce time for button
 
}
}

      boolean(playHum == true);Errr, no !
I assume you meant

static boolean playHum = true;

And why have 2 sections of code that test whether ledState is zero ?

Yeah the second one shouldnt have been there. Im unsure what I should have to be honest, utter noob I am lol.
should it be like this with the static boolean after the if statement?

    if  static boolean playHum = true;
    {
       hum();
    }

So like this essentially? :slight_smile:

static boolean playHum = true;

void setup(){}

void hum(){
 sfx.playTrack("T02HOLD WAV");}

void loop() {

if (playHum){
hum();
}

}
    if  static boolean playHum = true;

Would not work because of the single =
In your second program, playHum does not need to be declared static if it is going to be a global variable.

Oh so just a case of adding an '=' then? :slight_smile:

static boolean playHum == true;

void setup(){}

void hum(){
sfx.playTrack("T02HOLD WAV");}

void loop() {

if (playHum){
hum();
}

}