Skip to content

Instantly share code, notes, and snippets.

@shiffman
Last active August 29, 2015 14:23
Show Gist options
  • Select an option

  • Save shiffman/5df48e47bfbaa336c134 to your computer and use it in GitHub Desktop.

Select an option

Save shiffman/5df48e47bfbaa336c134 to your computer and use it in GitHub Desktop.
Does this Processing code match properly with this Ardunio code?
int val;
void setup() {
Serial.begin(9600);
pinMode(3, INPUT);
}
void loop() {
val = analogRead(0);
Serial.write(val);
}
import processing.serial.*;
int val = 0; // To store data from serial port, used to color background
Serial port; // The serial port object
void setup() {
size(200, 200);
port = new Serial(this, Serial.list()[0], 9600);
}
void draw() {
// Set the background
background(val);
}
// Called whenever there is something available to read
void serialEvent(Serial port) {
// Read the data
val = port.read();
}
int val;
void setup() {
Serial.begin(9600);
pinMode(3, INPUT);
}
void loop() {
// Only send out if something has come in
if (Serial.available() > 0) {
Serial.read();
val = analogRead(0);
Serial.write(val);
}
}
import processing.serial.*;
int val = 0; // To store data from serial port, used to color background
Serial port; // The serial port object
void setup() {
size(200, 200);
port = new Serial(this, Serial.list()[0], 9600);
port.write(65);
}
void draw() {
background(val);
}
void serialEvent(Serial port) {
val = port.read();
// Request a new value
port.write(65);
}
int sensor1 = 0;
int sensor2 = 0;
int sensor3 = 0;
void setup() {
Serial.begin(9600);
pinMode(3, INPUT);
}
void loop() {
if (Serial.available() > 0) { // Only send if you have hear back
Serial.read();
sensor1 = analogRead(0);
sensor2 = analogRead(1);
sensor3 = analogRead(2);
// Send the integer out as a string using print instead of write
Serial.print(sensor1);
// Send a comma -- ASCII code 44
Serial.print(',');
Serial.print(sensor2);
Serial.print(',');
Serial.print(sensor3);
// Send an new line -- ASCII code 10
Serial.print('\n');
}
}
import processing.serial.*;
int r, g, b; // Used to color background
Serial port; // The serial port object
void setup() {
size(200, 200);
port = new Serial(this, Serial.list()[0], 9600);
// Request values right off the bat
port.write(65);
}
void draw() {
background(r, g, b);
}
// Called whenever there is something available to read
void serialEvent(Serial port) {
// Read the data
String input = port.readStringUntil('\n');
if (input != null) {
// Print message received
println("Receiving: " + input);
// Split up the string into an array of integers
int[] vals = int(splitTokens(input, ",\n";));
// Fill r,g,b variables
r = vals[0];
g = vals[1];
b = vals[2];
}
// When finished ask for values again
port.write(65);
}
@kasperkamperman

Copy link
Copy Markdown

Serial.print('\n');

It more common to write: Serial.println();

You can also add bufferUntil:

// set buffer full flag on receipt of a newline (println from Arduino).
// the serialEvent is only triggered when a newline is received.
serialPort.bufferUntil(10);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment