Picking up where we left off in the last video, I've got my Fire-EdUp platform connected to Thonny and we still have that main.py file that we created from the last video. I've opened that file, and you can see we're working on the main.py file stored on the Pico because it's got the square brackets up here.
This time around, we're going to play with the audible alert. The Fire-EdUp platform has an audible alert that you can program. So I propose that we program it to beep whenever it enters a catastrophic fire danger rating. Let's get started by beeping the audible alert just once on start-up to make sure we know how to drive the alert.
Returning to our code, this object "fired up" is how we control this whole platform. To create a tone on the audible alert, we insert "firedup.tone" and I'll put it up here above the while true because code that is outside this while true gets executed only once. "firedup.tone" is a method that accepts a frequency, and I'll enter 262 Hz and it plays a tone for a duration. Duration equals 200, and that's in milliseconds, so 0.2 seconds. If we run the code, it'll save and run the code and there it is. We got a single beep from our audible alert, and just for laughs, I'll double that frequency two times. Different tone. Great, we're halfway there already. We can create a tone. Now we just need to access whatever variable takes care of the fire danger rating.
Returning to the code, we can see that we collect data to calculate a fire danger rating. So we collect some data and then we use that data to calculate a rating. So this method "calculate rating" is returning the fire danger rating that we're interested in. I'll replace this print statement down here with a copy of that fire danger rating. So I'll just paste the name of this variable into the print statement. I run the code again we get our beep and now we have the rating coming out on the shell. And if I increase the fuel load we can see I can move that all the way up to catastrophic. We can go down to extreme, high, moderate and no rating. Great, we have access to the audible alert and to the fire danger rating. Now we can implement some logic to only beep this audible alert when we enter a catastrophic fire danger rating.
I'll create a new line and just write an if statement which checks the state of the fire danger rating. If fire danger rating is equal to catastrophic, because it really does return the word catastrophic, we put in a colon and we can take our tone line from here and cut that with control X and paste it with control V. Make sure it indents like this. You can make sure it's going to do that by going to the end of that line and striking enter. If fire danger rating is equal to catastrophic, play a tone. Let's run the script. We have no rating and if I floor the fuel value, okay that's quite annoying. It's beeping continuously and that's because this statement is true while ever the rating is catastrophic. But we only want to beep when we enter the catastrophic state. For that we're going to update our if statement.
We can say if fire danger rating is equal to catastrophic and fire it up dot rating has changed. So now we're checking two things. We're checking if we're in the catastrophic state and we're essentially checking if the rating has changed since the last time we checked it. Is this the first time that we've entered a new state? So we can combine logic like this using and and I'll run the script. We have no rating and I'll slide this fuel load up until we get to catastrophic. There it is. There's only one beep. I'll go back down. We're in high and then back up. Great. Now it's a matter of personal preference whether you like to combine your logic on a single line like this or you take that second condition. I'll cut that out and I'll enter a new line and make a new if statement. Then I have to indent my tone as well. So we can say if it's catastrophic that's true. So we go into this code and then we just check straight away if the rating has changed. This will also work in exactly the same way. It's kind of a matter of preference.
Now we can change this logic however we like. For example my challenge to you is to beep once when we enter the extreme state. This will work just fine. And now add your own conditions to play some kind of three or four tone melody when we go into catastrophic. So we'll have two different types of alert. A single beep for extreme and a more serious multi-tone melody for when we go into catastrophic.
So there you have it. With just a little bit of extra code we've been able to change the way the Fired Up platform behaves. And you can extend this logic if you like. You can put in more and more if conditions to make more and more complex behavior. Have a think about how you might implement more if conditions for those additional checks. See you in the next video.
Makers love reviews as much as you do, please follow this link to review the products you have purchased.