Unit 6: Lesson 15 Interactive Art

Here is student work:

Here is my work:

What I expect to happen:
We want the light sensor to activate a song that plays only once

What actually happens:
The songs seem to loop and causes an echo.

What I’ve tried:
I tried to use a variable to activate the sound to start once, however, it won’t start again due to they way I used the variable.

Goal:
When the lights in the room turn on, play music and turn on LEDs. When the lights in the room turn off, the music stops, the LEDs turn off.

@bboos,

Fun project. I looked at your project as that seems to be where your question is coming from.

You did the right thing by adding a variable to track whether or not the sound should start and that appears to be working correctly. However, once the sound starts, the condition that allows it to start (the light sensor being over a certain value) is still true, so it starts over and over and over and over again which is creating the repetition and echo you are experiencing.

There are probably multiple solutions, but I chose to add a 2nd variable called “played” and I initially set it to false. Then, inside the conditional that checks if the sound should play, I added a 2nd conditional to see if it has already played. If not, I will play the sound and immediately set the played variable to true.

The next time it encounters that task (because the light sensor threshhold is still met), it checks the played variable and finds out it has been played, so it doesn’t play the sound again.

Then, in the 2nd light sensor conditional that checks if the light is low again, I set the played variable back to false. If I remove my hand from the circuit playground, the light comes back and since the played variable is false, the sound starts up again.

Here’s a screenshot of what I did. (I didn’t include the screenshot of where I declared the played variable, but it’s right below the SoundStarted variable. I also changed the threshholds for my own testing purposes so it starts if the lightSensor is over 8 and it stops if the lightSensor is below or equal to zero.

Screen Shot 2022-12-06 at 3.58.49 PM

In regards to your students project, I would ask one question about lines 4 and 8. What will happen if the lightSensor is greater than 10 but less than 14. There are conflicting instructions for when that happens. One of them will win out. Which one?

Check back in if you still have questions or can’t get it to work!

Mike