Atlas Scientific RGB Code Help

Hi,

I need some assistance with coding or rather un-coding. I have a working Atlas Scientific RGB sensor and have no problem getting the code to read the RGB values coming from the sensors. However what I need to do with the code is kicking my butt. I will add that I am not a novice Arduino user but most of what I do doesn’t require advanced methods and working with strings currently has me scratching my head.

What I am trying to do is split the RGB string coming from the sensor and assign each corresponding piece of code to a variable, for instance redval = (redstring). I am ultimatly then going to color correct this with multiplication (this I know how to do) and send each R, G, B value to a RGBLED.

I’m currently having trouble getting started. Any and all help is welcomed!

/*
This software was made to demonstrate how to quickly get your Atlas Scientific product running on the Arduino platform.
An Arduino Duemilanove board was used to test this code.
This code was written in the Arudino 1.0 IDE
Modify the code to fit your system.
Code efficacy was NOT considered, this is a demo only.
The soft serial port TX line goes to the RX pin.
The soft serial port RX line goes to the TX pin.
Make sure you also connect to power and GND pins to power and a common ground.
Data is received and re-sent through the Arduinos hardware UART TX line.
Open TOOLS > serial monitor, set the serial monitor to the correct serial port and set the baud rate to 38400.
Remember, select carriage return from the drop down menu next to the baud rate selection; not "both NL & CR".
The data from the Atlas Scientific product will come out on the serial monitor.
Type in a command in the serial monitor and the Atlas Scientific product will respond.
*/




#include <SoftwareSerial.h>                                                    //add the soft serial libray
#define rxpin 2                                                                //set the RX pin to pin 2 Green
#define txpin 3                                                                //set the TX pin to pin 3 White


SoftwareSerial myserial(rxpin, txpin);                                         //enable the soft serial port


String inputstring = "";                                                       //a string to hold incoming data from the PC
String sensorstring = "";                                                      //a string to hold the data from the Atlas Scientific product
boolean input_stringcomplete = false;                                          //have we received all the data from the PC
boolean sensor_stringcomplete = false;                                         //have we received all the data from the Atlas Scientific product


  void setup(){                                                                //set up the hardware
     Serial.begin(38400);                                                      //set baud rate for the hardware serial port to 38400
     myserial.begin(38400);                                                    //set baud rate for software serial port to 38400
     inputstring.reserve(5);                                                   //set aside some bytes for receiving data from the PC
     sensorstring.reserve(30);                                                 //set aside some bytes for receiving data from Atlas Scientific product
     }
 
 
 
   void serialEvent() {                                                         //if the hardware serial port receives a char
               char inchar = (char)Serial.read();                               //get the char we just received
               inputstring += inchar;                                           //add it to the inputString
               if(inchar == '\r') {input_stringcomplete = true;}                //if the incoming character is a <CR>, set the flag
              }  
 
 
 
 void loop(){                                                                   //here we go....
     
  if (input_stringcomplete){                                                   //if a string from the PC has been recived in its entierty 
      myserial.print(inputstring);                                             //send that string to the Atlas Scientific product
      inputstring = "";                                                        //clear the string:
      input_stringcomplete = false;                                            //reset the flage used to tell if we have recived a completed string from the PC
      }
 

  while (myserial.available()) {                                               //while a char is holding in the serial buffer
         char inchar = (char)myserial.read();                                  //get the new char
         sensorstring += inchar;                                               //add it to the sensorString
         if (inchar == '\r') {sensor_stringcomplete = true;}                   //if the incoming character is a <CR>, set the flag
         }


   if (sensor_stringcomplete){                                                 //if a string from the Atlas Scientific product has been received in its entirety
       Serial.println(sensorstring);                                             //use the hardware serial port to send that data to the PC
       sensorstring = "";                                                      //clear the string:
       sensor_stringcomplete = false;                                          //reset the flag used to tell if we have received a completed string from the Atlas Scientific product
      }
}
String inputstring = "";                                                       //a string to hold incoming data from the PC
String sensorstring = "";                                                      //a string to hold the data from the Atlas Scientific product
boolean input_stringcomplete = false;                                          //have we received all the data from the PC

     inputstring.reserve(5);                                                   //set aside some bytes for receiving data from the PC
     sensorstring.reserve(30);                                                 //set aside some bytes for receiving data from Atlas Scientific product

