Skip to content

Instantly share code, notes, and snippets.

@sfm1234
Forked from atduskgreg/arduino_firmware.cpp
Created September 13, 2022 15:28
Show Gist options
  • Save sfm1234/4166e1e831c50bbabd49540ecc05980d to your computer and use it in GitHub Desktop.
Save sfm1234/4166e1e831c50bbabd49540ecc05980d to your computer and use it in GitHub Desktop.
send multiple values over serial from Processing to Arduino
int currentValue = 0;
int values[] = {0,0};
void setup() {
Serial.begin(9600);
}
void loop() {
if(Serial.available()){
int incomingValue = Serial.read();
values[currentValue] = incomingValue;
currentValue++;
if(currentValue > 1){
currentValue = 0;
}
// after this point values[]
// has the most recent set of
// all values sent in from Processing
}
}
// import the processing serial library
import processing.serial.*;
// and declare an object for our serial port
Serial port;
void setup() {
// Get the name of the first serial port
// where we assume the Arduino is connected
String portName = Serial.list()[0];
// initialize our serial object with this port
// and the baud rate of 9600
port = new Serial(this, portName, 9600);
}
void draw() {
int value1 = 123;
int value2 = 17;
// load up all the values into a byte array
// then send the full byte array out over serial
// NOTE: This only works for values from 0-255
byte out[] = new byte[2];
out[0] = byte(value1);
out[1] = byte(value2);
port.write(out);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment