send data from processing to arduino

Hi! :slight_smile:

I’m making a program in processing and arduino so that when you press 0 on the keyboard, arduino lights led 0, etc… with 6 leds. The problem is that, i can see arduino gets the data (by small built in leds), but the card doesn’t power my leds.

Well, if you want to help me, here is the processing part :

import processing.serial.*;

Serial port;
String portname = “COM3”;
int baudrate = 9600;
int value = 0;

void setup(){

port = new Serial(this, portname, baudrate);
println(port);

}

void draw(){

if(keyPressed) {
if (key == ‘0’) {
port.write (“0”);
}
if (key == ‘1’) {
port.write (“1”);
}
if (key == ‘2’) {
port.write (“2”);
}
if (key == ‘3’) {
port.write (“3”);
}
if (key == ‘4’) {
port.write (“4”);
}
if (key == ‘5’) {
port.write (“5”);
}
}
}

And here is the Arduino part :

int pinArray = {
4,5,6,7,8,9 };
int led;

void setup(){

pinMode(pinArray, OUTPUT);

beginSerial(9600);
}

void loop(){

if( serialAvailable() )
{
led = serialRead;
digitalWrite (pinArray[led], HIGH);

}
delay(10);
}

Hmm, in your Arduino sketch, you have to call serialRead, it is a function, hence “led = serialRead();” rather than “led = serialRead;”.

Also, you have to understand that you are mixing two different things here: a number x and the ASCII representation of the character x. In C, “5” and 5 are different things. You have to convert the character read from serial to an index for you array. You can do something like this

if (serialAvailable())
{
char led;

led = (char)serialRead();
led -= (char)‘0’;

if (led > -1 && led < THE_SIZE_OF_YOUR_PINARRAY)
digitalWrite(pinArray[led], HIGH);
}

This will do the conversion since 0, 1, 2 and so on are directly after each other in the ASCII table. The check wether led is inside the array bounds should be there in case garbage data is read via serial (you have to replace THE_SIZE_OF… with the actual size of your pinArray, like 5 or 6).

hello,

i did a similar thing. maybe you just have to send integers instead of strings in the processing part.

here are parts of my code:

processing:

for(int i=8; i>=0; i–){
// Clear the buffer, or available() will still be > 0:
myPort.clear();
myPort.write(i);
// delay
delay(200);
}

arduino:

// read the serial port
int val = serialRead();
// if the input is ‘-1’ then there is no data
if (val != -1) {
value = val;
}

cheers,
fux

Thanks for your replies, but i’m still stuck. So I tried to simplify the 2 parts (i always think too big when i start out… ;)). Now, as I understand there should not be any problem because i clearly state the data is an integer in both programs. Anyway, it doesn’t change a bit.
This is what i have now :

Arduino ::::::::::::::::::::::::::::::::::::::::::::::::

int pinArray = {
4,5,6,7,8,9 };
int led;

void setup(){

pinMode(pinArray, OUTPUT);

beginSerial(9600);
}

void loop(){

if (serialAvailable())
{
int led;
led = serialRead();

digitalWrite(pinArray[led], HIGH);
}
delay(10);
}

Processing :::::::::::::::::::::::::::::::::::::::::::::::::::::::::.

import processing.serial.*;

Serial port;
String portname = “COM3”;
int baudrate = 9600;
int value = 0;

void setup(){

port = new Serial(this, portname, baudrate);
println(port);

}

void draw(){

int i = 0;

port.write (i);
println (i);

}

It’s possible that if you pass an integer to serial.write() in Processing, it sends the ASCII characters for the individual digits (e.g. the integer 10 would get sent as the two bytes ‘1’, ‘0’). Try:

port.write((char) i);

If that doesn’t work, you could go back to your original Processing code and translate from strings/characters to integers in Arduino:

int whichLed = serialRead() - '0'; // '0' gives the ASCII character of the digit 0.

Okay, I’m trying it :slight_smile: