It's time to make our own changes to the Fire-EdUp Platform's code! In this guide we'll modify the behaviour of the platform to create an audible alert under certain conditions.
Follow along with the video below, and see the references below for a deeper breakdown on what's going on in the code.
Code Reference
We used some new code in this guide, let's take a closer look at each idea as it was introduced.
.tone(frequency= , duration=)
The .tone() method plays a tone at the desired frequency (Hertz) for the specified duration (milliseconds).
firedup.tone(frequency = 262, duration = 200) # play a 262 Hz tone for 200ms
The above snipped plays a 262 Hz tone for 200ms. 262 Hz is the middle C note on a piano. You might want to try other frequencies that match musical notes. To play a short melody you can even play multiple notes sequentially!
.calculate_rating(data)
The .calculate_rating() method reads all slider values that are stored in a variable called data, and outputs one of the following Strings:
- "no rating"
- "moderate"
- "high"
- "extreme"
- "catastrophic"
Here the rating is stored in a variable called fire_danger_rating.
We can print the rating using print(fire_danger_rating)
if
"If statements" are used to make decisions in code.
We can use if to compare the value stored in fire_danger_rating against another string, and if the strings match we can execute some code to sound the audible alert. If there is no match, nothing happens.
To check if two things are the same in Python we use the double-equals operator: ==
The code that is executed when the condition is True has to be indented under the if statement. You can see examples of this below.
Try these examples in a new script to get a feel for how if statements work. You can see that here we're comparing numbers instead of strings.
A = 1 B = 2 if A == 1: print("yes, A is 1") if A == B: print("this won't ever print") else: print("but this will! because A is different to B") # We can also compare values if A < B: print("A is less than B") if B > A: print("or in other words, B is greater than A")
What Next?
Continue on to the next article where we will change some more advanced parameters of the Fire-EdUp platform: Models and Data!