Misty to Arduino

In this project, you’ll use MIsty’s bump sensor to trigger Arduino to change its LEDs.

Materials

  • Misty’s Arduino backpack

  • 2 LEDs

  • 2 resistance 330 Ω

  • Some wires of different lengths.

Arduino Circuit

Arduino Code

void setup(){
  Serial.begin(9600);
  Serial.setTimeout(100);
  pinMode(10, OUTPUT);      	//pin red LED
  pinMode(11, OUTPUT);      	//pin blue LED
}

void loop() {
  if (Serial.available() > 0){	        // check if something is written in the Serial
    String str = Serial.readStringUntil('\n');  // read the string
    if (str == "red"){			        // compare the string
      digitalWrite(10, HIGH);		        // red LED on
      Serial.println("red on");	                // write in the serial monitor
    }
    if (str == "blue"){
      digitalWrite(11, HIGH);
      Serial.println("blue on");
    }
    if (str == "red off"){
      digitalWrite(10, LOW);
      Serial.println("red off");
    }
    if (str == "blue off"){
      digitalWrite(11, LOW);
      Serial.println("blue off");
    }
    if (str == "all off"){
      digitalWrite(10, LOW);
      digitalWrite(11, LOW);
      Serial.println("all off");
    }
    if (str == "all on"){
      digitalWrite(10, HIGH);
      digitalWrite(11, HIGH);
      Serial.println("all on");
    }
  }
delay(500);
}

In this code, you can see the same structure as the previous one: A void setup and a void loop. In the void setup, you open the serial, set the maximum milliseconds to wait for serial data (the default is 1000 ms) and define the pins that you’ll use in the code.

In the void loop, the first thing you should do is to check if there is something written in the serial. The second step is to read the string that exists in the serial by defining it. To read strings completely you can use the character “\n” at the end, which represents the new line character/enter. Once you get the string the last part is to define all the conditions you can have.

For example :

if (str == "red"){
      digitalWrite(10, HIGH);	
      Serial.println("red on");
    }

If the string you get from Misty is “red” turn on the red LED and write in the Arduino’s serial “red on”. The Serial.println at the end of each if condition is not necessary for the code but it’s helpful for the programmer who can see what’s happening in the code from the serial monitor in the Arduino IDE.

Misty Code

from mistyPy.Robot import Robot
from mistyPy.Events import Events

misty = Robot("your_robot_IP_address")

def bumped(data):
    print(data)
    if(data["message"]["sensorId"] == "bfr" and data["message"]["isContacted"] == True):
        misty.change_led(255, 0, 0)
        misty.write_serial("red\n")
    if(data["message"]["sensorId"] == "bfl" and data["message"]["isContacted"] == True):
        misty.change_led(0, 0, 255)
        misty.write_serial("blue\n")
    if(data["message"]["sensorId"] == "brr" and data["message"]["isContacted"] == True):
        misty.change_led(0, 255, 0)
        misty.write_serial("red off\n")
    if(data["message"]["sensorId"] == "brl" and data["message"]["isContacted"] == True):
        misty.change_led(255, 127, 0)
        misty.write_serial("blue off\n")

misty.register_event(event_name='bump_event', event_type=Events.BumpSensor, callback_function=bumped, keep_alive=True)
misty.keep_alive()

To write in the serial you don’t need an event, but Misty has it as an element in her Python API. This code is a normal Bump sensor event that associates to each bump sensor a string to write in the serial.

For example:

if(data["message"]["sensorId"] == "bfr" and data["message"]["isContacted"] == True):
        misty.change_led(255, 0, 0)
        misty.write_serial("red\n")

If the bump front right sensor is contacted turn Misty’s chest LED red and write “red\n” in the serial. Arduino will receive the string from the serial, read it until the “\n” character and select the function from its list.

Last updated