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

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.

//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

// 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.
1 Like

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!

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.

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

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

1 Like

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.

//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 ?

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$

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.

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
This line I cant get to work; dataNumber = atoi(receivedChars);



``` 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

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

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

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.

1 Like

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?

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

ONE NUMBER

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

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');

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:

Hi angelman,

I think those posts can help you figure it out:

2 Likes

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