hey gang-
Im editing the WaveHC/WaveShield examples I see posted.. to suit my needs.
I dont mess with the initial SD/partition reading stuff at the top of all the sketches..etc....until 'READY'..
in most (all) the examples..they use a 6 button configuration.. and use a 'loop' to keep checking the states of all the buttons...etc
(default examples here)
HI-
hey gang-
Im editing the WaveHC/WaveShield examples I see posted.. to suit my needs.
(default examples here)
I am looking for more discussion/feedback...even a more broader/general understanding on how the code executes..and moving outside of the loop..and back..etc..
my question (first general) is more about WHERE (or when) in the code we can actually perform other tasks???
this may be my lack of Arduino/C programming.. and having to keep the main loop going..etc..
but I cant seem to get another function (PWM fade out an LED).. to happen STANDALONE outside of the audio playing function(s)?
what I mean by this is...
from what I have read (and most of it confusing).. only certain pins/timers are left after the WaveShield/WaveHC lib..etc..
I have done some mods..and I have these pins free:
A0
A1
A2
A4 - I2C
A5 - I2C
D0 - RX
D1 - TX
D2
D3 - PWM
D5 - PWM
D6 - PWM
D9 - PWM
although I understand I cant use PIn 9 as it uses the same timer as the WaveHC lib.
I have Pin3, Pin5 & Pin6 open..
I think I understand the basics of the lib.. (setting/giving several button state reference to use in your playing of the audio file (looping, stopping, playing complete..etc)
what I cant grasp is how/where I can code so I can do OTHER tasks, while the audio stuff is going on.
Im not sure what or how MANY times the the shield/lib uses in total? Just one? or two?
Also from searching here (and google) and seeing the limited details given.. I have only seen suggestions/answers of:
you have a tight loop here:
while(wave.isplaying){
//do whatever
}
//is done playing now
to add my code.
Which Im assuming was meant for the playcomplete() function????
the problem is.. if you use the while(isplaying){} function in one of the playfile() or playcomplete() functions..
your are STUCK inside that code snippet/routine is seems.. ..even if the audio is done playing?
maybe having/trying a quick goal would be better to get the idea/point across.
using the waveHC lib examples.. I hacked together a simple variant..
that when button[0] is pressed... if calls a routine where I check for a HIGH/LOW value of another switch..
if HIGH...... if use the normal playfile() function...etc
if LOW... I then start a 'timer' (think blink without delay).. so I can repeatedly call this playfile() function...
basically giving me a normal firing sound.. or an autofire sound (if the button is held down)..
code example:
unsigned long currentAutoMillis = millis();
//main trigger button code/routine
if (justpressed[0]) {
if(digitalRead(modeSelect)== HIGH){
//reset button var/value to show not pressed
justpressed[0] = 0;
putstring_nl("slider is high");
playfile("1.WAV");
}
else if(digitalRead(modeSelect) == LOW){
//auto/burst mode
if(currentAutoMillis - previousAutoMillis > autoInterval) {
// save the last time you blinked the LED
previousAutoMillis = currentAutoMillis;
putstring_nl("!!FIRE!!......");
putstring_nl("slider is low");
//wave.stop(); //needed? better form? what about..breaks code.
playMain("1.WAV");
if(pressed[0] != justpressed[0]){
putstring_nl("button released");
//reset button var/value to show not pressed
justpressed[0] = 0;
}
}
}
}
this all seems to be working just fine.. (as far as sound effects and the button reading/states)..etc
the problem comes into play when I want to try and take this another step further.... and add a LED into the mix.. to visually represent the sound being played.. (basically turn an led on when the sound plays.. and fade it out quickly... if on auto fire.. the led will turn on and fade out (as far as it can) before the sound is played again)...
here are the default playcomplete() and playfile() functions given in the examples:
// Plays a full file from beginning to end
void playcomplete(char *name) {
// call our helper to find and play this name
playfile(name);
while (wave.isplaying) {
// do nothing while its playing
}
// now its done playing
}
void playfile(char *name) {
// see if the wave object is currently doing something
if (wave.isplaying) {// already playing something, so stop it!
//putstring_nl("stopping current audio..........");
wave.stop(); // stop it
}
// look in the root directory and open the file
if (!f.open(root, name)) {
putstring("Couldn't open file ");
Serial.print(name);
return;
}
// OK read the file and turn it into a wave object
if (!wave.create(f)) {
putstring_nl("Not a valid WAV");
return;
}
// ok time to play! start playback
wave.play();
}
if you add code to the:
while(wave.isplaying){}
section..
like.. lets say a loop to turn on the led (255)..and fade it down to 0..
you are stuck in that loop... even if you are in autofire mode..and should be firing over & over..and hence light up the led each time..
So my problem(s) outside of the above is understand where or how I can have other functions (like this led fade out for example) execute/run independently from this audio function??
a quick example would help turn the light on.. as I have tried moving blocks of code all over.. from using delays in the led fade out to using another milis() check....
from my end it doesnt HAVE to be connected to when the wave.isplaying...
it should just be a function that is called/executed when the file is triggered to play.. q quick blip..and fade out.. each time.. like for a prop blaster/gun toy..
suggestions on how I can go about accomplishing this?
thanks!