Why is it never running the opened toggle command?

Hello,

I am trying to help a student debug this toggleswitch issue. He wants the buzzer to buzz at different frequencies depending on the toggle switch (open or closed). I have simplified the code to paste here and shared the link (first time using this forum so I’m not sure if I’m linking the project correctly:

var A = createButton(“A1”);
onBoardEvent(A, “down”, function() {
if (toggleSwitch.isOpen) {
buzzer.frequency(3520, 100);
console.log(“playing open A”);
} else {
buzzer.frequency(220, 100);
console.log(“playing closed A”);
}
});

I used the console log and it’s always playing the “close A”… we can’t get it to run the “open” A (console being “on”)… no matter which side the toggle is set to.

Link here

Hi @dtran1,

Well this was a puzzle!! I tried to recreate the problem as best as I could ( I didn’t have alligator clips so I used the board buttons). I don’t think you can evaluate a BoardEvent with an if statement. With that said, I also had difficulty nesting two BoardEvents which makes me think the board can’t sense two BoardEvents at the same time? So…I was able to create a workaround by creating a variable that started at 0 and when the toggle switch was opened, it changed to 1 (and played one buzzer sound). THEN, if the variable was == 1, and the button was pressed, play a different sound.

I will say, the toggle switch had to be actively switched for the program to see whether it was open or closed. If I clicked “run” and it was open, I had to close and open it again for it to sense the toggle switch state. Those were my findings. I am sure there are other solutions but this is one I found:)

Hope that helps!
~Michelle

Thank you! I ended up creating a separate switch event where it changes the in-app text to Open and Close - this seems to actually trigger the switch change every time. As a result, when they try the buzzer it now works with its intended mode.

Thank you for your help and your time!