Task #1: Make a Processing Etch-A-Sketch
This part everything went well. One thing worth mentioning is that LA asked me why I chose to use the slider-shape protentiometer instead of rotary knob one. Actually, we have enough rotary protentiometers; the reason I chose to use slider is that, the basic motions of the little white ball on screen is up, down, right, and left–it’s more direct to see the control direction of right and left.
What’s more, it’s interesting to comment out the background so that the bouncing ball will become the drawing sketch:D
Code from the Arduino:
/* ReceiveMultipleValues This sketch repeatedly receives a record that contains a single value, and uses it to control the builtin LED. The value should be 0 or 1. This sketch pairs well with the SendSingleValue example from the Processing SerialRecord library <https://osteele.github.io/Processing_SerialRecord/>. You can also interact with this sketch from the Serial Monitor. Enter `100,200` into the text area at the top, and press "Send". Then enter `!e` to ask the Arduino to send back the last values it received. by Oliver Steele, 2020-2022 This example code is in the public domain. */ #include "SerialRecord.h" // Change this number to the number of values you want to receive SerialRecord reader(2); void setup() { Serial.begin(9600); pinMode(A0,INPUT); pinMode(A1,INPUT); // pinMode(LED_BUILTIN, OUTPUT); // pinMode(9, OUTPUT); } void loop() { reader.read(); if (reader[0] > 512) { digitalWrite(LED_BUILTIN, HIGH); } else { digitalWrite(LED_BUILTIN, LOW); } tone(9, reader[1]); }
Code from processing:
/** * Example sketch for the SerialRecord library for Processing. * * Receives two integers from the serial port, and uses them to control the x * and y position of a circle on the canvas. */ import processing.serial.*; import osteele.processing.SerialRecord.*; Serial serialPort; SerialRecord serialRecord; void setup() { size(500, 500); String serialPortName = SerialUtils.findArduinoPort(2); 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); }
Task #2:
- Partner: Morgan
- During the coding part we first use the spacebar and keyPressed() function to test the arduino IDE and servo. However, there was an issue that the servo kept shaking after swaping once; and before it, the servo could not stop swapping.
Finally, with the help of Prof. Minsky and one of my friend, I tested the code with different servos. Then, the problem is–the servo! Perhaps, the original servo I used was somehow broken.
And for the latter problem, Rudi helped us to fix it, by making the reader.read()
into if (reader.read()) {}
Although it’s hard to know what exactly happened inside the reader.read() and arduino, it’s interesting to learn reader.read() can be used as a boolean parameter.
Code from Arduino:
#include "SerialRecord.h" #include Servo myservo; Servo myservo2; int step; int pos; // Change this number to the number of values you want to receive SerialRecord reader(2); void setup() { // Servos myservo.attach(9); myservo2.attach(8); myservo.write(0); myservo2.write(0); Serial.begin(9600); } void loop() { if (reader.read()) { //Rudi helped and suggested if (reader[0] == 1) { for (pos = 0; pos < 120; pos += 3) { myservo.write(pos); delay(3); } for (pos = 120; pos > 0; pos -= 3) { myservo.write(pos); delay(10); } } if (reader[1] == 1) { for (pos = 0; pos < 120; pos += 3) { myservo2.write(pos); delay(3); } for (pos = 120; pos > 0; pos -= 3) { myservo2.write(pos); delay(10); } } } }
Code from Processing:
/** * Example sketch for the SerialRecord library for Processing. * * Receives two integers from the serial port, and uses them to control the x * and y position of a circle on the canvas. */ import processing.serial.*; import osteele.processing.SerialRecord.*; Serial serialPort; SerialRecord serialRecord; int step=2; int x=0; void setup() { fullScreen(); size(500, 500); background(0); noCursor(); String serialPortName = SerialUtils.findArduinoPort(2); serialPort = new Serial(this, serialPortName, 9600); serialRecord = new SerialRecord(this, serialPort, 2); serialRecord.values[0] = 0; serialRecord.values[1] = 0; serialRecord.send(); } void draw() { background(0); //float x = map(value1, 0, 1024, 0, width); x+=step; //if (x>=90|| x<=0){ //step=-1*step; //} float r=radians(x); float move=sin(r)*width/2+width/2; circle(move, height/2, 80); serialRecord.values[0] = 0; serialRecord.values[1] = 0; if (x%360==180+step) { serialRecord.values[0] = 1; //serialRecord.send(); } else if (move==0) { serialRecord.values[1] = 1; //serialRecord.send(); } serialRecord.send(); } //void keyPressed() { // if (keyCode==32) { // background(100); // serialRecord.values[0] = 1; // serialRecord.send(); // //fill( // } //}