MISSION 20

Window Alarm

When windows are open, heat can escape. But fresh air is nice, even in the cold. We don’t want alarms going off just because a window is open. We just need to make sure that the window isn’t open for too long.

We can use the temperature sensor from Mission 17 and the door sensor from Mission 8 together to accomplish this mission.

Algorithm

We want the alarm to go off if the window is open and the temperature in the house has gone down since the last two readings.

Images

micro:bit

Images

Figure 20.1    Window set up with tinfoil.

Images

Figure 20.2    The micro:bit attached to the window with a speaker.

Build

Add the tinfoil and crocodile clips to the edge of your window. Follow the instructions from Mission 8 on how to do this.

Code

1.   Let’s grab the code from Mission 18 to get the temperature and add it to an array.

Images

Figure 20.3    Code from Mission 18.

2.   We’ll need to change this to record the temperature forever. Take out the code inside repeat 4 times and put it inside a forever block.

3.   Now let’s check whether the window is open or closed. We’re using the block running time (ms) from the menu Inputs then more.

4.   I’ve added the letters O and C to the code to let me know the position of the window.

5.   windowOpen is true when the window is open and false when the window is closed. The block on pin P1 released is under the menu Inputs then more.

Images

Figure 20.4    Setting the window state.

6.   Finally, if the window is open and our timer is over 15 minutes, get the last two temperatures from the array. I created variables LastTemp and SecondLastTemp to store these values.

In an array, variables have an address. This address tells us where a variable is. Only one variable can exist in one address. So it’s a unique way of finding a variable. We start counting addresses from 0. Here’s an array with addresses:

Images

The array size is 3: there are three variables in the array. The last variable in this array is at position 2, which is the size of the array minus 1. The second-to-last variable in this array is at position 1, which is the size of the array minus 2.

7.   I created another variable ArraySize to record the size of the array.

8.   If the window is open and the current time is greater than the timer + 15 minutes, get the last and second-to-last temperatures.

9.   Instead of checking if windowOpen = true, we can just use if windowOpen.

Images

Figure 20.5    Getting the last two temperatures.

10.   Check these temperatures against the current temperature.

11.   Signal the alarm.

Images

Figure 20.6    If it’s cold, sound the alarm!

Debug

We need to reset the timer back to the current running time, or else the alarm will go off constantly. Also, remember how we had to subtract an offset to get the real temperature?We don’t need to do this here, because we are just looking for the change in temperature. Here’s all the code together:

Images

Figure 20.7    Full bug-free code?

If your alarm never goes off, you could change the code. You could check only the last temperature rather than both the last and second to last. You could check after the window is open for an hour. My alarm only went off when it was getting dark and cooler outside, which is a good time to close your window.

There’s a second bug here that I’m not going to fix. If the window is open for 15 minutes and there is only one temperature recorded, what happens? You tell me!

In some programming languages, if you try to access an address that doesn’t exist in the array, the program crashes. What happens in MakeCode? Does it affect your program? If it doesn’t affect your program, should you fix it?

Expert Level

Oh wow, if you’ve created all that code and got it working with the window, I don’t think there is an expert level for you. You are an expert!

Circuit Playground Express

Build

Add the tinfoil and crocodile clips to the edge of your window and the frame. Follow the instructions from Mission 8 on how to connect the window and the speaker to the Circuit Playground Express.

Code

We’re basing this code on the code from Mission 18 to get the temperature and add it to an array. But there are a lot of changes too.

1.   We need to set pin A2 to pull up.

2.   I’m recording the temperature of the Circuit Playground Express, not the room. Remember how they’re different? Later on in the code, we will check the temperature twice. Instead of always subtracting the offset, I’m just checking the Circuit Playground Express temperature.

3.   Instead of just grabbing four temperature recordings, I’m going to run this code forever.

4.   In Mission 18, we had code under button A click. We don’t need that code here.

Images

Figure 20.8    Get the temperature.

5.   I’m getting the temperature every 2 seconds here: that’s so that I can test my code quicker instead of waiting for an hour.

6.   Now let’s check whether the window is open or closed. We’re using the block timer 1 reset from the Advanced menu and then Control.

7.   I’ve added the colors red for open and green for closed to let me know what the Circuit Playground Express thinks the window has done.

Images

Figure 20.9    Opening and closing the window.

8.   Finally, if our timer is over 15 minutes, get the last two temperatures from the array. I created variables LastTemp and SecondLastTemp to store these. Check out the box on page 239 of this mission to learn about arrays and addresses.

Images

Figure 20.10    Getting the last two temperatures.

9.   Check these temperatures against the current temperature.

10.   Signal the alarm.

Images

Figure 20.11    If it’s cold, sound the alarm!

Debug

We need to reset the timer, or else the alarm will go off constantly.

When testing the code, you might want to set the number 900 seconds (15 minutes) to something like 10 seconds.

Expert Level

Seriously, look at that code in Figure 20.12. There is nothing more I can teach you!

Images

Figure 20.12    Full bug-free code.

Raspberry Pi

Build

1.   Connect the Raspberry Pi exactly like the door setup in Mission 8.

2.   Make sure that the crocodile clips are long enough that you don’t pull the whole Raspberry Pi out the window when it’s opened!

3.   Code the Raspberry Pi before you connect it to the window.

Images

Figure 20.13    Raspberry Pi in the window.

Code

Looking closer at the Raspberry Pi code, we can see that this algorithm doesn’t apply here. The Raspberry Pi is not getting the inside temperature of the house. It gets the outside temperature according to a website. Our algorithm needs to be different. We could use this as a warning—it’s less than 10 degrees Celsius outside; close the window! Or we could look at the other data from the API. Is it raining? Close the window! Let’s do both.

1.   From the top:

a.   Import all your libraries.

b.   Add your variables for the temperature code from Mission 18.

c.   Set up your window just like you did for the door in Mission 8.

2.   Next, I’ve created some functions to check whether it’s raining or if it’s cold outside. I’ve also included the soundTheAlarm function from previous missions.

3.   Make sure that you’ve got the siren.wav file (or download your own file). Learn how to do that in Mission 8.

Images

Figure 20.14    Code.

Images

Figure 20.15    Code.

4.   Get your temperature data, and check whether it’s raining or cold outside by calling the preceding functions. Don’t forget time.sleep() here. We only want to get the temperature every 10 minutes, or the DarkSky API will be unhappy.

5.   If it’s raining or cold, is the window open? If it is, sound the alarm!

Images

Figure 20.16    Code.

FUNCTIONS

Finally, we have a full example of how functions work! In the functions isCold and isRaining, we send them a variable weather. The functions do their thing and return a value: True or False. In the main code, we just call the function in an if statement as if it were a variable: if isRaining(weather) or isCold(weather). After the functions return their value, this code will run like this (if it’s raining but not cold outside): if True or False. Functions make the code easier to read. In fact, if isRaining(weather) or isCold(weather) is practically a sentence in English!

6.   Add this code to the crontab so that it runs when the Raspberry Pi starts up. Check out Mission 8 on how to do this. Unplug the Raspberry Pi from your monitor, keyboard, and mouse, and set it up attached to the window.

Debug

My debugging on this mission was with the window—getting the tinfoil lined up and touching when the window closed was a bit tricky. A cool debug you could do is to find somewhere in the world where it’s raining! Put the coordinates for that place in your code, and see if your alarm goes off.

Expert Level

Nope. Nothing! There is nothing more I can challenge you with. You are an expert.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.141.26.230