Last active
April 22, 2019 13:13
-
-
Save Ellie-1809/2c964a65def9ed49d306da3360251dac 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
void setup() { | |
Serial.begin(9600); | |
} | |
void loop() { | |
int sensor1 = analogRead(A0); | |
int sensor2 = analogRead(A1); | |
Serial.print(sensor1); | |
Serial.print(","); | |
Serial.print(sensor2); | |
Serial.println(); | |
delay(100); | |
} |
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 NUM_OF_VALUES = 2; | |
int[] sensorValues; | |
int PORT_INDEX = 5; | |
void setup() { | |
size(500, 500); | |
background(0); | |
setupSerial(); | |
} | |
void draw() { | |
updateSerial(); | |
printArray(sensorValues); | |
// use the values like this! | |
// sensorValues[0] | |
//background(0); | |
float x = map(sensorValues[0], 0, 1024, 0, 500); | |
float y = map(sensorValues[1], 0, 1024, 0, 500); | |
noStroke(); | |
ellipse(x, y, 10, 10); | |
} | |
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