Recitation 8: Serial Communication (November 12, 2019) by Jackson McQueeney

Part 1: Etch-A-Sketch

Arduino Schematic:

Arduino Code:

void setup() {
  Serial.begin(9600);
}

void loop() {
  int sensorValue1 = analogRead(A0);
  int sensorValue2 = analogRead(A1);
  int mappedvalue1 = map(sensorValue1, 0, 1023, 0, 500);
  int mappedvalue2 = map(sensorValue2, 0, 1023, 0, 500);
  Serial.print(mappedvalue1);
  Serial.print(",");
  Serial.print(mappedvalue2);
  Serial.println();
  delay(1);        
}

Processing Code:

import processing.serial.*;
String myString = null;
Serial myPort;
int x2;
int y2;
int NUM_OF_VALUES = 2;   
int[] sensorValues;      

void setup() {
  size(500, 500);
  background(0);
  setupSerial();
}

void draw() {
  updateSerial();
  printArray(sensorValues);
  stroke(255);
  line(x2, y2, sensorValues[0], sensorValues[1]);
  x2 = sensorValues[0];
  y2 = sensorValues[1];
}

void setupSerial() {
  printArray(Serial.list());
  myPort = new Serial(this, Serial.list()[5], 9600);
  myPort.clear();
  myString = myPort.readStringUntil( 10 ); 
  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]);
        }
      }
    }
  }
}

Result:

Part 2: Musical Instrument
Arduino Schematic:

Arduino Code:

#define NUM_OF_VALUES 2

int tempValue = 0;
int valueIndex = 0;

int values[NUM_OF_VALUES];

void setup() {
  Serial.begin(9600);
  pinMode(13, OUTPUT);
}

void loop() {
  getSerialData();


  if (values[2] == 1) {
    tone(9, values[0]);
  } else {
    noTone(9);
  }

}


void getSerialData() {
  if (Serial.available()) {
    char c = Serial.read();
    switch (c) {
      case '0'...'9':

        tempValue = tempValue * 10 + c - '0';
        break;
      case ',':
        values[valueIndex] = tempValue;
        tempValue = 0;
        valueIndex++;
        break;
      case 'n':
        values[valueIndex] = tempValue;

        tempValue = 0;
        valueIndex = 0;
        break;

      case 'e':
        for (int i = 0; i < NUM_OF_VALUES; i++) {
          Serial.print(values[i]);
          if (i < NUM_OF_VALUES - 1) {
            Serial.print (",");
          }
          else {
            Serial.println();
          }
        }
        break;
    }
  }
}

Processing Code:

import processing.serial.*;

int NUM_OF_VALUES = 2;

Serial myPort;
String myString;

int values[] = new int[NUM_OF_VALUES];

void setup() {
  size(500, 500);
  background(0);

  printArray(Serial.list());
  myPort = new Serial(this, Serial.list()[5], 9600);

  myPort.clear(); 
  myString = myPort.readStringUntil(10); 
  myString = null;
}

void draw() {
  noStroke();
  fill(0, 30);
  rect(0, 0, 500, 500);

  strokeWeight(5);
  stroke(255);
  line(pmouseX, pmouseY, mouseX, mouseY);

  values[0] = mouseX;
  values[1] = mouseY;

  sendSerialData();
  echoSerialData(200);
}

void sendSerialData() {
  String data = "";
  for (int i=0; i<values.length; i++) {
    data += values[i];
    if (i < values.length-1) {
      data += ",";
    } else {
      data += "n";
    }
  }

  myPort.write(data);
}

void echoSerialData(int frequency) {
  if (frameCount % frequency == 0) myPort.write('e');

  String incomingBytes = "";
  while (myPort.available() > 0) {

    incomingBytes += char(myPort.read());
  }

  print(incomingBytes);
}

Leave a Reply