Task #1: Make a Processing Etch-A-Sketch
import processing.serial.*; import osteele.processing.SerialRecord.*; Serial serialPort; SerialRecord serialRecord; void setup() { size(500, 500); String serialPortName = SerialUtils.findArduinoPort(); serialPort = new Serial(this, serialPortName, 9600); // If the Arduino sketch sends a different number of values, modify the number // `2` on the next line to match the number of values that it sends. serialRecord = new SerialRecord(this, serialPort, 2); } void draw() { background(0); serialRecord.read(); int value1 = serialRecord.values[0]; int value2 = serialRecord.values[1]; float x = map(value1, 0, 1024, 0, width); float y = map(value2, 0, 1024, 0, height); circle(x, y, 20); // line(x,y,px,py); }
import processing.serial.*; import osteele.processing.SerialRecord.*; Serial serialPort; SerialRecord serialRecord; float px ; float py ; void setup() { size(500, 500); background(0); String serialPortName = SerialUtils.findArduinoPort(); serialPort = new Serial(this, serialPortName, 9600); // If the Arduino sketch sends a different number of values, modify the number // `2` on the next line to match the number of values that it sends. serialRecord = new SerialRecord(this, serialPort, 2); } void draw() { serialRecord.read(); int value1 = serialRecord.values[0]; int value2 = serialRecord.values[1]; float x = map(value1, 0, 1024, 0, width); float y = map(value2, 0, 1024, 0, height); //circle(x, y, 20); stroke(255); line(x,y,px,py); px = x; py = y;
The main difference of this two sketches is I have to keep the track of px and py to draw the line.
Task #2:
import processing.serial.*; import osteele.processing.SerialRecord.*; Serial serialPort; SerialRecord serialRecord; //int direction = 1; float x = 0 ; float speed = 10; int Direction = 1; int d = 100; void setup() { fullScreen(); String serialPortName = SerialUtils.findArduinoPort(); serialPort = new Serial(this, serialPortName, 9600); // In order to send a different number of values, modify the number `2` on the // next line to the number values to send. In this case, the corresponding // number in the Arduino sketch should be modified as well. serialRecord = new SerialRecord(this, serialPort, 2); } void draw() { background(0); if(x < d/2){ x = d/2; Direction *= -1; } else if ( x > width - d/2){ x = width -d/2; Direction *= -1; } x += speed* Direction; fill (255); circle(x, height/2, d); println(width); // store some values in serialTransport.values, and send them to the Arduino serialRecord.values[0] = int(x); serialRecord.send(); //} }
#include "SerialRecord.h" SerialRecord reader(1); #include <Servo.h> Servo myservo1; Servo myservo2; int val; int pos = 0; void setup() { Serial.begin(9600); // pinMode(LED_BUILTIN, OUTPUT); // pinMode(9, OUTPUT); myservo1.attach(8); myservo2.attach(9); } void loop() { reader.read(); if(reader[0] == 50){ myservo1.write(0); delay(50); myservo1.write(90); // tell servo to go to position in variable 'pos' }elseif(reader[0] == 1390){ myservo2.write(0); delay(50); myservo2.write(90); }
import processing.serial.*; import osteele.processing.SerialRecord.*; Serial serialPort; SerialRecord serialRecord; //int direction = 1; float x = 0 ; float speed = 10; int Direction = 1; int d = 100; void setup() { fullScreen(); //size(1440, 100); String serialPortName = SerialUtils.findArduinoPort(); serialPort = new Serial(this, serialPortName, 9600); serialRecord = new SerialRecord(this, serialPort, 1); } void draw() { background(0); if(x <= d/2){ x = d/2; Direction *= -1; } else if ( x >= width - d/2){ x = width -d/2; Direction *= -1; } x += speed* Direction; fill (255); circle(x, height/2, d); println(int(x)); // store some values in serialTransport.values, and send them to the Arduino serialRecord.values[0] = int(x); serialRecord.send(); //} }
I worked together with Claire Yang in task 2. To complete the animation of the bouncing ball, we used “Direction” in a for-loop to let the ball move back and forth. In the last step, we struggled for quite a long time. At first we were still using the SendMultipleValues and ReceiveMultipleValues coding example, but we actually only needed to send single value in this part. However, the motor was not working as expected even though we changed that and tested every part of the circuit and code. Gottfried pointed out it may be because we used a for -loop to control the servo motor. After revising that part, everything worked well. One question still remaining unsolved is, there seems to be a short delay of the servo motor when the ball gets to the left side.