Created
May 1, 2019 13:54
-
-
Save Jl9549/4de4941c07c4727b3382c19cdf4e88de to your computer and use it in GitHub Desktop.
Processing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import processing.serial.*; | |
String myString = null; | |
Serial myPort; | |
PImage photo; | |
PImage photo2; | |
int PORT_INDEX =0; | |
int NUM_OF_VALUES = 2; /** YOU MUST CHANGE THIS ACCORDING TO YOUR PROJECT **/ | |
int[] sensorValues; /** this array stores values from Arduino **/ | |
void settings() | |
{ | |
photo = loadImage("ExamplePhoto.png"); | |
photo2 = loadImage("orca.jpg"); | |
size(1000, 1000); | |
} | |
void setup() {; | |
background(0); | |
setupSerial(); | |
} | |
void draw() { | |
background(0); | |
updateSerial(); | |
int x = int(map (sensorValues[0], 0, 1023, 10, 500)); | |
int y = int(map (sensorValues[1], 0, 1023, 0, 700)); | |
printArray(sensorValues[1]); | |
image(photo, x, y); | |
image(photo2, mouseX, mouseY); | |
// use the values like this! | |
// sensorValues[0] | |
// add your code | |
// | |
} | |
void setupSerial() { | |
printArray(Serial.list()); | |
myPort = new Serial(this, Serial.list()[ PORT_INDEX ], 9600); | |
// WARNING! | |
// You will definitely get an error here. | |
// Change the PORT_INDEX to 0 and try running it again. | |
// And then, check the list of the ports, | |
// find the port "/dev/cu.usbmodem----" or "/dev/tty.usbmodem----" | |
// and replace PORT_INDEX above with the index number of the port. | |
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 ); // 10 = '\n' Linefeed in ASCII | |
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]); | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment