Misty Lessons
  • Misty Lessons
    • 📖Welcome to Misty Lessons
    • 📚Get to know your Misty
    • 📲Connect to Misty
    • 👩‍💻Misty Studio
    • 🖥️Desktop Environment
    • ⬆️Update your Misty
    • 👥Projects
  • Blockly
    • 🧩Blockly Lessons
      • 🤸Lesson 1: Movement
      • 🎶Lesson 2: Voice and Sound
      • 🎭Lesson 3: Expressions
      • 🎥Lesson 4: Robot Cinema
      • 🛠️Lesson 5: Events
      • ☺️Lesson 6: Face recognition
      • 🔢Lesson 7: Variables and Functions
      • 💬Lesson 8: NLP
    • 🏫Blockly projects
  • Blockly Elements
    • ⚛️Misty Blocks
      • Movement
      • Speech
      • Audio
      • Vision
      • Events
      • Miscellaneous
      • NLP
      • System
    • 🔁Basic Blocks
      • Logic
      • Loops
      • Math
      • Text
      • Lists
    • 🅰️Advanced Blocks
      • Variables
      • Functions
  • Python
    • 🐍Python Lessons
      • 🦿Lesson 1: Loco-motion
      • 🤖Lesson 2 : Build a character
      • 🧠Lesson 3: Create memories
      • ⚒️Lesson 4: Event skills
      • 👀Lesson 5: Expand awareness
      • 🔗Lesson 6: Compact code
      • 🗣️Lesson 7: Start a conversation
  • Python Elements
    • 🐸Misty Python API
      • Motion and Mobility
      • Display and LED
      • Record Assets
      • Change/Remove Assets
      • Stream Assets
      • Get Assets
      • Events
      • Sensor Events
      • Speech and NLP
      • Arduino Backpack
      • System
      • Slam
    • 📗Python Definitions
  • Python Projects
    • 🔮MistyGPT
    • 🚨Misty Intruder Alert
    • 📺Conference Assistant
    • 🏷️QR code detector
    • 🕵️‍♂️Misty follow human
    • 👋Misty wave back
    • 🖲️Misty OA
    • 🌐Get weather
    • 🚚Misty Delivery
    • 🫂Motivational Misty
    • 🖼️Misty Museum Guide
    • 🎃Who for Halloween
  • ARDUINO
    • ♾️Arduino Backpack
    • 🦎Arduino
    • 🔧Arduino Lessons
      • 🔌Arduino to Misty
      • ➕Misty to Arduino
  • ARDUINO PROJECTS
    • 🛠️Misty Tracker
    • 🦾Misty Arm
  • Community Projects
    • 🌤️Misty weather forecaster
  • HARDWARE EXTENSION
    • ⚙️Arduino breadboard support
    • 🦾Misty arm
    • 🥤Tin holder
  • Resource Database
    • 📁Image files
    • 📁Audio files
    • 📁Languages
    • 📁Known objects
    • 📁NLP Actions
    • 📁Action Commands
    • 📁ChatGPT PDF files
    • 📁AR Tag Dictionary
    • ⚙️Technical Specifications
Powered by GitBook
On this page
  • Materials
  • Arduino Circuit
  • Arduino Code
  • Misty Code
  1. ARDUINO
  2. Arduino Lessons

Misty to Arduino

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

Last updated 9 months ago

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.

🔧
➕
You can use the same scheme with Arduino UNO or the arduino Backpack