The last two lines here indicate that you have a pretty good idea how big the arrays of chars need to be. I'd strongly suggest that you ditch the String class and use NULL terminated char arrays. This will then let you use the strtok() function to extract tokens.

  if (input_stringcomplete){                                                   //if a string from the PC has been recived in its entierty 
      myserial.print(inputstring);                                             //send that string to the Atlas Scientific product
      inputstring = "";                                                        //clear the string:
      input_stringcomplete = false;                                            //reset the flage used to tell if we have recived a completed string from the PC
      }

If you get some serial data, in the serialEvent() function, print it and delete it. Hmmm, OK. What is this input data, and why are you getting it, if you don't want it?

   if (sensor_stringcomplete){                                                 //if a string from the Atlas Scientific product has been received in its entirety
       Serial.println(sensorstring);                                             //use the hardware serial port to send that data to the PC
       sensorstring = "";                                                      //clear the string:
       sensor_stringcomplete = false;                                          //reset the flag used to tell if we have received a completed string from the Atlas Scientific product
      }

You do the same thing here. If you don't want the data, why are you getting it?

Which of these String instances (to be replaced by strings) contains the data that you want to parse and use? What does the other instance contain?

What, exactly, does the data stream look like?

Parsing well defined strings is pretty easy. Parsing poorly structured strings is a bitch.

I'm not sure what is going on with the code. I tried another version that is lurking on the forums but it would never work for me. This is the example code from Atlas and it seems to be the only version that I can get to work.

Keep in mind I know very little about what exactly is going on in the code... I have tried several techniques to break it down and clean it out but it always stops working.

Thanks for the help.

I'm sorry I didn't answer you question.

the string of code is simply RGB...

example: 255,210,100

This is all thats printed in the serial monitor.

The proper ratio of answers to questions is 1:1 or higher. Your ratio is a lot lower. When you get it up to 1:1, maybe we can help.

I also posted here so that my problem could be shared and help others in the future.

But, you didn't think it necessary to answer all the questions asked? Fine. Don't expect this thread to help anybody else, then.

I answered your questions…?
I simply did not edit my original post before following up with a quick answer post sorry.

I answered your questions…?

Well, let’s see.

These were the questions:

What is this input data, and why are you getting it, if you don’t want it?

If you don’t want the data, why are you getting it?

Which of these String instances (to be replaced by strings) contains the data that you want to parse and use?

What does the other instance contain?

What, exactly, does the data stream look like?

And your answers:

I’m not sure what is going on with the code. I tried another version that is lurking on the forums but it would never work for me. This is the example code from Atlas and it seems to be the only version that I can get to work.

I don’t see that this answered any of them.

Keep in mind I know very little about what exactly is going on in the code… I have tried several techniques to break it down and clean it out but it always stops working.

Nor did this.

I’m sorry I didn’t answer you question.

the string of code is simply RGB…

example: 255,210,100

This is all thats printed in the serial monitor.

This was an attempt to answer the questions, but I have no idea which one.

Let’s try again.

Serial data is coming from two sources - the software serial port and the hardware serial port. Presumably, the Atlas Scientific RGB is connected to the software serial port. If that is the case, is it sending anything to the Arduino? If not, why are you reading data that will never appear?

That’s two questions. Two answers are needed.

What is running on the PC, connected to the other end of the hardware serial port? What are you entering in that application, to be sent to the Arduino.

Two more questions. Two more answers needed.

When you supply those 4 answers, we can determine how to proceed.

Serial data is coming from two sources - the software serial port and the hardware serial port. Presumably, the Atlas Scientific RGB is connected to the software serial port. If that is the case, is it sending anything to the Arduino? If not, why are you reading data that will never appear?

Ok I think I get what your asking. According to Atlas the sensor has a built in computer chip that process and outputs the code for RGB in true 8-bit RGB format these are the numbers I am receiving with their code. It also apparently outputs light intensity as one of the configuration modes. The data output is single comma separated string. I am using the software serial port and I am not sure what if anything is actually going to the Arduino. I only received a working version of this sensor a few weeks ago. What I receive is RGB values comma separated on the serial monitor. I'm not really sure what data is passing to the Arduino.

I will tell you that when I run their "super easy code" I receive a string of numbers that are not the same as the RGB values I receive when using their regular code. Below is their super easy code... and their is no mistake that is all of the code. void setup() {} void loop(){} I'm not at work but I will try and post the output from the super easy code tomorrow.

I also reached out to Atlas and asked for some further explanations and breakdowns of their hardware along with best practices for the Arduino. They understood the problems and are working a more expanded Arduino tutorial. This seems to be a recurring theme with their sensors... great product but a pain to get figured out.

Thanks for the help