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

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

// 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;
    }
3 Likes