Write Bytes to and read Bytes from Arduino

Hello,
I am trying to send 9 bytes to ARDUINO and then read them and print the 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

ARDUINO CODE:

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); 
  Serial.begin(9600);
}

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
      index++; 
        Serial.write(inByte);
        digitalWrite(ledPin, HIGH);
        if (index = 9){
        digitalWrite(ledPin, LOW);
        }
    }
  }
}

Processing Code

//This code reads the red pixel values of a 1x9 Matrix
//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);
size(300,300);
PImage myImage; 
myImage = loadImage("jelly9x9.jpg");
image (myImage, 0, 0, width, height);

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

Are you sure that Serial.list()[1] is the correct port?

Does the led come on?

Does the RX led on the Arduino flash as data is sent to it?

I see nothing obviously wrong with either the Arduino or the Processing code, with the possible exception of the wrong port being specified.

Hello, yes the PORT is correct I am sure about it. I get blinks on the LED pin13 and the TX/RX on ARDUINO - its COM4 to be more specific. Do you think there is necessary any byte/ASCII conversion? From your answer I understand that it is not wrong to write to and directly read back from ARDUINO...strange..I still get only -1s.thanks a lot for your reply.

I'd have the Arduino collect the data from the Processing application until it had enough to do something, then send back a reply. The Processing sketch is sending 9 bytes.

The Arduino sketch should save all 9 bytes into an array, and then send back a reply.

The Processing sketch should use Serial::available (port.available()) to determine if there is data to be read, before trying to read.

Hello again and thanks a lot for the replies…
I modified a bit my code to store an array in arduino but nothing seems to be printed in processing.When I remove the condition if (port.available() > 0 in processing it prints just a -1.
Any advice will be appreciated…

In the ARDUINO code i added a long allBytes to store all the values in an array and then I send them to processing:

void loop()
{
  if (Serial.available() > 0){
    
  while (Serial.available() > 0 && index < 9){

      inByte = Serial.read(); // Read a Byte
      inData[index] = inByte; // Store it
      index++; 
        delay(100);
        allBytes += inByte;
        if (index == 8){
        Serial.write(allBytes);
        }       
    }
  }
}

In the Processing code I added the if statement

 if (port.available() > 0 && pixval == rows-1) {
 int inByte = port.read();
 println(inByte);
 delay(100);
}

The allBytes variable is not an array. That's not how you store all the data in an arrray.

Not that it matters, since the issue isn't what the Arduino is doing with the incoming data (which you are already saving in an array), but why no response is being received from the Arduino in the Processing program.

I sent you a PM, too, asking for the image file you are using, so I can try the Processing and Arduino sketches myself.

By the way, what OS are you using?

I am on Windows XP

I found a couple of problems with your code, on the Processing side.

First, you’re missing the setup and draw functions. These perform the same role as the setup and loop functions in the Arduino.

Second, when a serial port is opened, the Arduino resets. It takes a little while before the serial port is read by the Arduino. Anything sent to the Arduino before it is ready is lost.

This is the Arduino code is ended up with:

byte inByte;   // Where to store the Bytes read
int ledPin = 13;  // Set the pin to digital I/O
int index = 0;

void setup()
{
  pinMode(ledPin, OUTPUT);
  Serial.begin(9600);
}

void loop()
{
  if(Serial.available() > 0)
  {  
    inByte = Serial.read(); // Read a Byte
    Serial.print(index);
    Serial.print(") The arduino received: ");
    Serial.println(inByte, HEX);
    index++;
  }
}

You can easily change this back to the code you had, since, as it turns out, the problems were all on the Processing side.

This is the Processing sketch that received all the data sent by Processing:

//This code reads the red pixel values of a 1x9 Matrix
//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
PImage myImage;
int pass = 0;

void setup()
{
  port = new Serial(this,Serial.list()[0], 9600);
  size(300,300);
  myImage = loadImage("jelly9x9.jpg");
  image (myImage, 0, 0, width, height);
  myImage.loadPixels();
}


void draw()
{
  if(pass == 0)
  {
    pass = 1;
    delay(2000);
    for (pixval = initval; pixval < initval + 1*rows; pixval = pixval+1)
    {
      color a = myImage.pixels[pixval];
      float ared = red(a);
      int inta = int(ared);
      print("Sending value #");
      print(pixval);
      print(" ");
      println(hex(inta));
      
      port.write(inta);
      delay(500);
      
      while(port.available()>0)
      {
        char inChar = port.readChar();
        print(inChar);
      }
      println();
    }
  }  
}

You should be able to expand on and modify this, as long as you keep the draw and setup functions, and allow time for the serial connection to be established. I had to change the index into the Serial.list array for my setup; you’ll need to change it back for your setup.

WORKS GREAT! one small question - whats the role of the pass==0 within the void draw() in the processing code? thanks a lot for the great help and for your replies!

Like loop in the Arduino sketch, draw is called over and over again in the Processing sketch. I didn't want it to anything on the second and subsequent calls, so I created the pass variable to determine if draw had been called more than once. It only does something (send and receive) on the first call.

good programming solution!!!!thanks..

Hi there, I ran your code and it works great. keep up the great work..i'm new to the arduino word but i'm picking up faster than i thought due to your support community. One quick question thoug, i didn't get the point behind "pass" variable..once pass gets a one, it stays as a one forever doesn't it? thank you again

once pass gets a one, it stays as a one forever doesn't it?

Yes, it does. What the original poster was trying to do was to send data to processing to be drawn once.

Processing calls the draw function in an endless loop, similar to the way that Arduino calls the loop function.

Since an action was to be taken once, the pass variable gets set to one, when that action occurs. From then on, the action will never occur again, since the flag that is being checked (pass) has been set to indicate that the action has already occurred.

i see..awesome...we only need to display the pic once then..thank you for your reply

I know this may not be the right forum to ask but speaking of arduino and processing code…anybody knows where i can get the library to learn how to program in arduino languahe…i know it’s similar to C/C++ but it’ll better for me to read all the libs and functions available…instead of googling every time to find an exmaple…

anybody knows where i can get the library to learn how to program in arduino languahe..i know it's similar to C/C++ but it'll better for me to read all the libs and functions available

Start here: http://arduino.cc/en/Reference/HomePage

Thank you...I meant for processing code..I noticed the page did not mention draw() and all the stuff we use for GUI...is it on the Arduino website as well

Processing functions:

http://processing.org/reference/