Help with lightsaber code

Hello, Ive been working on some code for an arduino controlled lightsaber. One of the members on here (Delta_G) has helped me get this bit going. What I want to do now is to activate a soundboard module everytime the led is switched on. I am unsure how to alter the code to do this, if anyone can help me I would be greatly apperchiated :slight_smile:

Code so far:

#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(ledPin == HIGH){   // and if the button is pressed
   
  sfx.playTrack("T01     WAV");
  
 sfx.playTrack("T02LATCHWAV");
}
}

I am aware that the void audio is probably wrong (newbie at this programming :P), so if anyone can help point me in the right direction would be great.

Thanks guys :smiley:

You currently never call the audio() function so even if were correct no audio will be played.

if(ledPin == HIGH){  // and if the button is pressedThis is not testing the state of the LED pin it is testing whether the number 11 is HIGH

sfx.playTrack("T01    WAV");Do you really have an audio file with that name ?

Yes I do have an audio file named that. Im using an adafruit sound board & amp, it uses an 8.3 character format so it needs to be named like that.
I had tried the code as it is there however the light was delayed coming on.
How would i change it to correct this, just change it to check the state not if the pin is high?

Hi Techyjoe,

What exactly is the problem? Is it that the LEDs aren't turning on? The audio isn't playing? Please specify.

Looking at the code, the thing that jumps out at me is that the audio is probably not going to play because you have a Software Serial initialized, but you're not sending any messages through the UART.

You have:

sfx.playTrack("T01 WAV");

sfx.playTrack("T02LATCHWAV");

But shouldn't it be:

ss.sfx.playTrack("T01 WAV");

Since "ss" is your instance of your UART? Where did "sfx" come from and how exactly should you communicate with it?

/Fred

I know it looks a little odd but the audio does work, its an adafruit board and thats how it I have been told to do from the instructions.

The problem is that I want to alter my code given, so that when the state of the led is high it plays one sfx followed by a delay then play a second on loop (the loop should be handled by the sound board as its named differently).
When the led state is low to play another sfx (lightsaber powering down). However I do not want it to play the powering down sfx when the arduino is initially powered (as it will see the led is in a low state on start up, but before the activation button is pressed.)

I am unsure how to alter my code to do this, and wonder if you would be able to offer me some guidence? :slight_smile: