We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpElectronics,  Serial Library › Write Bytes to and read Bytes from Arduino
Page Index Toggle Pages: 1
Write Bytes to and read Bytes from Arduino (Read 4892 times)
Write Bytes to and read Bytes from Arduino
Jan 18th, 2010, 10:18am
I am trying to send 9 bytes(with the intention to send much more later if I manage to send these 9 first) to ARDUINO and directly read them and print both writen and read values to processing.Is this possible to directly Serial.write and then Serial.read?I guess it is.. The values that processing returns from ARDUINO are always -1 so I can't confirm processing sends the bytes to ARDUINO. Any suggestion will be highly appreciated...thanks

//This code reads the red pixel values of a 1x9 array
//Then it writes to and read from the serial port
//The "writen" and "read" Bytes are then printed

import processing.serial.*;
Serial port;
int initval=0; //initial value that the matrix starts
int pixval; //pixel value each time
int rows = 9; //number of the matrix columns

port = new Serial(this,Serial.list()[1], 9600);
PImage myImage;
myImage = loadImage("jelly9x9.jpg");
image (myImage, 0, 0, width, height);

for (pixval = initval; pixval < initval + 1*rows; pixval = pixval+1) {
color a = myImage.pixels[pixval];
float ared = red(a);
int inta = int(ared);
int inByte = port.read();

int inData[10];  // Allocate some space for the Bytes
byte inByte;   // Where to store the Bytes read
byte index = 0;   // Index into array; where to store the Bytes
int ledPin = 13;  // Set the pin to digital I/O

void setup(){
 pinMode(ledPin, OUTPUT);

void loop()
 while(Serial.available() > 0){
   if(index < 9) // One less than the size of the array
inByte = Serial.read(); // Read a Byte
inData[index] = inByte; // Store it
       digitalWrite(ledPin, HIGH);
       if (index = 9){
       digitalWrite(ledPin, LOW);

Re: Write Bytes to and read Bytes from Arduino
Reply #1 - Jan 22nd, 2010, 9:14am
Just a couple of things that might help debugging your code.
if (index = 9) 

(will always be true, but also mess up your code) It should be: Code:
if (index == 9)  

Apart from that, I'm not sure about how well it works to send a bunch of bytes with serial. I've only used it to send something like 4, with values from sensors and pots via Arduino. Tom Igoe (Physical Computing) has good examples on how to send bytes, but I don't remember if that was a whole lot of bytes or not.

Btw, is the LED thing only to show activity? Is your real goal to store and send bytes back and forth?
Re: Write Bytes to and read Bytes from Arduino
Reply #2 - Feb 2nd, 2010, 7:52pm
Thanks for the de-bugging help
Page Index Toggle Pages: 1