For this recitation, the first few steps included building a circuit with two potentiometers and writing an Arduino sketch that reads their values and sends them serially. I didn’t have trouble building the circuit as it was a similar process to what we’ve done in previous recitations many times. I tested the connections by opening the serial monitor in Arduino and turning the potentiometers to see if the values were reactive to the potentiometers’ motion, meaning that the potentiometers were working.
After knowing I had a working circuit, I wrote a processing sketch drawing a circle and read the two analog values from Arduino. The sketch modifies the circle’s x and y values based on the input values from Arduino from the potentiometers connected to the circuit.
The circle moved up and down based on the potentiometer values. I had trouble creating lines from the movement of the circle. It was supposed to emulate an etch-a-sketch and create a drawing from the movements of plastic knobs requiring similar motion as potentiometers.
Code:
Arduino Code:
#include "SerialRecord.h"// Change this number to send a different number of valuesSerialRecord writer(2);void setup() {Serial.begin(9600);}void loop() {int sensorValue = analogRead(A0);writer[0] = millis() % 1024;writer[1] = sensorValue;writer.send();// This delay slows down the loop, so that it runs less frequently. This can// make it easier to debug the sketch, because new values are printed at a// slower rate.delay(10);}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);
}
#include "SerialRecord.h"#include <Servo.h>Servo myservo;Servo myservo2;int step;int pos;// Change this number to the number of values you want to receiveSerialRecord reader(2);void setup() {// Servosmyservo.attach(9);myservo2.attach(8);myservo.write(0);myservo2.write(0);Serial.begin(9600);}void loop() {if (reader.read()) { //Rudi helped and suggestedif (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);}}}}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();
}
We found this task quite challenging. Mainly the coding aspect of getting the servos to do precisely what we wanted, such as range of motion and timing, was challenging. The initial task also gave me coding difficulties. I’ll need to practice and improve my coding knowledge to be more proficient during the recitations.
Leave a Reply