task 1:
code for 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 pmX; int pmY; int mx; int my; 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]; pmX=(int)mx; pmY=(int)my; float x = map(value1, 0, 1024, 0, width); float y = map(value2, 0, 1024, 0, height); //circle(x, y, 20); mx=(int)x; my=(int)y; if(pmX!=mx || pmY!=my){ stroke(255); line(pmX, pmY, mx, my); } println(pmX, pmY, mx, my); }
It is a quite interesting task, and it is interactive. I learned to combine the potentiometer with processing and arduino, which was challenging but fun.
task 2:
(I worked on my own)
processing:
import processing.serial.*; import osteele.processing.SerialRecord.*; Serial serialPort; SerialRecord serialRecord; float x = 0; float d=10; void setup() { size(500,500); //background(176,131,250); String serialPortName = SerialUtils.findArduinoPort(); serialPort = new Serial(this, serialPortName, 9600); serialRecord = new SerialRecord(this, serialPort, 1); //serialRecord.logToCanvas(false); } void draw() { background(0); circle(x,height/2,20); x+=d; int m=(int)x; if (x>=width){ d= -d; } if (x<=0){ d= -d; } serialRecord.values[0] = m; serialRecord.send(); }
arduino:
#include "SerialRecord.h" #include // Change this number to the number of values you want to receive SerialRecord reader(1); Servo servo1; void setup() { Serial.begin(9600); servo1.attach(9); pinMode(9, OUTPUT); } void loop() { reader.read(); if (reader[0]>450){ servo1.write(180); }; if (reader[0]<=450){ servo1.write(60); }; }
It’s a pity that I didn’t find a partner, but I worked by myself and gradually find the solution to this task.
Leave a Reply