billpealer:
youtube video on its way. i am in better shape than yesterday. i know i am still doing it wrong.
first crack at Arduino and the WT588Du audio chip - YouTube
anyone have some insight on how to make the sounds trigger with buttons and not sound like max headrom, or act wonky?
Also, there needs to be a way that after the main and only button is pressed the second time, it locks up the sound module, so the swing and clash buttons don't trigger their noises. I am not there yet in my code.
maybe have the arduino get a feedback loop from the LED pin, if the LED pin is LOW, send the Stop Command(0xFE)
if (digitalRead(LED == LOW) {
WT588D_Send_Command(0xFE)
}
{
else
}
that's wrong im sure.,
tonight's flavor
#define WT588D_SDA 9 //Module pin "P03" or pin # 10
#define WT588D_BUSY 10 //Module pin "LED/BUSY" or pin # 15
int LED = 13;
int LEDa = 12;
int button = 2; // the button
int state = LOW; // the current state of the output pin
int reading; // the current reading from the input pin
int previous = HIGH; // the previous reading from the input pin
byte file_count = 1;
long time = 0; // the last time the output pin was toggled
long debounce = 100; // the debounce time, increase if the output flickers
void setup() {
pinMode(WT588D_SDA, OUTPUT);
pinMode(WT588D_BUSY, INPUT);
pinMode(button, INPUT_PULLUP);
pinMode(LED, OUTPUT);
pinMode(LEDa, OUTPUT);
digitalWrite(button, HIGH);
digitalWrite(WT588D_SDA, HIGH);
}
void loop()
{
reading = digitalRead(button);
digitalWrite(WT588D_SDA, HIGH);
// if the input just went from LOW and HIGH and we've waited long enough
// to ignore any noise on the circuit, toggle the output pin and remember
// the time
if (reading == HIGH && previous == LOW && millis() - time > debounce) {
if (state == HIGH)
state = LOW;
else
state = HIGH;
WT588D_Send_Command(0x02); //on sound
delay(5);
digitalWrite(WT588D_SDA, LOW);
time = millis();
}
digitalWrite(LED, state);
digitalWrite(LEDa, !state);
if (reading == LOW && previous == HIGH && millis() - time > debounce) {
if (state == HIGH)
state = LOW;
else
state = HIGH;
WT588D_Send_Command(0x00);
time = millis();
}
previous = reading;
}
void WT588D_Send_Command(byte addr) {
digitalWrite(WT588D_SDA, LOW);
delay(5);
for(int i = 0; i < 8; i++) {
digitalWrite(WT588D_SDA, HIGH);
if(bitRead(addr, i)) {
delayMicroseconds(600);
digitalWrite(WT588D_SDA, LOW);
delayMicroseconds(200);
} else {
delayMicroseconds(200);
digitalWrite(WT588D_SDA, LOW);
delayMicroseconds(600);
}
}
digitalWrite(WT588D_SDA, HIGH);
delay(100);
} //end WT588D_Send_Command
Finally, somebody posts a video! Thank you, Bill!
I took a look at your code. You aren't terribly far off. Believe it or not, I think you have too much code.
-
All of that stuff you are doing with millis() to try and debounce the button signal is not necessary. The cheap and dirty way to avoid all of that is to simply add 20 or 30 ms delay in your loop() function. That way, the button is only read every 20 milliseconds or so and jittery signal should be mitigated.
-
Add a global bool variable to keep track of your lightsaber state.
bool saber_is_on;
...
setup()
{
...
saber_is_on = false;
...
}
- Then make your loop() function look something like this:
loop()
{
if(digitalRead(button) == LOW) //button is pressed
{
if(saber_is_on) //Saber is on, so turn it off
{
//ADD CODE HERE TO TURN OFF THE LED AND PLAY OFF SOUND
saber_is_on = false;
}
else //Saber is off, so turn it on
{
//ADD CODE HERE TO TURN ON LED AND PLAY ON SOUND
saber_is_on = true;
}
}
}
- Take out those digitalWrite(WT588D_SDA...) lines in your loop() function. You don't need those there; they aren't doing anything useful.