Recitation 8, Rudy Song

Exercise 1,

In this week’s recitation, I have made an Etch A Sketch with Processing and Arduino, sending analog values of two potentiometers from Arduino to Processing. I met some difficulties when there is some delay in the value sending, and led to several crashes of my system.

The final look is like this,

The code for Arduino is as following,

int valueX;
int valueY;

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

void loop(){
valueX = analogRead(A0);
valueY = analogRead(A1);
Serial.print(valueX);
Serial.print(“,”);
Serial.print(valueY);
Serial.println();
delay(50);
}

For Processing: 

import processing.serial.*;

String myString = null;
Serial myPort;
int NUM_OF_VALUES = 2;
int[] sensorValues;
float x, y, lx, ly;

void setup() {
size(500, 500);
background(0);
setupSerial();
delay(1000);
updateSerial();
printArray(sensorValues);
lx=map(sensorValues[0], 0, 1023, width, 0);
ly=map(sensorValues[1], 0, 1023, height, 0);
}

void draw() {
updateSerial();
x=map(sensorValues[0], 0, 1023, width, 0);
y=map(sensorValues[1], 0, 1023, height, 0);
if (keyPressed && key==127) {
background(0);
}
stroke(color(255, 255, 255));
strokeWeight(2.5);
line(x, y, lx, ly);
lx=x;
ly=y;
}

void setupSerial() {
printArray(Serial.list());
myPort = new Serial(this, Serial.list()[1], 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);
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]);
    }
}

Exercise 2

I have written a Processing sketch that sends values to your Arduino based on your mouse’s x and y positions and/or keyboard interactions. However, I was not able to make it work eventually, mainly due to the crashes on my laptop. However, I believe I was really close to getting

it. 

Arduino code is like following:

#define NUM_OF_VALUES 3

int tempValue = 0;
int valueIndex = 0;

int values[1];

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

void loop() {
getSerialData();
analogWrite(11,values[0]);
if (values[0]==1){
tone(6,map(values[1],0,1023,200,4000),map(values[2],0,1023,20,200));
}
}

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;
}
}
}

For Processing:

import processing.serial.*;

int NUM_OF_VALUES = 3;
int values[] = new int[NUM_OF_VALUES];

Serial myPort;
String myString;

void setup() {
size(500, 500);
background(0);
myPort = new Serial(this, Serial.list()[ 1 ], 9600);
myPort.clear();
myString = myPort.readStringUntil(10);
myString = null;
colorMode(HSB,255);
background(0);
strokeWeight(2);
for (int x=0;x<width;x++){
for (int y=0;y<height;y++){
stroke(map(x,0,width,0,255),255,map(y,0,height,30,255));
point(x,y);
}
}
}

void draw() {
if (mousePressed) {
values[0]=1;
} else {
values[0]=0;
}
values[1]=int(map(mouseX, 0, width, 0, 1023));
values[2]=int(map(mouseY, 0, height, 0, 1023));
sendSerialData();
echoSerialData(200);
}

void sendSerialData() {
String data = “”;
for (int i=0; i<values.length; i++) {
data += values[i];
if (i < values.length-1) {
data += “,”; // add splitter character “,” between each values element
}
else {
data += “n”; // add the end of data character “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