Lightsaber

I've been trying to program an arduino mini 3.3v to be able to turn on the LED and then on the press of a second button, flash the clash led.

this is what I have so far (it will turn on and flash on clash but I can't get the thing to turn off)

int LEDpin = 9; // LED on pin 5 int switchPin = 3; // momentary switch on 13, other side connected to ground int clashButton = 10; int clashLed = 6; int buttonState = 0;

boolean running = false;

void setup() { pinMode(LEDpin, OUTPUT); pinMode(switchPin, INPUT); pinMode(clashLed, OUTPUT); pinMode(clashButton, INPUT); digitalWrite(switchPin, HIGH); digitalWrite(clashButton, HIGH); }

void loop() { if (digitalRead(switchPin) == LOW) { // switch is pressed - pullup keeps pin high normally delay(50); // delay to debounce switch running = !running; // toggle running variable while(true) { delay(50);digitalWrite(LEDpin, running); delay(150); digitalWrite(LEDpin, LOW); delay (20); digitalWrite(LEDpin, running); // indicate via LED buttonState = digitalRead(clashButton); if (buttonState == HIGH){ digitalWrite(clashLed, LOW); } else { digitalWrite(clashLed,HIGH);

} } }}

This is the first step, then I will want the clash to trigger from input from a sensor and then later I want it to pull sound from a WT588D but that is later steps. Anyone have an idea on how to fix this basic code? then I will tackle the other coding.

Ok, was able to figure it out on my own. Here’s the initial code I have. It turns on a pulsing LED, the button (momentary) turns on the LED and then turns it off and Clash works.

int LEDpin = 9;       // LED on pin 9
int switchPin = 3;   // momentary switch on 3, other side connected to ground
int clashButton = 10;
  int clashLed = 6;
  int buttonState = 0;
int buttonPushCounter = 0;
int buttonState1 = 0;
int lastButtonState = 0;

boolean running = false;

void setup()
{
  pinMode(LEDpin, OUTPUT);
  pinMode(switchPin, INPUT);
  pinMode(clashLed, OUTPUT);
  pinMode(clashButton, INPUT);
  digitalWrite(switchPin, HIGH);
  digitalWrite(clashButton, HIGH);
  
}


void loop(){
  for (int fadeValue = 0; fadeValue <= 255; fadeValue +=5){
    
buttonState = digitalRead(switchPin);
  buttonState1 = digitalRead(clashButton);
  if(buttonState !=lastButtonState){
    if(buttonState == HIGH){
      buttonPushCounter++;
    }delay(50);
  }
  lastButtonState = buttonState;
  if (buttonPushCounter %2 == 0){
    analogWrite(LEDpin, fadeValue);
    delay(30);
  }else{
    digitalWrite(LEDpin,LOW);}
  {

if (buttonState1 == HIGH){
  analogWrite(clashLed, LOW);
  delay(75);
  
} else {
  digitalWrite(clashLed,HIGH);
 

}
  }
}
  }

Now the next step. If I program the WT588D unit with sound files in different playlists, how do I add that code in here and tell it to play the different playlists for different events?

I forsee the following playlists

0 Bootup sound
1 Lightsaber turn on (button pressed)
2 Hum (constant sound played after 1)
3 Swing (sound to play over the top of 2)
4 Clash/Lockup (to play when Clash sensor registers a hit or I hold down Clash button)
5 Power Off

If anyone has any ideas on how to do this, I’d greatly appreciate it

Try my debouncer:

Better than a delay IMO.

I will try that when I get my Uno test board, currently I am using a virtual breadboard at https://123d.circuits.io/

and I don't think it allows me to load custom items into its setup for testing.

So help me understand why this would be better than delay? Is it that it will be able to run simultaneously in a smoother way, whereas delay literally causes it to pause and has to finish its action first?

