Serial Communications Workshop:
For this weeks recitation, I attended the serial communications workshop because I felt like I still sort of struggled with communication between Arduino and Processing.
During this workshop, we went over the Serial Communications folder and discussed multiple and one values between Arduino and Processing. We connected a potentiometer to Arduino for the oneValue exercise and added a button for the multipleValues exercise.
Lastly, we connected a servo motor for the last exercise. We had some obstacles during this exercise but overall, this workshop helped me a lot with telling Arduino and Processing what to do.
For using the map value, it was fairly simple with the potentiometer because there was a maximum and minimum value for the potentiometer and a maximum/minimum value for the mouse pressed function.
Arduino Code:
void setup() {
Serial.begin(9600);
pinMode(9, INPUT);
}
void loop() {
int sensor1 = analogRead(A0);
int sensor2 = digitalRead(9);
// int sensor3 = analogRead(A2);
// keep this format
Serial.print(sensor1);
Serial.print(“,”); // put comma between sensor values
Serial.print(sensor2);
//Serial.print(“,”);
// Serial.print(sensor3);
Serial.println(); // add linefeed after sending the last sensor value
// too fast communication might cause some latency in Processing
// this delay resolves the issue.
delay(100);
}
Processing Code:
import processing.serial.*;
String myString = null;
Serial myPort;
int NUM_OF_VALUES = 2; /** YOU MUST CHANGE THIS ACCORDING TO YOUR PROJECT **/
int[] sensorValues; /** this array stores values from Arduino **/
int previousSensorValues1;
int previousSensorValues2;
float posX;
void setup() {
size(600, 600);
background(255);
setupSerial();
}
void draw() {
//background(255);
updateSerial();
printArray(sensorValues);
float posX = map( sensorValues[0], 0, 1023, 0, 500);
ellipse(sensorValues[0],mouseY,50,50);
noFill();
line(previousSensorValues1,previousSensorValues2,sensorValues[0],sensorValues[1]);
previousSensorValues1 = sensorValues[0];
previousSensorValues2 = sensorValues[1];
// use the values like this!
// sensorValues[0]
// add your code
//
}
void setupSerial() {
printArray(Serial.list());
myPort = new Serial(this, Serial.list()[2], 9600);
// WARNING!
// You will definitely get an error here.
// Change the PORT_INDEX to 0 and try running it again.
// And then, check the list of the ports,
// find the port “/dev/cu.usbmodem—-” or “/dev/tty.usbmodem—-”
// and replace PORT_INDEX above with the index number of the port.
myPort.clear();
// Throw out the first reading,
// in case we started reading in the middle of a string from the sender.
myString = myPort.readStringUntil( 10 ); // 10 = ‘\n’ Linefeed in ASCII
myString = null;
sensorValues = new int[NUM_OF_VALUES];
}
void updateSerial() {
while (myPort.available() > 0) {
myString = myPort.readStringUntil( 10 ); // 10 = ‘\n’ Linefeed in ASCII
if (myString != null) {
String[] serialInArray = split(trim(myString), “,”);
if (serialInArray.length == NUM_OF_VALUES) {
for (int i=0; i<serialInArray.length; i++) {
sensorValues[i] = int(serialInArray[i]);
}
}
}
}
}
Young said that there wasn’t really an exercise for our workshop and to just write a few paragraphs on what we did during the workshop!