Created
April 23, 2019 14:24
-
-
Save Jl9549/ecc8dd27792af61574f1b6dbd18ab68e to your computer and use it in GitHub Desktop.
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; | |
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 setup() { | |
size(500, 500); | |
background(0); | |
setupSerial(); | |
} | |
void draw() { | |
//background(0); | |
updateSerial(); | |
printArray(sensorValues); | |
float use = map(sensorValues[0], 0, 1023, 0, 500); | |
float use2 = map(sensorValues[1], 0, 1023, 0, 500); | |
ellipse(use, use2, 100, 100); | |
// use the values like this! | |
// sensorValues[0] | |
// add your code | |
// | |
} | |
void mousePressed() | |
{ | |
background(0); | |
} | |
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