3.4 Animal Counter
-
Have you ever had to keep track of lots of things at once? If you have, you've probably used a list to remember them all, like your teacher uses when reading the names of everyone in your class.
What if the things you had to track moved around on their own? Farmers have to keep track of their animals, and sometimes will tag them or mark them so they know which animal is which.
Scientists will also track wild animals to learn about their habits and routines. This has been especially necessary with our local Tasmanian Devils and the ongoing case of trying to solve their facial tumor disease. In cases such as this, it is important that scientists are able to identify a specific Tasmanian Devil, and track where it has been.
In this lesson, you will use multiple micro:bits to create an animal counting system. Instead of counting animals though, we'll be counting people!
We’re not going to need any new hardware for this, just a few micro:bits and a battery pack for each of them.
-
Sometimes, we want to keep track of multiple variables. For example, in a video game with four players, you might want to keep track of four number variables to store the score for each of the four players. When you have lots of variables to keep track of, you can use arrays to store them for you.
An array is a list of items that have the same basic type. So, you can have a number array (which is a list of numbers), or a string array (a list of strings), or an array of booleans (a list of values that are 1 (true) or 0 (false). If you have multiple lists that you need to update and keep track of, you can use multiple arrays to do that.
You can find items in an array as long as you know the position that data holds in the array. So if you are looking for player 3’s score, and you know that player 3’s score is at position 2 in the array named score, you can display that data by using the [variable] get value at block and setting it to position 2.
Here are some tips for using Arrays:
- You should consider using arrays in situations where you are keeping track of a lot of variables of the same types (for example, you want to store the values of 10 numbers, or 15 strings).
- You can find blocks relating to arrays in the Arrays menu in the Advanced section.
- There are code blocks for creating arrays, reading arrays and writing to arrays.
- When you create an array, MakeCode will set the type of array (string, number or boolean) based on the values that are first entered.
- Arrays can only contain one type of data. If your array is being used to store strings, you’ll need another array if you also want to store numbers or booleans.
- Arrays count from 0, so the first item in an array will always be item number 0, the second item will be item number 1, and so on.
- The length of array block does not count from 0, it counts from 1. If an array has 4 items in it, the
length of array block will return the number 4, even though if you were to count from 0 (0, 1, 2, 3), the number of the last item in the array would be 3. The easiest way to use this block is to remember that you need to subtract 1 from the length of array to get the number of the last item in the array.
- You should consider using arrays in situations where you are keeping track of a lot of variables of the same types (for example, you want to store the values of 10 numbers, or 15 strings).
-
The Challenge
- Create an array with four names in it.
- When you press the A button, move to the next item in your array and show the number for your current position on the micro:bit display.
- When you press the B button, show the name that is stored at your current position in the array on the micro:bit display.
- When you reach the end of the array, press the A button to return to the start again.
Required Devices
- 1 micro:bit
Suggested Blocks
- show number
- show string
- on button pressed
- if true then else
- [variable]
- set variable/array
- change [variable]
- get value at
Hints
- Remember that arrays start counting from 0, so the first item in an array is item 0, the second is item 1, the third is item 2 and so on.
Bonus Challenges
- Can you change your code so that you can add more items to an array (increase the length of the array) and not have to change other parts of your code for everything to keep working properly? To do this, you’ll probably need a
length of array block, as well as a math block. - Can you adapt your project to have multiple arrays instead of just one? Try changing your code so that you have a list of four names and a list of four favourite foods. Use buttons A+B together to change your position in the arrays and show the number for your current position on the display. When you press the A button, show the name at your current position, and when you press the B button show the favourite food at your current position.
- Create an array with four names in it.
-
Sometimes when we create an Internet of Things system, we need different devices with different capabilities to perform different jobs. Some devices will have relatively simple jobs to do, whereas other devices might be more complicated.
One such device that is often used with data tracking is called a base station. This device will need to do multiple jobs at the same time and asks other devices nearby to send information to it. A device that communicates with a base station and sends it the information is called a transmitter.
For this lesson, we will be programming a single micro:bit to function as a Base Station, and will then program an additional micro:bit to function as a Transmitter.The Base Station's job will be to listen for radio messages from 5, 10, or 20 other micro:bits and keep track of how many messages have been received from each of these individual micro:bits. It will also have the ability to manage connections and data from lots of other devices.
The Transmitters will be programmed to be able to interact with the Base Station and transmit information to it, but won't be able to interact directly with one another.
Lots of computer systems in the real world work this way. If you built a system to monitor street lights, that would probably have a base station that keeps track of the recent traffic intersection data and would have transmitters attached to the traffic lights that sent the data to the base station.
If you wanted to track animals on a farm, you’d use a base station that stays in a particular location, and transmitters that would be attached to the animals, who are free to wander around. You could then gather data about particular animals and their daily habits.
If you wanted to set up a security system in your home, it would most likely feature some kind of Base station that you can view in your home and see the data that has been gathered from the Transmitters spread around the outside of your house. Whether these devices are transmitting video/audio recordings or some other kind of data, the same usage case still applies. -
The radio in a micro:bit can be set up to use more or less power when it’s broadcasting messages. The more power that you use, the further a message can travel. But there are times when we don’t want our messages to travel a long distance.
We might only want to send our signal a short distance to communicate with devices that are nearby and not interfere with ones that are far away. Radio messages also use power to be sent. If our micro:bit is running on battery power, the batteries will last longer if we turn the power down.
Changing the broadcast power of the micro:bit is easy using the radio set transmit power block. Generally, you only need to set this once when your micro:bit starts up, but there might be scenarios where you want to temporarily increase the power for a longer range message and then turn the power back down to conserve battery life.
-
The Challenge
- For this challenge, you will need to work in a group with 2 micro:bits.
- Program one micro:bit to be a transmitter that sends your name as a radio message whenever you press the A button. Set the radio power to its lowest possible transmit power
- Program the other micro:bit to be a base station that shows the received name on the display whenever it receives a message.
Required Devices
- 2 micro:bits
Suggested Blocks
- show string
- radio set group
- radio set transmit power
- radio send string
- on radio received
- on button pressed
-
The strength of a radio signal can change based on a number of factors. One key factor is distance – the further away from the source you are, the weaker the signal will be. But another way that signal strength is reduced is by something getting in the way of a signal. This could be something like a person standing in the way, or a container that blocks some of the signal. Knowing how different things affect radio strength is useful, as sometimes we only want a signal to travel a short distance, so we can only detect the signal when we are close to it.
In this activity, we want to investigate how far micro:bits can send messages.
- Using your code from the previous challenge, test how far you can move away before the radio message does not get through.
- See how adjusting the radio signal strength affects the distance a message can be sent.
- Try putting your micro:bit in different types of containers, like a paper envelope or a cardboard box to see what effect it has on radio strength.
-
Sometimes we want to use Internet of Things devices to keep track of something over a period of time. We can use our devices to store data in a log over time in a process known as data logging. When we log data, the data is stored in an array along with a record of when the event happened.
For example, we might want to know how temperature changes overnight. Rather than having to manually write the temperature down at particular intervals, we can have the temperature measured continuously over a period of time and store the values in a data log. Then in the morning, we can retrieve these values and either look at them or import them into another piece of software (such as Microsoft Excel) in order to analyse the data.
This data is generally stored in such a way that it can be retrieved later, even staying safe if the power is turned off. In this case, we say that the data is persistent. This means that we can run a device on batteries in the field and keep the data stored until we plug it into a computer to download it.
-
The Data Logger extension allows our micro:bit to log data over time. This data is stored in a part of the micro:bit's memory that is persistent – that means the data will not be lost when the micro:bit loses power.
Let's download the ‘datalogger’ extension in the same way that we have downloaded other extensions in previous lessons 2.2 and 2.4.
The Data Logger extension allows us to access new code blocks that let us log data to the micro:bit. Once this extension is installed, we can create a data logger array using the set column block, and then log data into it, using the log data block.
Here are some tips on how to use the Data Logger extension blocks:
- You need to create a new log with the set columns block – this creates the object that will log your data.
- To save data to our logger, we use the log data block. This block works a bit like storing data in a variable – we are simply logging the current value to a column in the data logger array.
- Data logs are saved even when the micro:bit is powered off – it isn’t deleted automatically. You can set a way for your program to delete the log using the delete log block.
- You need to create a new log with the set columns block – this creates the object that will log your data.
-
The Challenge
- Extend your base station code from Challenge 2 to store the names it receives into a Data Logger array. You want the array to record the names of the micro:bits it receives signals from.
- The base station should display an icon when it receives a name, then clear the screen.
- Add the ability to clear the data log when you press the A+B buttons.
- Update your transmitter code to send your name every 10 seconds. Show an icon when you send the name, then clear the screen.
Required Devices
- 2 micro:bits
- 2 Battery Packs
Suggested Blocks
- show icon
- clear screen
- pause
- on radio received receivedString
- radio set group
- radio set transmit power
- radio send string
- on button pressed
- set column array of
- log data array of
- delete log
-
Now that we’ve built our animal trackers, let's gather some data with them!
Set up half of your class's micro:bits as base stations and put them around the classroom. Try not to put them too closely together. If you don’t have enough space in your classroom, use battery packs and set up the base stations outdoors.
Set up the rest of your micro:bits as transmitters. Walk slowly around the classroom with the transmitters. Go close to the base stations as you walk to see if they detect you – they should flash your icon when they detect a signal from the transmitter.
Once you've spent some time gathering data, we can plug in our base stations and look at our data.
- Plug in your base station to a computer via the USB cable.
- Using File Explorer (Windows) or Finder (Mac), open the micro:bit.
- Double click the log file (MY_DATA) to view your data.
Your log file will display a table that contains your data. One column will show the names that the base station recorded and the other column will record the times that the data was logged. You can download the data to a file and use Microsoft Excel to further analyse the data.
-
In this lesson we learned how to use arrays, how to build Base Stations and Transmitters, how to log data to the micro:bit and to build our own animal counter!
In the next lesson, we'll put together everything we've learned in this module to design our own smart farm.