Pages: [1]   Go Down
Author Topic: Problem with color sensor  (Read 1294 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 17
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 238
Posts: 24322
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
« Last Edit: October 06, 2012, 01:32:01 pm by AWOL » Logged

"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.

Offline Offline
Newbie
*
Karma: 0
Posts: 17
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 238
Posts: 24322
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
i get my readings but they are all gibberish
No, there're mostly -1.
Re-read reply #1
Logged

"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.

Offline Offline
Newbie
*
Karma: 0
Posts: 17
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Not able to figure out anything  smiley-cry
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 238
Posts: 24322
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
« Last Edit: October 06, 2012, 02:29:09 pm by AWOL » Logged

"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.

Nice, France
Offline Offline
Full Member
***
Karma: 11
Posts: 234
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
(color[3]-'0')*100)
« Last Edit: October 06, 2012, 02:34:18 pm by Nantonos » Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 238
Posts: 24322
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Logged

"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.

Pages: [1]   Go Up
Jump to: