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);
}