Circuit playground code

Hello. I am new to coding. I am an elementary teacher helping students with coding a circuit playground to pick up breathing. We are using Adafruit makecode because of the age group that I am working with. We have used the sound sensor to pick up breathing. What we cannot figure out is how to chart or plot the breathing. The idea is that we can monitor so many breaths within a half-minute. If it is over a certain number, say 15, then an alarm will sound. We need support with the coding and the plotting. I cannot get the device console to show. If anyone could help, that would be great.

3 Likes

Hi Kathy,

I am more than happy to help. If you can share a picture of what you have so far (code and setup) that would be lovely. In the meantime I’ve got Adafruit’s page on plotting graphs in Makecode but I can also help out there once I have a bit more detail.

2 Likes

Thank you for your help. The code that I have attached picks up a student blowing on it. The idea is that it acts as if it is recording breathing. In normal breathing (while asleep), I think it is 40 breaths a minute, the red right should light up 40 times. The end goal is to be able to record rapid raspatory breathing. So, if the red light comes on 55 times in a minute, the red light should light up 55 times in a minute and an alarm with sound. I have no idea how to code that. That is what I need help with.

forever(function () {
light.graph(input.soundLevel(), 190)
})

As you can see, we only have the sound sensor to work. It only pickes up the breathing. We do not know how to code the 40 breaths in a minute and have an alarm sound if it lights up 55 times in a minute. I have copied the JavaScript, but we are using the block coding in class because I am working with elementary students.

1 Like

Hi Kathy,

You’re more than welcome. I can’t test this myself as I don’t have what I need to set it up so let me know how this goes. I’ve added comments to the code to explain each line.

let Breaths = 0                                     // Set variable breath equal to 0
for (let i = 0; i < 60; i++) {                      // For loop for timer. Each loop add 1 to i until i = 60
    console.logValue("Breaths", Breaths)            // Plot variable Breaths as "Breaths"
    if (input.soundLevel() > 190) {                 // If soundLevel is greater than 190 execute next line
        Breaths = Breaths + 1                       // Add 1 to breaths
    }
    pause(100)                                      // Pause 1 second
    if (Breaths >= 40) {                            // Execute commands if breathes greater than 40 or 55
        light.showRing(
        "red red red red red red red red red red"
        )
    }
    if (Breaths >= 55) {
        music.siren.play()
    }
}

And the block setup looks like this. I don’t know how I managed to have the sound level logic backwards here but not in Javascript. Sound level should be greater than and not less than in the if statement.

Let me know how that goes.

2 Likes

Hello,

Thank you for your help. As of now, the code is not reading within a minute, then repeating. For example, when I test the code on the simulator, it will count up to 55 breaths and sound the alarm. This will happen even if it is over a minute test. What I need is for the code to work for each minute. The goal is to pick up rapid breathing while sleeping. If the norm is 40 breaths within a minute then there is no alarm. But if there are 55 breaths within a minute the alarm sounds because it is picking up rapid breathing. I hope this make sense. If not please let me know.

8A328376665A4742A433D27FB0190FEC.png

1 Like
  1. Initialize a variable (let’s call it breathCount) to keep track of the number of breaths detected.
  2. Inside the loop, use the sound sensor to detect breathing.
  3. If the sound sensor indicates a breath, increment the breathCount by 1.
  4. Repeat this process for one minute.
1 Like

Hi @Kathy262403, here is my attempt that expands on @Jack’s - it loops forever and resets the count every 60 seconds.

let Breaths = 0
forever(function () {

    // Detect a breath and add to counter
    if (input.soundLevel() > 190) {
        Breaths = Breaths + 1
    }

    // Reset the counter every 60 seconds
    if (control.timer1.seconds() % 60 == 0) {
        Breaths = 0
    }

    // Show a warning light over 40 breath/min
    if (Breaths >= 40) {
        light.showRing("red red red red red red red red red red")
    }

    // Sound buzzer over 55 breath/min
    if (Breaths >= 55) {
        music.siren.play()
    }

    pause(200)
})

While the program is easy enough to describe, there will be certain edge-cases that are trickier to reject - take like long breaths counting as two breaths for example.
My naive approach uses pause(200) to set the sample time, and to help tune-out such double-counts. If you increase 200 to say, 500 or 1000 it will make the code reject such events at the cost of responsiveness.
See how it goes and let us know. Don’t be afraid to tune the constants like the pause or the thresholds used in each if statement. For example, you can reset the counter every 30 seconds by changing

if (control.timer1.seconds() % 60 == 0) {

to

if (control.timer1.seconds() % 30 == 0) {
1 Like