[SOLVED] Problems sending data from Processing to Arduino through serial port

Hi @bacum,

I’m confused reading your code. Your processing is talking to the com port on the PC, but the Arduino code is reading “xbee” which is using software serial on 2 pins. The basic Arduino serial uses “Serial” which is built in serial from the PC via usb.

Taking your requirement “communication from Processing to Arduino through the Serial Port” I’ve used your Processing sketch with only 1 tiny change, and a very simplified Arduino sketch.

xbee = new Serial(this, "COM17", 9600); // (calling it xbee can be confusing, at the moment it's only talking to the Arduino).

My Arduino sketch started as yours, but removed xbee and software serial. Now, as you press chars on processing sketch, it flashes one of 4 LEDs. I hope you can see this work, then progressively merge in your xbee comms.


#define dW digitalWrite
 
char serialData;

int ledA = 5;
int ledB = 4;
int ledC = 3;
int ledD = 2;

void setup() 
{
  pinMode(ledA, OUTPUT);
  pinMode(ledB, OUTPUT);
  pinMode(ledC, OUTPUT);
  pinMode(ledD, OUTPUT);
  
  Serial.begin(9600);
  
  // Test the LEDs
  dW(ledA, 1); delay(100); dW(ledA, 0); delay(100);
  dW(ledB, 1); delay(100); dW(ledB, 0); delay(100);
  dW(ledC, 1); delay(100); dW(ledC, 0); delay(100);
  dW(ledD, 1); delay(100); dW(ledD, 0); delay(100);  
}
 
void loop() 
{  
  if (Serial.available()) 
  {
    // Recieve Serial from the PC
    serialData = Serial.read();
    // turn 1 led on...
    if (serialData == '0') {dW(ledA, 1);}
    if (serialData == '1') {dW(ledB, 1);}
    if (serialData == '2') {dW(ledC, 1);}      
    if (serialData == '3') {dW(ledD, 1);}      
  }
  delay(200);
  // turn all leds off.
  dW(ledA, 0);
  dW(ledB, 0);
  dW(ledC, 0);
  dW(ledD, 0);
}
1 Like