Last active
February 9, 2016 10:07
-
-
Save Arty2/c0e7a4d8dfeaa57ec6d3 to your computer and use it in GitHub Desktop.
Arduino to Processing with Serial
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
int knob_0_pin = A0; | |
int knob_1_pin = A1; | |
int knob_2_pin = A2; | |
int knob_3_pin = A3; | |
int knob_4_pin = A4; | |
int knob_5_pin = A5; | |
void setup() { | |
Serial.begin(9600); | |
} | |
/* | |
prefer Serial.write to Serial.print for Processing | |
a small delay helps as well, too small works not | |
*/ | |
void loop() { | |
int knob_0 = analogRead(knob_0_pin); | |
int knob_1 = analogRead(knob_1_pin); | |
int knob_2 = analogRead(knob_2_pin); | |
int knob_3 = analogRead(knob_3_pin); | |
int knob_4 = analogRead(knob_4_pin); | |
int knob_5 = analogRead(knob_5_pin); | |
if (Serial) { | |
Serial.print(knob_0); | |
Serial.print(','); | |
Serial.print(knob_1); | |
Serial.print(','); | |
Serial.print(knob_2); | |
Serial.print(','); | |
Serial.print(knob_3); | |
Serial.print(','); | |
Serial.print(knob_4); | |
Serial.print(','); | |
Serial.print(knob_5); | |
Serial.println(' '); | |
} | |
} |
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
/* | |
Sending anything other than structured bytes from Arduino to Processing, is a fucking nightmare. | |
read: https://www.arduino.cc/en/Tutorial/SerialCallResponse | |
IMPORTANT: First open Processing, then boot Arduino, or stream gets cut in the middle and causes errors. | |
*/ | |
import processing.serial.*; | |
Serial port; | |
String input; | |
PFont mono; | |
void settings() { | |
//fullScreen(); | |
size(640, 480); | |
} | |
void setup() { | |
println(Serial.list()); | |
port = new Serial(this, Serial.list()[0], 9600); | |
//prevent reading from middle of stream, or try to | |
port.bufferUntil('\n'); | |
port.clear(); | |
input = port.readStringUntil('\n'); | |
input = null; | |
} | |
void draw() { | |
if ( port.available() > 0) { | |
input = port.readStringUntil('\n'); | |
if (input != null) { | |
println(input); //DEBUG | |
//split input to an array | |
input = trim(input); | |
String[] tokens = split(input, ','); | |
//map input to your variables, may need a check for NULL | |
float[] value = new float[6]; | |
value[0] = map(float(tokens[0]), 0, 1023, 1, 4381376); | |
value[1] = map(float(tokens[1]), 0, 1023, 1, 10815); | |
value[2] = map(float(tokens[2]), 0, 1023, -9290, 9290); | |
value[3] = map(float(tokens[3]), 0, 1023, 0, 9290); | |
value[4] = map(float(tokens[4]), 0, 1023, 32, 3817); | |
value[5] = map(float(tokens[5]), 0, 1023, 1, 751); | |
printArray(value); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment