Skip to content

Instantly share code, notes, and snippets.

@atduskgreg
Created November 8, 2011 20:52
Show Gist options
  • Star 12 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save atduskgreg/1349176 to your computer and use it in GitHub Desktop.
Save atduskgreg/1349176 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);
}
@fubarKing
Copy link

I tried running these two sketches together and printed result to console, result remained 0. Did you test this yourself?
EDIT: my above observation was based on running the processing code, closing it and then viewing the results in the arduino serial monitor. Once I wrote some code to send the the results back to processing and print them in the processing serial monitor the correct results were displayed.

@rmirandan
Copy link

Thanks a lot! This code is just what i was looking for. Is there a way to send integers via Serial?
I need to send bigger numbers (more than from 0 - 255), or do you recommend me to use the map() function to scale them?
I'm really new at programming.

Thanks a lot anyway.

@thesauband
Copy link

these codes are working i add Serial.println() statement to watch the values, thank you very much for the codes

@sanchez777
Copy link

I've been trying to send more than one value for some days, until I found your code. It works perfectly, thank you!

@victorslimak1
Copy link

I seem to be getting a lot of interference when i do it this way. I am only sending 1s and 0s but they seem to not be correct all the time. Has anybody else experienced this and what was the soulution? Thank you.

@alanisjer
Copy link

Hi, the code works perfectly with DC motors, I tried to work with stepper motors, but when one turns on and changes speed the other motor shuts down, can you help me?

@21mehran
Copy link

Hi! Greg Borenstein....the code worked for me. I am using P5.js so making slight changes to code helped me drive the servo motors.
Thanks a lot man!!

@aliahsan001
Copy link

Hi, the code works perfectly with DC motors, I tried to work with stepper motors, but when one turns on and changes speed the other motor shuts down, can you help me?

Hi, I am also trying to work with 3 stepper motors 28BYJ-48 am facing a different issue like the speed of motor is not what i set in the processing. e.g if i set it 200 step/sec it run less than that. Did you figure out your problem and can help please?

@21mehran
Copy link

21mehran commented Nov 18, 2020 via email

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