Go Down

Topic: Problem with color sensor (Read 1 time) previous topic - next topic

amruth11

Hi all , i am having trouble with making this color sensor work. I dont know what i have done wrong in my code but i am not able to get it to work.

This is the code i am using:
Code: [Select]

char color[19];

void setup(){
  Serial.begin(9600);
  Serial.write("Ready");
  Serial.println();
  delay(100);
}

void loop(){
  if(Serial.available() != 0){
    for(int x=1; x<18; x++){
      color[x] = Serial.read();
      if(color[18] == 0x0A){
        Calibrate();
      }
    }
  }
}

void Calibrate(){
  int R;
  int G;
  int B;
 
  R = (((color[3]-'0')*100) + ((color[4]-'0')*10) + (color[5]-'0'));
  G = (((color[9]-'0')*100) + ((color[10]-'0')*10) + (color[11]-'0'));
  B = (((color[15]-'0')*100) + ((color[16]-'0')*10) + (color[17]-'0'));
 
  Serial.println(R);
  Serial.println(G);
  Serial.println(B);
  Serial.println();
}


And this is the datasheet of the sensor:  http://www.sunrom.com/files/1185-datasheet.pdf

I need advice and help on how to get this working fast.
Thx in advance.

Amruth


AWOL

#1
Oct 06, 2012, 08:29 pm Last Edit: Oct 06, 2012, 08:32 pm by AWOL Reason: 1
Usual problem - check to see if there's at least one character available to read, then go ahead and read all 18 of them.
Also, array indices usually begin at zero.

Quote
. I dont know what i have done wrong in my code but i am not able to get it to work.

You need to define what "work" means to you in this context.
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

amruth11

i get my readings but they are all gibberish, i have set baud rates correctly but still the readings are in negative and above 1000.

AWOL

Quote
i get my readings but they are all gibberish

No, there're mostly -1.
Re-read reply #1
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

amruth11

Not able to figure out anything  =(

AWOL

#5
Oct 06, 2012, 09:18 pm Last Edit: Oct 06, 2012, 09:29 pm by AWOL Reason: 1
Look at your code
Is there a single character in the serial receive buffer?
Yes, so read all eighteen of them.

Does that seem like a sensible thing to do?

Some useful reading:
http://arduino.cc/en/Serial/Available    

Hint: you're not the first person to make this mistake.
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Nantonos

#6
Oct 06, 2012, 09:31 pm Last Edit: Oct 06, 2012, 09:34 pm by Nantonos Reason: 1

Usual problem - check to see if there's at least one character available to read, then go ahead and read all 18 of them.


I agree that is a problem with the code as posted.


Also, array indices usually begin at zero.


They do, but oddly, part of the code (the read loop) is 1-based while another part (the parsing) is correctly 0-based.  As an example, the first bit of actual information is the fourth character, "Red Value Hundreds Character ASCII" and the code correctly accesses this fourth item as
Code: [Select]
(color[3]-'0')*100)

AWOL

Quote
help on how to get this working fast.
At 9600 it is never going to be particularly fast.

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy