Code from class, circuit schematics, etc. are at the bottom
Let’s get the chocolate in the peanut butter
- Serial communication – you’ve already done this! Serial monitor in Arduino
- Give us even more power to compare different things in unique and weird ways
- sending voltage along the USB cable to represent numbers, letters, and punctuation
- one number/letter is easy
- more will require you to make up your own protocol (agreement between systems)
- Physical system is already determined for us, as is the mechanism for communication.
- We’re gonna make up our own words and hooks that uniquely identify our information (formatting above all else)
- baud rate must be in agreement
- println() on Arduino side – just like the serial monitor. Gonna replace the duck with a processing program
- Processing doesn’t have a native idea of serial (Arduino can only communicate with one other thing, Processing can communicate with many at the same time).
- Make sure Arduino is sending information
- Close serial monitor (Ardy can only talk to one at a time)
- import serial lib
1import processing.serial.*; - make serial object & initialize
- need to choose the correct port
12345println(Serial.list());// Print out the list and look for port your Microcontroller is on// Finding the Arduino is not easy because they have weird names like "/dev/tty.usbmodem1421" or COM1String portName = Serial.list()[0];myPort = new Serial(this, portName, 9600);
- need to choose the correct port
- set up listener/callback/what have you
1myPort.readStringUntil('\n');
This gets paired with a freebie function serialEvent. It waits until a newline character (the ‘\n’) is received.
12345678910void serialEvent(Serial _port) { //this is a callback functionif (myPort == null) return; //this is a hack to cover a bug where the port does not get set up in time.//this says if the port is not set up yet, bail (for now.)String input = myPort.readStringUntil('\n');if (input != null) { //if a '\n' character has in fact now arrivedinput = input.trim(); //Take off the '\n' charactersize = int(input); //Turn it into numberprintln(xpos);}}- This converts the text from Arduino into numbers that Processing can use.
- Arduino can send raw bytes (an 8-bit value between 0-255), and that’s great if you’re an engineer. For the rest of us, it’s better to think about text. It’s easier to read & debug, you can send arbitrarily long numbers, and it’s easier to send multiple pieces of data because it allows for non-numeric characters that can act as delimiters.
- How ASCII works
- ASCII Table is the nerdiest website I know by heart.
- sending multiple bits from Arduino to Processing (CallResponseASCII)
- Sending from Processing to Arduino (Physical Pixel)
- This is the exception to the rule, it’s easier to send bytes to a micro controller. (physical pixel/dimmer)
- Picture to Pixel
- Examples from class
– Space Invaders (Handshake, 2 sensors to Processing)
– Pixel to RGB LED (multiple bytes to Arduino)
– Dimmer (one byte to Arduino)
– Expanding Circle (one sensor to Processing)