Task #1: Make a Processing Etch-A-Sketch
For this task we were directed to build a sketch in processing and Arduino, that would simulate an Etch-A-Sketch, my approach for building the circuit was based on Nov 17’s class, however, the circuit for today’s recitation required 2 potentiometers, one controlling the X-axis, and the other one controlling the Y-axis. The picture of the finished circuit can be found below. The second step of task 1 is to open the “analog read serial” so we can check if everything is wired correctly. After checking, I opened the “send multiple values” sketch and modified it so it could take values from the 2 potentiometers, it also allowed to read the values on the serial monitor in a more stable way. Moreover, the sketch also contained the serial record that would allow Arduino to communicate with processing. The code from Arduino can be found below, as well as the video.
First Circuit build:
Testing Analog Read Serial:
Arduino Code:
#include “SerialRecord.h”
// Change this number to send a different number of values
SerialRecord writer(2);
void setup() {
Serial.begin(9600);
}
void loop() {
int sensorValue1 = analogRead(A0);
int sensorValue2 = analogRead(A1);
writer[0] = sensorValue1;
writer[1] = sensorValue2;
writer.send();
delay(10);
}
Testing Send Multiple Values:
After the Arduino part was done, I opened processing, and built a sketch that used “Receive multiple values” so it allows to read what the Arduino is sending. After, I drew the circle sketch and it worked pretty well. The video can be found below. After verifying that it worked, I made the background black, and switched the circle for a line. I established 2 variables in order to control the x and y axis of the line. I also had to move the background to setup, so that the stroke movement is recorded. Also, I added a “if” statement, in order to be able to erase the old strokes. Video, and code can be found below.
Processing circle:
Processing final Code test:
Processing code:
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); serialRecord = new SerialRecord(this, serialPort, 2); } void draw() { serialRecord.read(); float value1 = serialRecord.values[0]; float value2 = serialRecord.values[1]; if( key == 'b') { background(0); } float x = map(value1, 0, 1024, 0, width); float y = map(value2, 0, 1024, 0, height); stroke(255); line(x, y, px, py); px=x; py=y; //circle(x,y,100); }
For task 2, I decided to work with my neighbor. This time, the information is sent from processing into Arduino using “serial record” and “receive multiple values”, and after it is sent, it will make the servo motors “hit the ball” and make bounce into the opposite side. The circuit we came up for can be found below. After a little bit of trial and error, we managed to fix the Arduino code, because after the hitting the ball, it wouldn’t retract back into original position. And it only hit it once. The video for the final sketch can be found below, as well as the code for processing, and Arduino.
Circuit:
Final video:
Arduino Code:
import processing.serial.*;
import osteele.processing.SerialRecord.*;
Serial serialPort;
SerialRecord serialRecord;
int x = 0;
int size = 200;
int b = 0;
void setup(){
fullScreen();
String serialPortName = SerialUtils.findArduinoPort();
serialPort = new Serial(this, serialPortName, 9600);
serialRecord = new SerialRecord(this, serialPort, 2);
}
void draw(){
drawBack();
drawBall();
}
void drawBack(){
background(0);
}
void drawBall(){
noStroke();
int R = floor(map(x, 0, width, 0, 255));
int B = floor(map(x, 0, width, 255, 0));
fill(R, 0, B);
circle(x, height/2, size);
if(x < width && b % 2 == 0)
{
x += 5;
serialRecord.values[0] = 0;
serialRecord.values[1] = 0;
}
if(x > 0 && b % 2 != 0)
{
x -= 5;
serialRecord.values[0] = 0;
serialRecord.values[1] = 0;
}
if(x == 0 || x == width)
{
b++;
if(x == 0){
serialRecord.values[0] = 1;
}
if(x == width){
serialRecord.values[1] = 1;
}
}
serialRecord.send();
}
Leave a Reply