Recitation 9: Serial Communication

Exercise 1: 

We did similar things in the previous class, so I went smoothly with the circuit and codes about the first part of this exercise. Remarkably, I thought the points are so separated so I tried multiple times changing some variables. At first I was changing frameRate() in processing, but later I found to change delay() in Arduino is more powerful. 

When modify the code into drawing with lines, it takes me some effort to figure out how to substitute px&py with x&y. 

Code in processing: 
...
void draw() {
  getSerialData();
  printArray(sensorValues);
  fill(255,0,0);
  stroke(255);
  px=x;
  py=y;
  x=map(sensorValues[0],0,1023,10,490);
  y=map(sensorValues[1],0,1023,10,490);
  line(px,py,x,y);
}
...
Schematic:

Exercise 2: 

Schematic: 

In this exercise I did the part of processing smoothly but had some problems with the code in Arduino to control servos. After class I spent more time on it and it worked successfully. However since there’s only one servo then, I modified the code so that the servo spins every time the face bounced against the edge. 

Homework

Unfortunately I don’t have time for the two workshop this week. But I still completed the homework by my own effort and some help from Sarah and Daniel!

Code in Arduino: 
int buttonState0 = 0;
int buttonState1 = 0;
int lastButtonState0 = 0;
int lastButtonState1 = 0;
int val0;
int val1;

void setup() {
  Serial.begin(9600);
  pinMode(2, INPUT);
  pinMode(3, INPUT);
}

void loop() {
  lastButtonState0 = digitalRead(2);
  lastButtonState1 = digitalRead(3);
  if ( lastButtonState0 != buttonState0 && lastButtonState0 == HIGH )  {
    val0 = 1 - val0;
  }
  buttonState0  = lastButtonState0;
  if ( lastButtonState1 != buttonState1 && lastButtonState1 == HIGH )  {
    val1 = 1 - val1;
  }
  buttonState1  = lastButtonState1;
  Serial.print(val0);
  Serial.print(",");
  Serial.print(val1);
  Serial.println();
  delay(100);
}

Here Daniel taught me a little trick, which is using “val0 = 1 – val0″ to change the value. In this way 0 can turn into 1 and 1 can turn into 0 automatically in one line, without extra “if” statements.

Code in Processing: 
import processing.serial.*;
int NUM_OF_VALUES = 2;   /** YOU MUST CHANGE THIS ACCORDING TO YOUR PROJECT **/
int sensorValues[];      /** this array stores values from Arduino **/
String myString = null;
Serial myPort;

void setup() {
  size(600, 600);
  background(0);
  setupSerial();
}
void draw() {
  background(0);
  getSerialData();
  printArray(sensorValues);
  if (sensorValues[0] == 1) {
    star(width*0.3, height*0.3, 30, 70, 5);
  }
  if (sensorValues[1] == 1) {
    star(width*0.7, height*0.7, 80, 100, 40);
  }
}

void star(float x, float y, float radius1, float radius2, int npoints) {
  float angle = TWO_PI / npoints;
  float halfAngle = angle/2.0;
  beginShape();
  for (float a = 0; a < TWO_PI; a += angle) {
    float sx = x + cos(a) * radius2;
    float sy = y + sin(a) * radius2;
    vertex(sx, sy);
    sx = x + cos(a+halfAngle) * radius1;
    sy = y + sin(a+halfAngle) * radius1;
    vertex(sx, sy);
  }
  endShape(CLOSE);
}
void setupSerial() {
  printArray(Serial.list());
  myPort = new Serial(this, Serial.list()[2], 9600);
  myPort.clear();
  myString = myPort.readStringUntil( 10 );  // 10 = '\n'  Linefeed in ASCII
  myString = null;
  sensorValues = new int[NUM_OF_VALUES];
}

void getSerialData() {
  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]);
        }
      }
    }
  }
}

The Processing part went more smoothly. But at first I forgot to include background in draw() so the stars couldn’t disappear. I realized the problem immediately.

Video:

 

Leave a Reply

Your email address will not be published. Required fields are marked *