🔌Arduino to Misty
In this project, you will use Arduino to trigger Misty to change the colour of her chest LED.
Materials
Misty’s Arduino backpack
2 buttons
2 LEDs
2 resistance 330 Ω
2 resistance 10 kΩ
Some wires of different lengths.
Arduino Circuit



Arduino Code
void setup() {
Serial.begin(9600);
pinMode(2, INPUT_PULLUP); //pin green button
pinMode(3, INPUT_PULLUP); //pin blue button
pinMode(10, OUTPUT); //pin green LED
pinMode(11, OUTPUT); //pin blue LED
}
void loop() {
int pushedg = digitalRead(2); //green button
int pushedb = digitalRead(3); // blue button
if (pushedg == LOW){ // green button pressed condition
digitalWrite(10, HIGH); // green LED on
Serial.println("green"); // write "green" in the serial
} else {
digitalWrite(10, LOW); // green LED off
}
if (pushedb == LOW){ // blue button pressed condition
digitalWrite(11, HIGH); // blue LED on
Serial.println("blue"); // write "blue" in the serial
} else {
digitalWrite(11, LOW); // blue LED off
}
delay(200); // wait for 200ms
}
The Arduino code is composed of two parts: the void setup and the void loop. The first one will run only once at the beginning and here you set all your variables. The second part will run in a loop until stopped.
In this void setup, you open the serial and set the pins you’ll use in this project. In the void loop, you associate the pins to our buttons and then we set two conditions:
If the green button is pressed you’ll turn on the green LED on the Arduino board and you will write in the serial the word “green” if this doesn’t happen the green LED will be off. It happens the same with the blue button as you can read in the code. At the end, we insert a pause in order not to send too many signals to Misty.
Misty Code
from mistyPy.Robot import Robot
from mistyPy.Events import Events
misty = Robot("your_robot_IP_address")
def msg(data):
print(data)
if data["message"]["message"] == "green":
misty.change_led(0, 255, 0)
if data["message"]["message"] == "blue":
misty.change_led(0, 0, 255)
misty.register_event(event_name="serial_message_event", event_type=Events.SerialMessage, callback_function=msg, keep_alive=True)
misty.keep_alive()
To allow Misty to read in the Serial you need to build an event, so you can import the usual libraries that Misty uses to register events. Then we create the robot and the function that will be called with the event.
In this function we read the serial message and if the condition is verified Misty will complete the function. If we press the green button, Arduino will write “green” in the serial and Misty will register “green” as message from the serial port, so she’ll act as shown in the function.
Last updated