# What is right method to pass led info from processing to arduino

**URL:** https://discourse.processing.org/t/what-is-right-method-to-pass-led-info-from-processing-to-arduino/8010
**Category:** Electronics (Arduino, etc.)
**Created:** [February 1, 2019, 12:53am UTC](https://discourse.processing.org/t/what-is-right-method-to-pass-led-info-from-processing-to-arduino/8010 "2019-02-01T00:53:58Z")
**Posts on this page:** 16
**Page:** 1

<div class="post-metadata">

### Author: ![angelman](https://avatars.discourse-cdn.com/v4/letter/a/dfb087/32.png) [@angelman](https://discourse.processing.org/u/angelman)
#### Post date: [February 1, 2019, 12:53am UTC](https://discourse.processing.org/t/what-is-right-method-to-pass-led-info-from-processing-to-arduino/8010/1 "2019-02-01T00:53:58Z")

</div>

I am just a beginner. This my processing code; stil lhave a little trouble with the clear part of the input function . but main thing is the arduino write.out. My arduino code works. with the serial port. The processing code connects to the arduino but the data is not registering. Thank you for any help.

```auto
//generate color barcode by typing input , 
//save it to computer and broadcast the colors

import processing.net.*;
import processing.serial.*;
import java.util.*;
import java.lang.reflect.*;
import java.security.*;
import java.awt.Graphics2D;
import controlP5.*;

ControlP5 cp5;
import processing.serial.*;

Serial arduinoPort;

    //Start with an array to store the colorbars.
    int[] colorLines;
    int[] red;
    int[] green;
    int[] blue;
   int n;
   int r1;
   int g1;
   int b1;
    int time;
  
   int rate1;
   int rate2;
   int rate3;
 
   
  boolean hasBeenDrawn = false;
  boolean hasBeenInput = false;
   String inputText;
   boolean colorCodeClear = false;
   String textValue;
   
 
  
    void setup(){
      size(400,280);
    
   // Create the array for width of lines.
       
   {n = 400;
    colorLines = new int[n];
    red = new int[n];
    green = new int[n];
     blue = new int[n];
      // Populate the array with some values.
      for( int i=0; i < colorLines.length; i++){ 
        strokeCap(SQUARE);
        int sW = int(random(20,40));
        strokeWeight(sW);       
      colorLines[i] = sW;
      }
      //noLoop();
    }
  // set arduino port 
 arduinoPort = new Serial(this,"/dev/tty.usbmodem1411", 9600 );
  //create input field via CP5  
  cp5 = new ControlP5(this);
  cp5.addTextfield("Enter Name")
  .setPosition(0, 10)
  .setSize(200,30)
  .setAutoClear(false) 
  .setFont(createFont("times",20))
  .setColor(255);
 //cp5.getController("Enter Name").getCaptionLabel().setColor(color(148,0,211) );
 cp5.getController("Enter Name").getCaptionLabel().setSize(14);

  
  
  cp5.addBang("clear")
     .setPosition(240,10)
     .setSize(100,20)
     .getCaptionLabel().align(ControlP5.CENTER, ControlP5.CENTER)
     ;    
  
  
    }
    void draw()
    {
     background(0);
    
   inputText = cp5.get(Textfield.class,"Enter Name").getText();
   text(inputText,50,70);

    }
    
  void keyPressed() { 
    if (keyCode == ENTER)
    {
      {colorCode();
       broadcast();
       println(inputText);
     
      if (hasBeenDrawn == false)
      {
        
      {saveColorCode();
    hasBeenDrawn = true;
     cp5.getController("Enter Name").getCaptionLabel().setSize(2);
      }
    }
    }
  }
    }

    
   void colorCode()   
   {  
      //Draw each line.
      for( int i = 0; i < 400; i += 16 )
      {
    // Give each line a random color and width each time it's drawn.
     strokeWeight(colorLines[i]);

   float r = int(random(255));
    red[i]=int(r);
    float g = int(random(255));
    green[i] = int(g);
    float b = int(random(255));
    blue[i] = int(b);//blue
    
    stroke(r, g, b);
    line(i, 60 ,i, height);
      }
      }

  //ready for led turn ons using Arduino RGB control 2
  void broadcast()
  { 
    for( int i = 0; i < 400; i += 16 )
    {
     r1 = red[i];
     g1 = green[i];
    b1 = blue[i];
    time = colorLines[i];
  byte out[] = new byte[4];
  out[0] = byte(r1);
  out[1] = byte(g1);
  out[2] = byte(b1);
  //send carriage return character 
  out[3] = byte('\r');
    println(out);
  arduinoPort.write(out);

  //println(out);
   }
 }   
   
  
  void saveColorCode() {
    g = createGraphics(400,200);
    //this.height = dim;
    //this.width = dim;
    g.beginDraw();
    for (int i = 0; i < n; i += 16 )
    {
    strokeWeight(colorLines[i]); 
    stroke(red[i],green[i],blue[i]);
    line(i, 0, i , 250); 
  //put input on top of colorcode
    }
    g.textSize(48);
    g.fill(red[0],green[0],blue[0]);
    g.text(inputText,50,50);
    g.endDraw();
  
    save(inputText);
    println("screenshot saved");
}

public void clear() {
  cp5.get(Textfield.class,"Enter Name").clear();
  cp5.getController("Enter Name").getCaptionLabel().setSize(14);
  colorCodeClear = true;
}
 
 

  

void mousePressed(){
  g.beginDraw();
  g.clear();
  g.endDraw();
  //rect(0,20,400,200);
}

void controlEvent(ControlEvent theEvent) {
  if(theEvent.isAssignableFrom(Textfield.class)) {
    println("controlEvent: accessing a string from controller '"
            +theEvent.getName()+"': "
            +theEvent.getStringValue()
            );
  }
}

public void input(String theText) {
  // automatically receives results from controller input
  println("a textfield event for controller 'input' : "+theText);
}

```

this is my arduino code

````auto
// Example 4 - Receive a number as text and convert it to an int

const byte numChars = 4;
char receivedChars[numChars]; // an array to store the received data
byte values[3];
boolean newData = false;
int i = -1;
int dataNumber = 0;
int colorData;
// new for this version
const int redPin = 5;
const int greenPin = 6;
const int bluePin = 3;

void setup() {
    Serial.begin(9600);
    //
    Serial.println("<Arduino is ready>");
   
  pinMode(redPin, OUTPUT);
  pinMode(greenPin, OUTPUT);
  pinMode(bluePin, OUTPUT);
}

void loop() {
    recvWithEndMarker();
    
    showNewNumbers();
  
    
  
  
}

void recvWithEndMarker() {
    static byte ndx = 0;
    char endMarker = '\n';
    char rc;
   
    if (Serial.available() > 0) {
        rc = Serial.read();
       

        if (rc != endMarker) {
            receivedChars[ndx] = rc;
            ndx++;
            if (ndx >= numChars) {
                ndx = numChars - 1;
            }
            
        
        }
        else {
            receivedChars[ndx] = '\0'; // terminate the string
            ndx = 0;
            newData = true;
        }
    }
}

void showNewNumbers() {

      
   if (newData == true) {
     i++;
      Serial.println(i);
   
      
       dataNumber = atoi(receivedChars); 
 
      
        Serial.print("This just in ... ");
        Serial.println(receivedChars);
        Serial.print("Data as Number ... "); // new for this version
        Serial.println(dataNumber);
        
     
       
        values[i] = dataNumber;
        Serial.println(values[i]);     
        Serial.println(i);
        
   

        if (i == 2) {
        Serial.print("i is ...");
         Serial.println(i);
        broadcastNewColor();
           i = -1;
            } 
           
   }   
   
        
        newData = false;
    }

void broadcastNewColor(){
   
  
  Serial.print("colors as Number ... ");
   
  analogWrite(redPin, values[0]);
   
   Serial.println(values[0]);
   
  
   
    analogWrite(greenPin,values[1]);
   Serial.println(values[1]);
   analogWrite(bluePin, values[2]);
   Serial.println(values[2]);
   delay(10);
  
}```
I think I am still not understanding delay just yet either.
````

---

<div class="post-metadata">

### Author: ![kll](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kll/32/964_2.png) [@kll](https://discourse.processing.org/u/kll)
#### Post date: [February 1, 2019, 1:18am UTC](https://discourse.processing.org/t/what-is-right-method-to-pass-led-info-from-processing-to-arduino/8010/2 "2019-02-01T01:18:35Z")

</div>

and what feed back you get from arduino when you send your setpoints?  
ups: that part : get and show the arduino info in processing seems missing!

---

<div class="post-metadata">

### Author: ![angelman](https://avatars.discourse-cdn.com/v4/letter/a/dfb087/32.png) [@angelman](https://discourse.processing.org/u/angelman)
#### Post date: [February 1, 2019, 1:44am UTC](https://discourse.processing.org/t/what-is-right-method-to-pass-led-info-from-processing-to-arduino/8010/3 "2019-02-01T01:44:12Z")

</div>

when I run the processing program , the Rx led blinks once. is that what you mean? when I use Serial monitor, put in 3 numbers, my RGB led comes on. I have replaced the get and set arduino info by this line; `arduinoPort = new Serial(this,"/dev/tty.usbmodem1411", 9600 );`

“/dev/tty.usbmodem1411” is the port.

---

<div class="post-metadata">

### Author: ![kll](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kll/32/964_2.png) [@kll](https://discourse.processing.org/u/kll)
#### Post date: [February 1, 2019, 8:20am UTC](https://discourse.processing.org/t/what-is-right-method-to-pass-led-info-from-processing-to-arduino/8010/4 "2019-02-01T08:20:36Z")

</div>

> [@angelman](#):
>
> is that what you mean?

no, from arduino you print lots of text to serial port  
do you see this text in processing?

can you give

- a screenshot from the arduino IDE monitor ( terminal )
- and from the processing console print

---

<div class="post-metadata">

### Author: ![kfrajer](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kfrajer/32/196_2.png) [@kfrajer](https://discourse.processing.org/u/kfrajer)
#### Post date: [February 1, 2019, 1:02pm UTC](https://discourse.processing.org/t/what-is-right-method-to-pass-led-info-from-processing-to-arduino/8010/5 "2019-02-01T13:02:27Z")

</div>

> [@angelman](#):
>
> g = createGraphics(400,200);

The line above is correct. However, you should name it something else as g is a variable that Processing uses to manage its main sketch. It is unclear, as I am not able to run your example, if this has anything to do with your reported problem. Nevertheless, I encourage you to implement this change and continue your testing.

Additionally, `Serial.list()` prints all the available serial ports in your platform. I suggest you pick the right element in this array and use it while creating your Serial handler: `arduinoPort = new Serial(this, Serial.list()[?], 9600 );` where ? would be the index in the array you want to use.

Finally, you can try to reduce your code to a minimal version which you could use anytime for testing purposes. This version would be as simple as outputting raw data that you receive from your arduino. No need to have controlP5 or color processing.

Kf

---

<div class="post-metadata">

### Author: ![angelman](https://avatars.discourse-cdn.com/v4/letter/a/dfb087/32.png) [@angelman](https://discourse.processing.org/u/angelman)
#### Post date: [February 1, 2019, 9:24pm UTC](https://discourse.processing.org/t/what-is-right-method-to-pass-led-info-from-processing-to-arduino/8010/6 "2019-02-01T21:24:27Z")

</div>

thank you [kfrajer]; I have commented out the serial events. This way you can use the code in processing. mainly I would like to know how to clear the color bar after clear is pressed. I will work on the other.

````auto
//generate color barcode by typing input , 
//save it to computer and broadcast the colors

import processing.net.*;
import processing.serial.*;
import java.util.*;
import java.lang.reflect.*;
import java.security.*;
import java.awt.Graphics2D;
import controlP5.*;

ControlP5 cp5;
import processing.serial.*;

//Serial arduinoPort;

    //Start with an array to store the colorbars.
    int[] colorLines;
    int[] red;
    int[] green;
    int[] blue;
   int n;
   int r1;
   int g1;
   int b1;
    int time;
  
   int rate1;
   int rate2;
   int rate3;
 
   
  boolean hasBeenDrawn = false;
  boolean hasBeenInput = false;
   String inputText;
   boolean colorCodeClear = false;
   String textValue;
   
 
  
    void setup(){
      size(400,280);
    
   // Create the array for width of lines.
       
   {n = 400;
    colorLines = new int[n];
    red = new int[n];
    green = new int[n];
     blue = new int[n];
      // Populate the array with some values.
      for( int i=0; i < colorLines.length; i++){ 
        strokeCap(SQUARE);
        int sW = int(random(20,40));
        strokeWeight(sW);       
      colorLines[i] = sW;
      }
      //noLoop();
    }
  // set arduino port 
 //arduinoPort = new Serial(this,"/dev/tty.usbmodem1411", 9600 );
  //create input field via CP5  
  cp5 = new ControlP5(this);
  cp5.addTextfield("Enter Name")
  .setPosition(0, 10)
  .setSize(200,30)
  .setAutoClear(false) 
  .setFont(createFont("times",20))
  .setColor(255);
 //cp5.getController("Enter Name").getCaptionLabel().setColor(color(148,0,211) );
 cp5.getController("Enter Name").getCaptionLabel().setSize(14);

  
  
  cp5.addBang("clear")
     .setPosition(240,10)
     .setSize(100,20)
     .getCaptionLabel().align(ControlP5.CENTER, ControlP5.CENTER)
     ;    
  
  
    }
    void draw()
    {
     background(0);
    
   inputText = cp5.get(Textfield.class,"Enter Name").getText();
   text(inputText,50,70);

    }
    
  void keyPressed() { 
    if (keyCode == ENTER)
    {
      {colorCode();
       broadcast();
       println(inputText);
     
      if (hasBeenDrawn == false)
      {
        
      {saveColorCode();
    hasBeenDrawn = true;
     cp5.getController("Enter Name").getCaptionLabel().setSize(2);
      }
    }
    }
  }
    }

    
   void colorCode()   
   {  
      //Draw each line.
      for( int i = 0; i < 400; i += 16 )
      {
    // Give each line a random color and width each time it's drawn.
     strokeWeight(colorLines[i]);

   float r = int(random(255));
    red[i]=int(r);
    float g = int(random(255));
    green[i] = int(g);
    float b = int(random(255));
    blue[i] = int(b);//blue
    
    stroke(r, g, b);
    line(i, 60 ,i, height);
      }
      }

  //ready for led turn ons using Arduino RGB control 2
  void broadcast()
  { 
    for( int i = 0; i < 400; i += 16 )
    {
     r1 = red[i];
     g1 = green[i];
    b1 = blue[i];
    time = colorLines[i];
  byte out[] = new byte[4];
  out[0] = byte(r1);
  out[1] = byte(g1);
  out[2] = byte(b1);
  //send carriage return character 
  out[3] = byte('\r');
    println(out);
  //arduinoPort.write(out);

  //println(out);
   }
 }   
   
  
  void saveColorCode() {
    g = createGraphics(400,200);
    //this.height = dim;
    //this.width = dim;
    g.beginDraw();
    for (int i = 0; i < n; i += 16 )
    {
    strokeWeight(colorLines[i]); 
    stroke(red[i],green[i],blue[i]);
    line(i, 0, i , 250); 
  //put input on top of colorcode
    }
    g.textSize(48);
    g.fill(red[0],green[0],blue[0]);
    g.text(inputText,50,50);
    g.endDraw();
  
    save(inputText);
    println("screenshot saved");
}

public void clear() {
  cp5.get(Textfield.class,"Enter Name").clear();
  cp5.getController("Enter Name").getCaptionLabel().setSize(14);
  colorCodeClear = true;
}
 
 

  

/*void mousePressed(){
  g.beginDraw();
  g.clear();
  g.endDraw();
  //rect(0,20,400,200);
}*/ 

void controlEvent(ControlEvent theEvent) {
  if(theEvent.isAssignableFrom(Textfield.class)) {
    println("controlEvent: accessing a string from controller '"
            +theEvent.getName()+"': "
            +theEvent.getStringValue()
            );
  }
}

public void input(String theText) {
  // automatically receives results from controller input
  println("a textfield event for controller 'input' : "+theText);
}```

I have written this little code;```import processing.serial.*;

Serial serialPort;
  String[] portNames = Serial.list();
  int len = portNames.length ;
  println("portNames has "+len+" elements");
 
  for (int i=0 ; i<len ; i++ )
  {
  println(portNames[i]);
  }
 String portName4 = new String( portNames[3]); // usual needs to be portNames[1] for arduino
 
serialPort = new Serial(this,portName4, 9600);
serialPort.write(65);``` console shows portNames has 8 elements
/dev/cu.AngelmansiPhone-Wireles-1
/dev/cu.Bluetooth-Incoming-Port
/dev/cu.RadhasMacBookPro-Blueto
/dev/cu.usbmodem1411
/dev/tty.AngelmansiPhone-Wireles-1
/dev/tty.Bluetooth-Incoming-Port
/dev/tty.RadhasMacBookPro-Blueto
/dev/tty.usbmodem1411

but where does the letter A (65) meant to appear in the Arduino ?
````

---

<div class="post-metadata">

### Author: ![angelman](https://avatars.discourse-cdn.com/v4/letter/a/dfb087/32.png) [@angelman](https://discourse.processing.org/u/angelman)
#### Post date: [February 1, 2019, 9:27pm UTC](https://discourse.processing.org/t/what-is-right-method-to-pass-led-info-from-processing-to-arduino/8010/7 "2019-02-01T21:27:49Z")

</div>

you are saying that once the right connection is made between the arduino and processing that the serial.print from the arduino should appear in the processing console? and that using terminal (which I am not familiar with but will give it a go ) you should see the serial.write coming from the processing sketch? Is that right?

f I run terminal I get this; Last login: Sat Feb 2 08:48:50 on ttys000  
Allans-MacBook-Pro:~ angelman$ ls /dev/tty.\*  
/dev/tty.AngelmansiPhone-Wireles-1 /dev/tty.RadhasMacBookPro-Blueto  
/dev/tty.Bluetooth-Incoming-Port /dev/tty.usbmodem1411  
Allans-MacBook-Pro:~ angelman$ screen /dev/tty.usbmodem1411  
[screen is terminating]  
Allans-MacBook-Pro:~ angelman$

---

<div class="post-metadata">

### Author: ![kll](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kll/32/964_2.png) [@kll](https://discourse.processing.org/u/kll)
#### Post date: [February 2, 2019, 6:09am UTC](https://discourse.processing.org/t/what-is-right-method-to-pass-led-info-from-processing-to-arduino/8010/8 "2019-02-02T06:09:49Z")

</div>

> [@angelman](#):
>
> and that using terminal (which I am not familiar with but will give it a go )

sorry,  
the Arduino IDE monitor  
is a terminal software  
but you could use any other “serial terminal” software  
but no idea whats good on MAC

so , where is your code, the minimal version what just deal with  
processing serial and arduino serial,  
no CP5, no LED…  
cut it down and make it a extra little project to solve that problem.

---

<div class="post-metadata">

### Author: ![angelman](https://avatars.discourse-cdn.com/v4/letter/a/dfb087/32.png) [@angelman](https://discourse.processing.org/u/angelman)
#### Post date: [February 2, 2019, 8:20am UTC](https://discourse.processing.org/t/what-is-right-method-to-pass-led-info-from-processing-to-arduino/8010/9 "2019-02-02T08:20:28Z")

</div>

so I learned lots today. This is where I am up to; any help I am grateful for.

This is my arduino code which I got from here; [https://forum.arduino.cc/index.php?topic=396450.0](https://forum.arduino.cc/index.php?topic=396450.0)  
This line I cant get to work; dataNumber = atoi(receivedChars);

````auto

``` Example 4 - Receive a number as text and convert it to an int

const byte numChars = 4;
char receivedChars[numChars]; // an array to store the received data
byte values[3];
boolean newData = false;
int i = -1;
int dataNumber = 0;
int colorData;
// new for this version
const int redPin = 5;
const int greenPin = 6;
const int bluePin = 3;

void setup() {
    Serial.begin(9600);
    //
    Serial.println("<Arduino is ready>");
   
  pinMode(redPin, OUTPUT);
  pinMode(greenPin, OUTPUT);
  pinMode(bluePin, OUTPUT);
}

void loop() {
    recvWithEndMarker();
    
    showNewNumbers();
  
    
  
  
}

void recvWithEndMarker() {
    static byte ndx = 0;
    char endMarker = '\r';
    char rc;
   
    if (Serial.available() > 0) {
        rc = Serial.read();
        Serial.print("data received.....");
        
         Serial.print(rc);
         Serial.print(" ");
         Serial.println(rc,DEC);
       

        if (rc != endMarker) {
            receivedChars[ndx] = rc;
            
            ndx++;
            if (ndx >= numChars) {
                ndx = numChars - 1;
            }
            
        
        }
        else {
            receivedChars[ndx] = '\0'; // terminate the string
            ndx = 0;
            newData = true;
        }
    }
}

void showNewNumbers() {

      
   if (newData == true) {
     i++;
      Serial.print("RGB position is.....");
      Serial.println(i);
   
      dataNumber = 0; 
      dataNumber = atoi(receivedChars); 
 
      
        Serial.print("This just in ..... ");
        Serial.println(receivedChars);
        Serial.print("Data as Number ... "); // new for this version
        Serial.println(dataNumber);
        
     
       
        values[i] = dataNumber;
        Serial.print("RGB values...");
        Serial.print(values[i]);
        Serial.print("....position... ");     
        Serial.println(i);
        
   

        if (i == 2) {
        Serial.print("i is ...");
         Serial.println(i);
        broadcastNewColor();
           i = -1;
            } 
           
   }   
   
        
        newData = false;
    }

void broadcastNewColor(){
   
  
  Serial.print("colors as Number ... ");
   
  analogWrite(redPin, values[0]);
   
   Serial.println(values[0]);
   
  
   
    analogWrite(greenPin,values[1]);
   Serial.println(values[1]);
   analogWrite(bluePin, values[2]);
   Serial.println(values[2]);
   delay(10);
  
}```

Here is my output on the processing console; You can see that this line dataNumber = atoi(receivedChars); is not working :sunglasses:
ControlP5 2.2.6 infos, comments, questions at http://www.sojamo.de/libraries/controlP5
<Ar<Arduino is ready>
today
screenshot saved
[0] -102
[1] -17
[2] -40
[3] 13
[0] -93
[1] 34
[2] 47
[3] 13
[0] -77
[1] 85
[2] 0
[3] 13
[0] -48
[1] 64
[2] 123
[3] 13
[0] 64
[1] -13
[2] -114
[3] 13
[0] 36
[1] -62
[2] 47
[3] 13
[0] 20
[1] -69
[2] -44
[3] 13
[0] -89
[1] -74
[2] -55
[3] 13
[0] -37
[1] 68
[2] -63
[3] 13
[0] 44
[1] -5
[2] -34
[3] 13
[0] 100
[1] 105
[2] -100
[3] 13
[0] 43
[1] 85
[2] -110
[3] 13
[0] -30
[1] 54
[2] 87
[3] 13
[0] 73
[1] -3
[2] -98
[3] 13
[0] 106
[1] -67
[2] -81
[3] 13
[0] -127
[1] -57
[2] -18
[3] 13
[0] 40
[1] -36
[2] -55
[3] 13
[0] 5
[1] 53
[2] 56
[3] 13
[0] 77
[1] -120
[2] -113
[3] 13
[0] 38
[1] 78
[2] 84
[3] 13
[0] -115
[1] -76
[2] 1
[3] 13
[0] -24
[1] -44
[2] -39
[3] 13
[0] 72
[1] -29
[2] -89
[3] 13
[0] -124
[1] 97
[2] -55
[3] 13
[0] 61
[1] 64
[2] -66
[3] 13
controlEvent: accessing a string from controller 'Enter Name': today
data received.....� -102
data received.....� -17
data received.....� -40
data received.....
   13
RGB position is.....0
This just in ..... ���

Data as Number ... 0_ **:grinning:**  **strong text**

RGB values...0....position... 0
data received.....� -93
data received....." 34
data received...../ 47
data received.....
   13
RGB position is.....1
This just in ..... �"/
**Data as Number ... 0**
RGB values...0....position... 1
data received.....� -77
data received.....U 85
data received..... 0
data received.....
   13
RGB position is.....2
This just in ..... �U
Data as Number ... 0
RGB values...0....position... 2
i is ...2
colors as Number ... 0
0
0
data received.....� -48
data received.....@ 64
data received.....{ 123
data received.....
   13
RGB position is.....0
This just in ..... �@{
Data as Number ... 0
RGB values...0....position... 0
data received.....@ 64
data received.....� -13
data received.....� -114
data received.....
   13
RGB position is.....1
This just in ..... @�
Data as Number ... 0
RGB values...0....position... 1
data received.....$ 36
data received.....� -62
data received...../ 47
data received.....
   13
RGB position is.....2
This just in ..... $�/
Data as Number ... 0
RGB values...0....position... 2
i is ...2
colors as Number ... 0
0
0
data received..... 20
data received.....� -69
data received.....� -44
data received.....
   13
RGB position is.....0
This just in ..... ��
Data as Number ... 0
RGB values...0....position... 0
data received.....� -89
data received.....� -74
data received.....� -55
data received.....
   13
RGB position is.....1
This just in ..... ���
Data as Number ... 0
RGB values...0....position... 1
data received.....� -37
data received.....� -63
data received.....
   13
RGB position is.....2
This just in ..... �D�
Data as Number ... 0
RGB values...0....position... 2
i is ...2
colors as Number ... 0
0
0
data received....., 44
data received.....� -5
data received.....� -34
data received.....
   13
RGB position is.....0
This just in ..... ,��
Data as Number ... 0
RGB values...0....position... 0
data received.....d 100
data received.....i 105
data received.....� -100
data received.....
   13
RGB position is.....1
This just in ..... di�
Data as Number ... 0
RGB values...0....position... 1
data received.....+ 43
data received.....U 85
data received.....� -110
data received.....
   13
RGB position is.....2
This just in ..... +U�
Data as Number ... 0
RGB values...0....position... 2
i is ...2
colors as Number ... 0
0
0
data received.....� -30
data received.....6 54
data received.....W 87
data received.....
   13
RGB position is.....0
This just in ..... �6W
Data as Number ... 0
RGB values...0....position... 0
data received.....I 73
data received.....� -3
data received.....� -98
data received.....
   13
RGB position is.....1
This just in ..... I��
Data as Number ... 0
RGB values...0....position... 1
data received.....j 106
data received.....� -67
data received.....� -81
data received.....
   13
RGB position is.....2
This just in ..... j��
Data as Number ... 0
RGB values...0....position... 2
i is ...2
colors as Number ... 0
0
0
data received.....� -127
data received.....� -57
data received.....� -18
data received.....
   13
RGB position is.....0
This just in ..... ���
Data as Number ... 0
RGB values...0....position... 0
data received.....( 40
data received.....� -36
data received.....� -55
````

---

<div class="post-metadata">

### Author: ![kll](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kll/32/964_2.png) [@kll](https://discourse.processing.org/u/kll)
#### Post date: [February 2, 2019, 8:53am UTC](https://discourse.processing.org/t/what-is-right-method-to-pass-led-info-from-processing-to-arduino/8010/10 "2019-02-02T08:53:39Z")

</div>

at first i load the code you linked to  
and it does work  
( sorry i just had a arduino leonardo connected. )

 ![2019-02-02_15-41-12_snap](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/5/5b43158e7f58d086b2d8ce660b08ef01c9ed2d6d.png)

meaning it understands one integer number send with [enter]  
it could take 32 bytes but that would be a much too big int

> **[int - Arduino Reference](https://www.arduino.cc/reference/en/language/variables/data-types/int/)**
>
> The Arduino programming language Reference, organized into Functions, Variable and Constant, and Structure keywords.

> On the Arduino Uno (and other ATmega based boards) an int stores a 16-bit (2-byte) value. This yields a range of -32,768 to 32,767 (minimum value of -2^15 and a maximum value of (2^15) - 1).

so what you try to do?  
why you change to 4 bytes?  
why i not see what you send and what it understands?  
did you send one line or 4 lines?

if you need 4 number to send best way would be  
199,299,399,499[enter]  
( i call that a CSV line )  
and you need arduino code to understand that.

- split on ‘,’
- 4 times atoi

if you want send separate lines need identifier  
n999[enter]  
R199[enter]  
G299[enter]  
B399[enter]  
split on first char and identify n ,R, G, B  
atoi rest of string.

i will load your code later to understand it.

---

<div class="post-metadata">

### Author: ![angelman](https://avatars.discourse-cdn.com/v4/letter/a/dfb087/32.png) [@angelman](https://discourse.processing.org/u/angelman)
#### Post date: [February 2, 2019, 9:19am UTC](https://discourse.processing.org/t/what-is-right-method-to-pass-led-info-from-processing-to-arduino/8010/11 "2019-02-02T09:19:27Z")

</div>

I I think I see the problem.  
char receivedChars[numChars]; is 4 bytes. an int is only 2 bytes so dataNumber = atoi(receivedChars); does not work; How to do that?

```auto

```

---

<div class="post-metadata">

### Author: ![kll](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kll/32/964_2.png) [@kll](https://discourse.processing.org/u/kll)
#### Post date: [February 2, 2019, 9:22am UTC](https://discourse.processing.org/t/what-is-right-method-to-pass-led-info-from-processing-to-arduino/8010/12 "2019-02-02T09:22:21Z")

</div>

sure it does work,  
just to get numbers up to 9999

ONE NUMBER

---

<div class="post-metadata">

### Author: ![angelman](https://avatars.discourse-cdn.com/v4/letter/a/dfb087/32.png) [@angelman](https://discourse.processing.org/u/angelman)
#### Post date: [February 2, 2019, 11:29am UTC](https://discourse.processing.org/t/what-is-right-method-to-pass-led-info-from-processing-to-arduino/8010/13 "2019-02-02T11:29:00Z")

</div>

you will in broadcast() in processing I send as;

```auto
byte out[] = new byte[4];
  out[0] = byte(r1);
  out[1] = byte(g1);
  out[2] = byte(b1);
  //send carriage return character 
  out[3] = byte('\r');

 arduinoPort.write(out);
  arduinoPort.bufferUntil('\n');

```

---

<div class="post-metadata">

### Author: ![kll](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kll/32/964_2.png) [@kll](https://discourse.processing.org/u/kll)
#### Post date: [February 2, 2019, 11:48am UTC](https://discourse.processing.org/t/what-is-right-method-to-pass-led-info-from-processing-to-arduino/8010/14 "2019-02-02T11:48:54Z")

</div>

so,  
atoi of that string might be not the right idea.  
and yes, that might be the most efficient protocol for this  
but can never be used via a terminal ( they all ascii )  
and actually i prefer the complete opposite way to do communication:

> [@Good practice for sending data from Arduino to Processing](https://discourse.processing.org/t/good-practice-for-sending-data-from-arduino-to-processing/6339/2):
>
> there are many ways, but for datasets i use CSV style, 1024,1024,1024,1024, per line what actually requires good documentation what data is what sensor… and it is faster insofar as the line sending and line catching via serial/USB ( you would send 4 lines ) takes more time as the string separation. ( your way also needs that value decoding… ) but there are many other way, the fastest still would be a byte stream ( not ASCII ) but i hate, as i can not read it in Arduino IDE terminal t…

---

<div class="post-metadata">

### Author: ![jb4x](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jb4x/32/789_2.png) [@jb4x](https://discourse.processing.org/u/jb4x)
#### Post date: [February 2, 2019, 9:45pm UTC](https://discourse.processing.org/t/what-is-right-method-to-pass-led-info-from-processing-to-arduino/8010/15 "2019-02-02T21:45:46Z")

</div>

Hi angelman,

I think those posts can help you figure it out:

> [@Multiple Values from Processing to Arduino through Serial Port](https://discourse.processing.org/t/multiple-values-from-processing-to-arduino-through-serial-port/4152):
>
> I have managed to send one value from processing to arduino. (which maps mouseX and lights the respective LED on an LED strip of Neopixels) I went through a lot of forums and tried to send Mouse Position(2 values ) from processing to arduino … By trying to separate the numbers using substring and toInt … But then the program doesn’t seem to be working. can anyone share an simple example that sends and receives X and Y values, or multiple values.

> [@Send 3 variable to Arduino?](https://discourse.processing.org/t/send-3-variable-to-arduino/5467):
>
> Hi, I’m trying to send 3 variable w1, w2 and phiV which are 3 parameter changing with the mouse tracking. I was trying to place those variable in 3 arrays and add it in a string but it didn’t work. I think there is one or several steps i have missed somewhere. Can I send a float array or just an int array? I put my program import processing.serial.\*; Serial arduinoport; float w1, w2; float w12twist = 10.0; //int x=1; int Um=100; int Vm=100; float phiU=0; float phiV=HALF\_PI/2; float X1t,…

---

<div class="post-metadata">

### Author: ![angelman](https://avatars.discourse-cdn.com/v4/letter/a/dfb087/32.png) [@angelman](https://discourse.processing.org/u/angelman)
#### Post date: [February 2, 2019, 11:48pm UTC](https://discourse.processing.org/t/what-is-right-method-to-pass-led-info-from-processing-to-arduino/8010/16 "2019-02-02T23:48:55Z")

</div>

I finally got it working yay. here is final code;

```auto
// Example 4 - Receive a number as text and convert it to an int

const byte numChars = 4;
char receivedChars[numChars]; // an array to store the received data
byte values[3];
boolean newData = false;
int i = 0;
long dataNumber0 = 0L;
int dataNumber = 0 ;
int colorData;
// new for this version
const int redPin = 5;
const int greenPin = 6;
const int bluePin = 3;

void setup() {
    Serial.begin(9600);
    //
    Serial.println("<Arduino is ready>");
   
  pinMode(redPin, OUTPUT);
  pinMode(greenPin, OUTPUT);
  pinMode(bluePin, OUTPUT);

 
}
void broadcastNewColor(){
  Serial.print("colors as Number ... ");
  
  analogWrite(redPin, values[0]);
  Serial.println(values[0]);
  
   analogWrite(greenPin,values[1]);
   Serial.println(values[1]);
   
   analogWrite(bluePin, values[2]);
   Serial.println(values[2]);
   delay(10);
  
}
void loop() {
    recvWithEndMarker();
    
    showNewNumbers();
  
    
  
  
}

void recvWithEndMarker() {
    static byte ndx = 0;
    char endMarker = '\r';
    char rc;
   
    if (Serial.available() > 0) {
        rc = Serial.read();
        Serial.print("data received.....");
        
         Serial.print(rc);
         Serial.print(" ");
         Serial.println(rc,DEC);
       

        if (rc != endMarker) {
            receivedChars[ndx] = rc;
            
            ndx++;
            if (ndx >= numChars) {
                ndx = numChars - 1;
            }
            
        
        }
        else {
            receivedChars[ndx] = '\r'; // terminate the string
            ndx = 0;
            newData = true;
        }
    }
}

void showNewNumbers() {
      
   if (newData == true) 
   { 
    for (i=0; i<3; i++)
   {
    
      Serial.print("RGB position is.....");
      Serial.println(i);
   
      //dataNumber = 0; 
      dataNumber = receivedChars[i];
     
 
      
        Serial.print("This just in ..... ");
        Serial.println(receivedChars[i]);
        Serial.print("Data as Number ... "); // new for this version
        Serial.println(dataNumber);
        
     
       
        values[i] = dataNumber;
        Serial.print("RGB value...");
        Serial.print(values[i]);
        Serial.print("....position... ");     
        Serial.println(i);
        
   

        if (i == 2) {
        Serial.print("i is ...");
         Serial.println(i);
        broadcastNewColor();
                      
            } 
            
   }   
   }
        
        newData = false;
     
    return;
    }

```
