I am using a switch case to trigger a file on my waveshield - the problem I am having is that as far as I can tell, it keeps running the case, sending a constant stream of ones, which has the effect of stuttering it.
What happens is that when I go into the 'off' switch case, it actually run the sample completely, because it stops sending the 'on' trigger constantly.
What I am asking is this: is there any way to make a switch case execute only once, then stop executing? Here is my switch case - it:
int playtrigger2() {
int trig;
switch(playpauseswitch) { // all-off
case 0: trig = 1; Serial.println("play"); break;
case 1: trig = 0; Serial.println("stop"); break;
}
return trig;
Good programming practice would say to add a default case to your [switch/i] statement to catch any unexpected value of playpauseswitch: ```
*int playtrigger2() {
int trig;
switch(playpauseswitch) { // all-off
case 0: trig = 1; Serial.println("play"); break;
case 1: trig = 0; Serial.println("stop"); break;
default: trig = 999; Serial.println("uh-oh"); break;
}
return trig;
}* * *Your function could also be made even simpler and cleaner by eliminating the reference to the global variable inside the function and passing it in as an argument, if you guarantee that playpauseswitch was always a boolean (1/0) value.* *Like this:* *
*int playtrigger2 (int switch) {
Andy - I will update my arduino code tomorrow and post back whether or not the default setting fixes things - I have to admit that I am not sure I understand what you are doing with the boolean bit - though yes, trigger should indeed be a two state value.
Roy - thanks for looking - I am calling the method with another method which alternates button states, onpauseswitch(), which is being run in the main loop:
void onpauseSwitch()
{
int val;
int val2;
val = digitalRead(playpin); // read input value and store it in val
delay(0); // 10 milliseconds is a good amount of time
val2 = digitalRead(playpin); // read the input again to check for bounces
if (val == val2) { // make sure we got 2 consistant readings!
if (val != buttonState) { // the button state has changed!
if (val == LOW) { // check if the button is pressed
if (playpauseswitch == 0) { // if its off
playpauseswitch = 1; // turn lights on!
} else {
if (playpauseswitch == 1) { // if its all-on
playpauseswitch = 0; // make it blink!
}
}
}
}
}
buttonState = val; // save the new state in our variable
}
I have to admit that I am not sure I understand what you are doing with the boolean bit - though yes, trigger should indeed be a two state value.
It's kind of a trick, relying on the fact that you are using 0 and 1 as the state values for your switch. A boolean variable is defined as one that can be either TRUE or FALSE, so for example:
bool paused = TRUE;
bool buttonPressed;
means you can say
if (paused) then ...
or
if (paused && buttonPressed) then ...
The "!" operator is NOT, which means the boolean opposite ( ! TRUE == FALSE; ! FALSE == TRUE).
So I assigned the opposite of the input parameter to the result of the function, which just toggles it from TRUE to FALSE, or vice versa.
And finally, this works with int, because TRUE is actually equal to 1 and FALSE is zero. In fact , any even value of an int is equivalent to FALSE and any odd value is equivalent to TRUE.
So, a more proper function to toggle a boolean input to its opposite would look like:
bool toggle (bool b) {
return (! b);
}
or you could just do it in an assignment statement:
b = !b;
And by the way, in normal C, these variables are declared as type "boolean," not just "bool."
You may have already sorted this out but if not maybe the following will help.
What I usually do - automatically in my head for simple stuff and actually type it out for more complicated stuff - is to rough out some pseudo code so I get a rough idea of the scope of the task before diving into coding it. From what you have posted, it sounds like you want a simple toggle type switch to play/stop something (light, audio, whatever).
So the pseudo code would be something like:
if switched pressed, then
if playing then
stop
else
play
end if
end if
The tricky part is since your function is called through loop() and can execute many times a second, you can end up starting and stopping over and over and end up stuttering, etc.
So you need to keep track of your button pin state and only call your toggle() function when the button pin state is FALSE (i.e. button not pressed).
So some rough code might be
int buttonPin = 1;
boolean lastButtonPinState = 0;
boolean trigger = 0;
void setup(){
//do some set up
}
void loop(){
if (lastButtonPinState == 0){
//only read button state if last state was FALSE
boolean buttonState = digitalRead(buttonPin);
if (buttonState == 1){
// discrete button press so do something
trigger != trigger;
if (trigger == 1){
///play
}
else {
///stop
}
}
}
//store buttonPinState
lastButtonPinState = digitalRead(buttonPin);
}
Hello - just want to thank the both of you for your help, and also to confirm that I did get it working as desired - after several readings, I also now understand what is going on!
Just for the record, this didn't work in in switching the boolean:
trigger != trigger;