๐Ÿ”Œ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