0

I know this has been asked before, but I'm new to Arduino and Processing, and I'm trying to take this step by step.

I would like to pass an RGB value from processing, into Arduino via the Serial.Write/Read.

I have my Processing as:

void draw(){

  //orange colour in RGB values
  color _rgbColor = color(255, 40, 0);

  //convert colour into hex
  String _hexColor = hex(_rgbColor, 6);

  //convert colour into binary
  String _binaryColor = binary(_rgbColor);

  //pass colour to port
  myPort.write(_hexColor);
  delay(500);
}

I assume I can just pass the HEX value straight across, right?

First problem I get is that I can't debug what my Arduino device is getting (when I run the Processing program, the Serial Monitor wont work. Seems like I can only use one or the other). I can print the myPort.read within my Processing after the delay, and I get:

-1

So I'm not quite sure what I'm doing wrong there.

On the Arduino side, I'm using Serial.read() to retrieve the Serial data, which I assume will all be broken down into bytes, so I need to then convert it into a HEX formate, right?

----- Update -----

I now know that I need to break down my color variable into a char-message. I'm guessing I can do something like this then, for example: And then on the other side (Arduino), I need to rebuild the message, right?

if(mousePressed == true){
    //Red
     myPort.write('2'); 
     myPort.write('5'); 
     myPort.write('5');
     myPort.write(','); 

     //Green
     myPort.write('0'); 
     myPort.write('3'); 
     myPort.write('0');
     myPort.write(','); 

     //Blue
     myPort.write('0'); 
     myPort.write('0'); 
     myPort.write('0');
     myPort.write('\n'); 
  }

My Arduino script is as follows, is this an okay route to go down?

String message;
void loop() {

  int _size = Serial.available();

  if(_size > 0){

    char value = Serial.read();
    message += value;

    //full message received (255,255,255)
    if(message.length() == 11){
      String _red = message.substring(0,3);
      String _green = message.substring(4,7);
      String _blue = message.substring(8,11);
      Serial.println("R:" + _red + " G:" + _green + " B:" + _blue);

      //apply to light system
      setColour(_red.toInt(), _green.toInt(), _blue.toInt());

      //clear buffer
      message = "";
      Serial.flush();
    }
  }
}
20
  • You should digest this first: hackingmajenkoblog.wordpress.com/2016/02/01/…
    – Majenko
    Dec 16, 2016 at 18:41
  • 1
    Also you should understand exactly how the UART protocol (RS-232) works.
    – Majenko
    Dec 16, 2016 at 18:47
  • Surely someone, somewhere has already made a defined script for this? To simply pass a colour variable to the Serial? Dec 16, 2016 at 19:52
  • 1
    Many people have, yes. It is a simple enough operation once you understand how a UART works that it becomes a non-entity and you don't need to really worry about finding special code for it.
    – Majenko
    Dec 16, 2016 at 19:53
  • The simplest way is to print the three colour values as a comma-separated line, maybe with a prefix character to indicate the start (C23,49,119\n for example). Then it is a case of reading that string and interpreting it properly.
    – Majenko
    Dec 16, 2016 at 19:54

2 Answers 2

1

Add print(_hexColor); to you processing sketch. The value it's sending should appear in the Processing Console (at the bottom)

1

I know I'm late, but actually got a working solution, when I was working out a solution for this.

Code in Processing assuming variable myPort is handled in setup():

void draw() {
    int r = 255;
    int g = 123;
    int b = 2;
    myPort.write('S'); // tell the arduino to do something with leds
    myPort.write(r);
    myPort.write(g);
    myPort.write(b);
}

Code for Arduino loop:

void loop() {
  int r;
  int g;
  int b;
  if(Serial.available()) {   
    if(Serial.read() == 'S') {
      while(!Serial.available()){}
      r = Serial.read();
      while(!Serial.available()){}
      g = Serial.read();
      while(!Serial.available()){}
      b = Serial.read();
    }
    delay(10); // some recovery time, not sure if nessecary

    // do something with rgb values
  }
}

This works for me with every possible RGB range. It's not the most elegant solution, but by far better then converting between strings/chars and ints with different bit counts on different devices.

2
  • 002 is an octal number. In this case it's still 2, but 020 would be 16. Don't use leading zeros, unless you actually want octal numbers.
    – gre_gor
    Sep 4, 2017 at 18:46
  • you're right, I wrote this on the fly!
    – Cronay
    Sep 5, 2017 at 20:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.