RESITATION:
Sorry for post so late. I realized my post was missing at Sunday, while those days I could not spare time to make it up. This is my second time recitation 9, hope you can forgive my late.
Arduino:
void setup() {
Serial.begin(9600);
}
void loop() {
int sensor1 = analogRead(A0);
int sensor2 = analogRead(A1);
int sensor3 = digitalRead(9);
// keep this format
Serial.print(sensor1);
Serial.print(“,”); // put comma between sensor values
Serial.print(sensor2);
Serial.print(“,”);
Serial.print(sensor3);
Serial.println(); // add linefeed after sending the last sensor value
// too fast communication might cause some latency in Processing
// this delay resolves the issue.
delay(100);
}
Processing:
import processing.serial.*; String myString = null; Serial myPort; int NUM_OF_VALUES = 3; int[] sensorValues; PImage img; float j=0; float i=0; float x=0; float y=0; float speedX=10; float speedY=10; void setup() { size(1000, 1000); background(0); setupSerial(); img = loadImage("pumpkin.png"); } void draw() { updateSerial(); printArray(sensorValues); imageMode(CENTER); float j=map(sensorValues[0],0,1023,0,500); float i=map(sensorValues[1],0,1023,0,10); background(0); image(img, x, y, j*1.2, j*1.2); filter(BLUR, i); if (sensorValues[2]==0 ){ x=300; y=300; }else{ x=x+1.5*speedX; y=y+0.8*speedY; if(x>width||x<0){ speedX=-speedX; } if(y>height||y<0){ speedY=-speedY; } } // } void setupSerial() { printArray(Serial.list()); myPort = new Serial(this, Serial.list()[ 3], 9600); myPort.clear(); // Throw out the first reading, // in case we started reading in the middle of a string from the sender. myString = myPort.readStringUntil( 10 ); // 10 = '\n' Linefeed in ASCII myString = null; sensorValues = new int[NUM_OF_VALUES]; } void updateSerial() { while (myPort.available() > 0) { myString = myPort.readStringUntil(10); if (myString != null) { String[] serialInArray = split(trim(myString), ","); if (serialInArray.length == NUM_OF_VALUES) { for (int i=0; i<serialInArray.length; i++) { sensorValues[i] = int(serialInArray[i]); } } } } }
I’d like to use two the potentiometers and a Botton to control the movements of image. At the first, I checked the code of the button online and have no idea how to move the digital Read(); to the processing, after asking help from assistant, I found out using sensorValue(); can transform all into processing;