Ok, this next step has me completely lost. How can I tell the Arduino to call for a specific audio clip from the WT588D-U when a certain variable is true? I was looking at SugarBomb’s post and it says that it will cycle through the audio list, but that’s not what I want, I want it to play certain ones on command and am not sure how to do that exactly. here’s Sugarbomb’s code

/* 

This program cycles through the first eight files loaded onto a WT588D-16P Sound Module

The pins labeled on the data sheet as CLK, DI, DO, CS, REST are for programming
the module via the external USB programmer.

In the Waytronic VoiceChip beta software, the settings are configured as "Control Mode: Three Line Mode, and Busy Mode: LOW"


*/

#define WT588D_RST 7 //Module pin "REST" or pin # 1
#define WT588D_CS 6 //Module pin "P02" or pin # 11
#define WT588D_SCL 9 //Module pin "P03" or pin # 10
#define WT588D_SDA 8 //Module pin "P01" or pin # 9
#define WT588D_BUSY 10 //Module pin "LED/BUSY" or pin # 15

byte file_count = 1;

void setup() {

  pinMode(WT588D_RST, OUTPUT);  
  pinMode(WT588D_CS, OUTPUT); 
  pinMode(WT588D_SCL, OUTPUT); 
  pinMode(WT588D_SDA, OUTPUT); 
  pinMode(WT588D_BUSY, INPUT);  

  digitalWrite(WT588D_CS, HIGH);
  digitalWrite(WT588D_RST, HIGH);
  digitalWrite(WT588D_SCL, HIGH);

}


void loop()
{

  WT588D_Send_Command(file_count);

  file_count++;

  if(file_count == 8) file_count = 0;

  delay(50); //give the module time to start playing

  while(digitalRead(WT588D_BUSY) == 0) { }

  delay(200); 

}


void WT588D_Send_Command(unsigned char addr) {

  unsigned char i;

  digitalWrite(WT588D_CS, LOW); 

  delay(5); //delay per device specifications

  for( i = 0; i < 8; i++)  {    
    digitalWrite(WT588D_SCL, LOW);    
    if(bitRead(addr, i))digitalWrite(WT588D_SDA, HIGH);
    else digitalWrite(WT588D_SDA, LOW);          
    delay(2);  //delay per device specifications   
    digitalWrite(WT588D_SCL, HIGH);    
    delay(2);  //delay per device specifications 
  } //end for

  digitalWrite(WT588D_CS, HIGH);

} //end WT588D_Send_Command

I am just looking at this and my head feels like it’s going to explode. I can understand the void setup part, but the #define has me scratching my head especially with the notes coming after it. Can someone explain what it means? How can I speficy which file to play for a specific set of variables? This code doesn’t seem to cover that.

Ok, here’s where I am now. I have the LED configured to turn on and pulse on input from a momentary switch, the clash LED is accepting inputs from the Aux button and the clash sensor. Now the audio…the bane of my existence…The problem I am having is that the sound comes out garbled and runs together in an unintelligible clipping. I know I must have the code wrong somewhere, could anyone offer me a suggestion? I am using a WT588D-U set in 3-Line Serial mode, the sound files are WAV set at 16-bit at 22000hz. Now the code:

int LEDpin = 9;       // LED on pin 9
int switchPin = 3;   // momentary switch on 3, other side connected to ground
int clashButton = 10;
int clashLed = 6;
int buttonState = 0;
int buttonPushCounter = 0;
int buttonState1 = 0;
int lastButtonState = 0;
int clashPin = 2;
int clashSense = 0;



const int groundpin = 18;             // analog input pin 4 -- ground
const int powerpin = 19;              // analog input pin 5 -- voltage
const int xpin = A3;                  // x-axis of the accelerometer
const int ypin = A2;                  // y-axis
const int zpin = A1;                  // z-axis (only on 3-axis models)

