arduivis

a bi-directional communication paradigm for programming languages & microcontrollers

Getting Started FAQ Projects
    MaxMSP Pure Data Python NodeJS

    Abstract

    This is an example of how to send strings and numbers to MaxMSP using Python in real-time using the terminal.

    Video

    Instructions

    * The process below is documented in the video below *

    1. Find and upload the sketch "SerialEvent.ino" in the Arduino IDE (File -> Examples -> 04.Communication -> SerialEvent.ino)
    2. Once the upload process is complete, open the patch named "arduivis_python_model_rtWriteMax.maxpat" (arduivis -> Python -> model_rtWriteMax)
    3. Follow the instructions inside the patch and start the serial buffer
    4. Next, find and open the computer terminal and run the Python sketch named "arduivis_python_model_rtWriteMax.py" (arduivis -> Python -> model_rtWriteMax)
    5. Once the sketch is running, a string can be sent from the terminal using Python and is received by Max via the [serial] object. The Arduino is used as a communication layer where these two languages can converse in real-time.

    Communication Diagram

    SEND: Write real-time messages to the serial buffer from Python via the terminal.

    THROUGH: An Arduino is used to create the serial buffer. This is where the Python messages are formatted for the receiver language/

    RECEIVE: MaxMSP/PD receives the real-time messages that can are accesible to the user.

    Python Code

    
    # Import pySerial
    import serial
    
    # Port Name
    # To find out the correct port type the line below into the terminal
    # python -m serial.tools.list_ports
    board = '/dev/cu.usbmodem1411'
    
    # Baud Rate
    # Match baud rate with the rate used in the Arduino sketch and the Max patch
    baud = 9600
    
    # Define the connected port
    arduino = serial.Serial(board, baud, timeout=1)
    
    # Keep the data flowing with an infinite while loop
    infinite = 1
    
    while (infinite == 1):
    
      # Read each line of the serial output 
      output = arduino.readline()
    
      toArduino = raw_input('Write to Serial buffer: ')
      print('Write to Serial buffer', toArduino)
      arduino.write(toArduino+ '\n')
      # Print each line of the serial output
      # print output
    

    Arduino Code

    * The Arduino code can be found in the following directory. (File -> Examples -> 04.Communication -> SerialEvent)

    
     /*
      Serial Event example
     
     When new serial data arrives, this sketch adds it to a String.
     When a newline is received, the loop prints the string and 
     clears it.
     
     A good test for this is to try it with a GPS receiver 
     that sends out NMEA 0183 sentences. 
     
     Created 9 May 2011
     by Tom Igoe
     
     This example code is in the public domain.
     
     http://www.arduino.cc/en/Tutorial/SerialEvent
     
     */
    
    String inputString = "";         // a string to hold incoming data
    boolean stringComplete = false;  // whether the string is complete
    
    void setup() {
      // initialize serial:
      Serial.begin(9600);
      // reserve 200 bytes for the inputString:
      inputString.reserve(200);
    }
    
    void loop() {
      // print the string when a newline arrives:
      if (stringComplete) {
        Serial.println(inputString); 
        // clear the string:
        inputString = "";
        stringComplete = false;
      }
    }
    
    /*
      SerialEvent occurs whenever a new data comes in the
     hardware serial RX.  This routine is run between each
     time loop() runs, so using delay inside loop can delay
     response.  Multiple bytes of data may be available.
     */
    void serialEvent() {
      while (Serial.available()) {
        // get the new byte:
        char inChar = (char)Serial.read(); 
        // add it to the inputString:
        inputString += inChar;
        // if the incoming character is a newline, set a flag
        // so the main loop can do something about it:
        if (inChar == '\n') {
          stringComplete = true;
        } 
      }
    }
    
    
    

    Max Patch

    some_text