Have you ever chatted with iphone’s Siri or interact with a smart robot? If not, that’s ok, cause now we can directly program an intelligent chatbot by ourselves with mind+ and micro:bit. Isn’t that sounds cool! Well, let’s begin.
The AI translations will be involved in this chapter, also we will get to know about the temperature sensor on the micro:bit board.
We have already get known about variable, you may find that there is a “create a list” block in the variable module. What is a list ? we know that a variable can be used to store number, char etc and can be refereed repeatedly. Of course, we can also update a variable. In fact, a list is a collection variable, just like a queue. For example, we create a list and name it as “present”, then place four items in the present list: crystal ball, music box, lego, model car. We can add items into the list, here we add an item to the end of the list, and then the list would be like this “crystal ball, music box, lego, model car, doll house”. Besides, the items in the list can be revised, for instance, change the first item into dinosaur model, then the list would be “dinosaur model, music box, lego, model car, doll house”. Apparently, we can delete items in the list. Well now, let’s create a list and see what functions will be found in blocks. As figure 6-1 shows, we can add or delete a specified item to a list; replace an item, hide or show a list.
In this case, we are gonna create three sprites: Santa Claus, a boy and a girl, as figure 6-2 shows. The boy and girl asked Santa Claus respectively “what can I get for my Christmas present?” Santa Claus thinks for a while and then determines to pick a gift randomly from his pocket. But here comes a problem: there are crystal ball and Transformer in his pocket, so a situation like this could happen: the crystal ball was sent to a boy and the Transformer went to a girl, then it would be awkward! To deal with that problem, we need to classify our gifts into For Girl and For Boy. Cause it could be much reasonable to pick a present from For Girl
list when a girl asked for her gift.
In this case we learned how to create a list, add items for the list, hide and acquire a certain item form the list and so on. We will use a new block
here . It can be found in “Operators” module. The block concatenates or “links” the two values together and reports the result.
We will use list to detect chat language in this case. The functions the programs need to realize: obtain a speech the user input, divide it into single character and put them into a list, then check every item in the list.
First of all, we need to learn how to get the number of characters.
Secondly, divide a speech into single characters and place the characters into a list.
Input a speech “I am Micko”, the number of characters is 3, and it needs three times to get all the characters. Set a variable “i” to represent the number of cycles. Get the first character in the first loop and so on. The value of i will increase by 1 in every loop. When it comes to the last loop, the value of i will be 4 after getting the third character. Then stops the loop and display the content of the list.
Input “I am Micko” and check the result. So far, we can detect some particular characters of the chatting language.
With the knowledge of list we can make much more fun programs, detect language, chatting room and so on. Ok, now let’s start to make the chatbot.
- Create a list “Chatbot” and show it;
- Repeatedly run the program “ask … and wait”, and “answer”. Add “answer” to the end of the list to make it like an effect of asking question.
- Embed the “if then…else” repeatedly and detect if the words like “time”, “joke”, “song”, “translation”, and “temperature” includes in the “answer”.
We can ask the chatbot to tell us time, temperature, or let him tell jokes, play music and do translations. The scripts are shown in figure 6-7.
You can play the sound in the sound library, record your own sound or upload a sound file from your computer. We use the sound “dance around” in the loops of the sound library.
Load the google translate: click “Extensions”-> “Function”-> “Google Translate”. Then we can use the related google translate module.
Connect micro:bit and the computer with micro USB. The temperature sensor on the micro:bit board will detect the current temperature of the device. (unit: °/℃)
Click the green flag to start the program, and check the effect. We can drag the frame of the list to expand the display area.
Now the chatbot can only realize the function of chatting, but we can combine it with the micro:bit to simulate the smart home, for example, to give orders like “turn on/off the light” to control the LED light on the micro:bit board.