#define WT588D_RST 7 //Module pin "REST" or pin # 1
#define WT588D_CS 12 //Module pin "P02" or pin # 11
#define WT588D_SCL 11 //Module pin "P03" or pin # 10
#define WT588D_SDA 13 //Module pin "P01" or pin # 9
#define WT588D_BUSY 4 //Module pin "LED/BUSY" or pin # 15

byte file_count = 1;

void setup()
{
  pinMode(LEDpin, OUTPUT);
  pinMode(switchPin, INPUT);
  pinMode(clashLed, OUTPUT);
  pinMode(clashButton, INPUT);
  pinMode(clashPin,INPUT);
  digitalWrite(switchPin, HIGH);
  digitalWrite(clashButton, HIGH);
  digitalWrite(clashPin,HIGH);
  
  
  

// WT588D-U
  pinMode(WT588D_RST, OUTPUT);  
  pinMode(WT588D_CS, OUTPUT); 
  pinMode(WT588D_SCL, OUTPUT); 
  pinMode(WT588D_SDA, OUTPUT); 
  pinMode(WT588D_BUSY, INPUT); 

  digitalWrite(WT588D_CS, HIGH);
  digitalWrite(WT588D_RST, HIGH);
  digitalWrite(WT588D_SCL, HIGH); 

  pinMode(groundpin, OUTPUT);
  pinMode(powerpin, OUTPUT);
  digitalWrite(groundpin, LOW);
  digitalWrite(powerpin, HIGH);

// ADXL335
  analogReference(DEFAULT);

  pinMode(xpin, INPUT);
  pinMode(ypin, INPUT);
  pinMode(zpin, INPUT);
}

void WT588D_Send_Command(unsigned char addr) {

  unsigned char i;

  digitalWrite(WT588D_CS, LOW); 

  delay(5); //delay per device specifications

  for( i = 0; i < 8; i++)  {    
    digitalWrite(WT588D_SCL, LOW);    
    if(bitRead(addr, i))digitalWrite(WT588D_SDA, HIGH);
    else digitalWrite(WT588D_SDA, LOW);          
    delay(2);  //delay per device specifications   
    digitalWrite(WT588D_SCL, HIGH);    
    delay(2);  //delay per device specifications 
  } //end for

  digitalWrite(WT588D_CS, HIGH);

} //end WT588D_Send_Command

void loop(){
  
  {
{
  int x = analogRead(xpin);

  //add a small delay between pin readings.  I read that you should
  //do this but haven't tested the importance
    delay(1); 

  int y = analogRead(ypin);

  //add a small delay between pin readings.  I read that you should
  //do this but haven't tested the importance
    delay(1); 

  int z = analogRead(zpin);

  //zero_G is the reading we expect from the sensor when it detects
  //no acceleration.  Subtract this value from the sensor reading to
  //get a shifted sensor reading.
  float zero_G = 512.0; 

  //scale is the number of units we expect the sensor reading to
  //change when the acceleration along an axis changes by 1G.
  //Divide the shifted sensor reading by scale to get acceleration in Gs.
  float scale = 102.3;

{
  for (int fadeValue = 0; fadeValue <= 255; fadeValue +=5){
    
buttonState = digitalRead(switchPin);
  buttonState1 = digitalRead(clashButton);
  clashSense = digitalRead(clashPin);
 
  
  if(buttonState !=lastButtonState){
    if(buttonState == HIGH){
      buttonPushCounter++;
    }delay(50);
  }
  lastButtonState = buttonState;
  if (buttonPushCounter %2 == 0){
    analogWrite(LEDpin, fadeValue);
    delay(50);
    WT588D_Send_Command(0x01);
    
  }else{
    digitalWrite(LEDpin,LOW);}

if (buttonState1 == HIGH){
  digitalWrite(clashLed, LOW);
  
  
} else {
  digitalWrite(clashLed,HIGH);

}
 }
if (clashSense == HIGH){
  digitalWrite(clashLed,LOW);
  
  } else {
  digitalWrite(clashLed,HIGH);


  }  }  }  }  }