Sensing color with the ADJD-S311 + Arduino

Discussions area for bildr's blog posts.
You need to decide what blue is. there are a million was to do it, but I only know the wrong ways.


if(blue < 50 && red > 100 && green < 50){
//is red
}else if(blue > 100 && red < 50 && green < 50){
//is blue
}else if(blue < 50 && red < 50 && green > 100){
//is green
}


There are many better ways to do this, I just dont know them.

Sorry.


Perri wrote:Hey, thanks for the help, the sensor is calibrating now. But how can we let the sensor read colors, and then let these readings trigger movements for the arm? So for instance, when the color sensor reads blue, it sends the value to our program, and then the program triggers the movement for the arm. Again, thanks for the help!
ameyer
Founder
 
Posts: 2981
Joined: Thu Jan 21, 2010 11:59 pm
Location: The Bay Area

Hey, thanks for the reply, but we really dont understand anything of that code..
Perri
 
Posts: 7
Joined: Thu Sep 20, 2012 2:15 am

I know, it's complicated.

So you need to as your self. What is red?
Red is when the reflected light coming off an object is MOSTLY red. Not completely red. You will never have only red reflected off an object.

So red is when the red reading is much higher than the blue and green. So you look at the red, the blue and the green and you say that if the red is over 100 (just a random number you will have to mess with it) and the blue/green are both under 50.... Well that must be red!

BUT... If you need to test for magenta for instance, that would be when the red and blue are both high, but a very low green.

Green is when only green is high.

And so on.

Color is CRAZY weird. I once wrote an essay on the color white. White is the most complicated of them all.

But you need to train your robot by picking up a brick shove it in the face of the sensor, and write down what the reading is.

Then figure out a way to check if that reading is close to that.
If it is... bam... it's the same color!


So you could say that a red brick reads at (I have no clue) 150, 34, 47.

So you could do something like this to see if the colors are within a tolerance (you need to choose this) of what you expected, you are good.

if abs(readRed - expectedRed) < tolerance) you are good
if abs(readBlue - expectedBlue) < tolerance) you are good
if abs(readGreen - expectedGreen) < tolerance) you are good



Perri wrote:Hey, thanks for the reply, but we really dont understand anything of that code..
ameyer
Founder
 
Posts: 2981
Joined: Thu Jan 21, 2010 11:59 pm
Location: The Bay Area

Hi there

Thank you for the tutorial! I just received my ADJD-S311 and I think I've set everything up correctly but I have some problems. I downloaded the library but when I try to verify it before I upload to the UNO, I get a lot of error messages:

adjd_s311:2: error: variable or field 'lightLED' declared void
adjd_s311:2: error: 'RGBC' was not declared in this scope
adjd_s311:4: error: 'ADJDS311' does not name a type
adjd_s311.cpp: In function 'void setup()':
adjd_s311:14: error: 'colorSensor' was not declared in this scope
adjd_s311.cpp: In function 'void loop()':
adjd_s311:23: error: 'RGBC' was not declared in this scope
adjd_s311:23: error: expected `;' before 'color'
adjd_s311:25: error: 'color' was not declared in this scope
adjd_s311:33: error: 'lightLED' was not declared in this scope
adjd_s311.cpp: At global scope:
adjd_s311:39: error: variable or field 'lightLED' declared void
adjd_s311:39: error: 'RGBC' was not declared in this scope

Do you have any idea what this is? Have I somehow set the library up wrong (I've never actually used libraries, just single sketches)?

As you may be sensing, my Arduino knowledge is a bit flaky. I'm quite new to the game and have until now only been doing simple things with IR distance sensors and feeding the data into Max/MSP which is more like my home turf. For those projects I've managed to find ready-made Arduino sketches and with very little changes to the code got everything to work as I wanted. With errors like I'm getting now I simply wouldn't know where to start, to be honest.

I hope you'll be able to guide me in the right direction with this so I can try out the sensor!

Thanks!
hugigu
 
Posts: 3
Joined: Thu Dec 13, 2012 5:02 pm

Yeah, the library just isnt in the right place, or you didnt remember to quit arduino after you installed the library. (It will only pick it up if it is there when arduino launches)


hugigu wrote:Hi there

Thank you for the tutorial! I just received my ADJD-S311 and I think I've set everything up correctly but I have some problems. I downloaded the library but when I try to verify it before I upload to the UNO, I get a lot of error messages:

adjd_s311:2: error: variable or field 'lightLED' declared void
adjd_s311:2: error: 'RGBC' was not declared in this scope
adjd_s311:4: error: 'ADJDS311' does not name a type
adjd_s311.cpp: In function 'void setup()':
adjd_s311:14: error: 'colorSensor' was not declared in this scope
adjd_s311.cpp: In function 'void loop()':
adjd_s311:23: error: 'RGBC' was not declared in this scope
adjd_s311:23: error: expected `;' before 'color'
adjd_s311:25: error: 'color' was not declared in this scope
adjd_s311:33: error: 'lightLED' was not declared in this scope
adjd_s311.cpp: At global scope:
adjd_s311:39: error: variable or field 'lightLED' declared void
adjd_s311:39: error: 'RGBC' was not declared in this scope

Do you have any idea what this is? Have I somehow set the library up wrong (I've never actually used libraries, just single sketches)?

As you may be sensing, my Arduino knowledge is a bit flaky. I'm quite new to the game and have until now only been doing simple things with IR distance sensors and feeding the data into Max/MSP which is more like my home turf. For those projects I've managed to find ready-made Arduino sketches and with very little changes to the code got everything to work as I wanted. With errors like I'm getting now I simply wouldn't know where to start, to be honest.

I hope you'll be able to guide me in the right direction with this so I can try out the sensor!

Thanks!
ameyer
Founder
 
Posts: 2981
Joined: Thu Jan 21, 2010 11:59 pm
Location: The Bay Area

Thanks, you are absolutely right. There was a mistake in the folder structure in my libraries folder. I started all over and it now works like a charm. Thanks again for sharing the code and for your help!
hugigu
 
Posts: 3
Joined: Thu Dec 13, 2012 5:02 pm

Hello again.

After I fixed the library issue (see previous post from me) I got the sensor working and sending consistant data to Max/MSP. I spent one amazing night with her and there were real fireworks going on. I was consistantly tracking 8 different colors (slight inconsistancy between different calibrations, but nothing too serious). However, when I woke up the next morning she was gone....

When I open the serial monitor it says "iniinit" and nothing more happens. When I handled the sensor I noticed some strange activity with the led, flashing on and off even if the pin was set to low (or vice versa) so I assumed there was some bad connection. I measured all the solderings and switched all the jumper wires but to no avail. I then figured the board was burnt and switched to another ADJD-S311 but the result was exactly the same. I've only got 2 so I can't try any more.

Have you got any clue what might be going on? I find it strange that it worked so well for one night and then something must have happened. And if the board was burnt, why didn't the new board work for at least a moment....? Have you got any suggestions for thing I could check to try to isolate the problem?

All help and suggestions are very well appreciated!

Best regards,
H
hugigu
 
Posts: 3
Joined: Thu Dec 13, 2012 5:02 pm

Check the wiring 10 times, and try a different bread board. It sounds like a hook up issue.

Also try uploading new code. Do you have a second arduino to test with?



hugigu wrote:Hello again.

After I fixed the library issue (see previous post from me) I got the sensor working and sending consistant data to Max/MSP. I spent one amazing night with her and there were real fireworks going on. I was consistantly tracking 8 different colors (slight inconsistancy between different calibrations, but nothing too serious). However, when I woke up the next morning she was gone....

When I open the serial monitor it says "iniinit" and nothing more happens. When I handled the sensor I noticed some strange activity with the led, flashing on and off even if the pin was set to low (or vice versa) so I assumed there was some bad connection. I measured all the solderings and switched all the jumper wires but to no avail. I then figured the board was burnt and switched to another ADJD-S311 but the result was exactly the same. I've only got 2 so I can't try any more.

Have you got any clue what might be going on? I find it strange that it worked so well for one night and then something must have happened. And if the board was burnt, why didn't the new board work for at least a moment....? Have you got any suggestions for thing I could check to try to isolate the problem?

All help and suggestions are very well appreciated!

Best regards,
H
ameyer
Founder
 
Posts: 2981
Joined: Thu Jan 21, 2010 11:59 pm
Location: The Bay Area

Hi ameyer, Thanks for the library and tutorial! I followed the wiring and uploaded the codes, got a white object in front of the sensor, but my serial monitor showed nothing, I don't know how to get it calibrated. I wonder if it is because of my wiring, I use a different controller : the Romeo V2 all in one controller, which is a ATMega32U4. Could you please tell me how do you calibrate with the Arduino software.
plblj
 
Posts: 6
Joined: Mon Jun 17, 2013 8:54 am

With the Romeo V2 the I2C pins are not on Analog4/Analog5
Look here: http://www.dfrobot.com/wiki/images/f/fc/RomeoV2R3.png

Connect SDA of the sensor to SDA on the Romeo, and SCL of the sensor to SCL on the Romeo.

plblj wrote:Hi ameyer, Thanks for the library and tutorial! I followed the wiring and uploaded the codes, got a white object in front of the sensor, but my serial monitor showed nothing, I don't know how to get it calibrated. I wonder if it is because of my wiring, I use a different controller : the Romeo V2 all in one controller, which is a ATMega32U4. Could you please tell me how do you calibrate with the Arduino software.
ameyer
Founder
 
Posts: 2981
Joined: Thu Jan 21, 2010 11:59 pm
Location: The Bay Area

Thank you very much for showing the correct connection, I connected my SDA and SCL to those pins on my Romeo, but there is still noting on the serial monitor, do I also need to replace the code
int sdaPin = A4;
int sclPin = A5;
with some other codes?

ameyer wrote:With the Romeo V2 the I2C pins are not on Analog4/Analog5
Look here: http://www.dfrobot.com/wiki/images/f/fc/RomeoV2R3.png

Connect SDA of the sensor to SDA on the Romeo, and SCL of the sensor to SCL on the Romeo.

plblj wrote:Hi ameyer, Thanks for the library and tutorial! I followed the wiring and uploaded the codes, got a white object in front of the sensor, but my serial monitor showed nothing, I don't know how to get it calibrated. I wonder if it is because of my wiring, I use a different controller : the Romeo V2 all in one controller, which is a ATMega32U4. Could you please tell me how do you calibrate with the Arduino software.
plblj
 
Posts: 6
Joined: Mon Jun 17, 2013 8:54 am

Where do you see the code: sdaPin = A4 ?

It is possible this code isn't compatible with this board. (I dont know either way)

plblj wrote:Thank you very much for showing the correct connection, I connected my SDA and SCL to those pins on my Romeo, but there is still noting on the serial monitor, do I also need to replace the code
int sdaPin = A4;
int sclPin = A5;
with some other codes?

ameyer wrote:With the Romeo V2 the I2C pins are not on Analog4/Analog5
Look here: http://www.dfrobot.com/wiki/images/f/fc/RomeoV2R3.png

Connect SDA of the sensor to SDA on the Romeo, and SCL of the sensor to SCL on the Romeo.

plblj wrote:Hi ameyer, Thanks for the library and tutorial! I followed the wiring and uploaded the codes, got a white object in front of the sensor, but my serial monitor showed nothing, I don't know how to get it calibrated. I wonder if it is because of my wiring, I use a different controller : the Romeo V2 all in one controller, which is a ATMega32U4. Could you please tell me how do you calibrate with the Arduino software.
ameyer
Founder
 
Posts: 2981
Joined: Thu Jan 21, 2010 11:59 pm
Location: The Bay Area

Thank you very much for your connection guide, finally got it working. I want to control the motors with color, could you please help me write a code that controls the motor to run by the 4 RGBC data? The motors will run if the red value is <=678 the green value is <=681 the blue value <= 704

Red Green Blue Clear
Data: 678 681 704 1013

and my code for the motor is
Code: Select all
      digitalWrite(motor_pin1,LOW);
      digitalWrite(motor_pin2,HIGH);
      digitalWrite(motor_pin3,HIGH);
      digitalWrite(motor_pin4,LOW);

How can I write the codes?


ameyer wrote:Where do you see the code: sdaPin = A4 ?

It is possible this code isn't compatible with this board. (I dont know either way)

plblj wrote:Thank you very much for showing the correct connection, I connected my SDA and SCL to those pins on my Romeo, but there is still noting on the serial monitor, do I also need to replace the code
int sdaPin = A4;
int sclPin = A5;
with some other codes?

ameyer wrote:With the Romeo V2 the I2C pins are not on Analog4/Analog5
Look here: http://www.dfrobot.com/wiki/images/f/fc/RomeoV2R3.png

Connect SDA of the sensor to SDA on the Romeo, and SCL of the sensor to SCL on the Romeo.

plblj wrote:Hi ameyer, Thanks for the library and tutorial! I followed the wiring and uploaded the codes, got a white object in front of the sensor, but my serial monitor showed nothing, I don't know how to get it calibrated. I wonder if it is because of my wiring, I use a different controller : the Romeo V2 all in one controller, which is a ATMega32U4. Could you please tell me how do you calibrate with the Arduino software.
plblj
 
Posts: 6
Joined: Mon Jun 17, 2013 8:54 am

I might be missing something, but wouldn't you do this?

Code: Select all

if(color.red <=678 && color.green <=681 && color.blue  <= 704){
      digitalWrite(motor_pin1,LOW);
      digitalWrite(motor_pin2,HIGH);
      digitalWrite(motor_pin3,HIGH);
      digitalWrite(motor_pin4,LOW);
}





plblj wrote:Thank you very much for your connection guide, finally got it working. I want to control the motors with color, could you please help me write a code that controls the motor to run by the 4 RGBC data? The motors will run if the red value is <=678 the green value is <=681 the blue value <= 704

Red Green Blue Clear
Data: 678 681 704 1013

and my code for the motor is
Code: Select all
      digitalWrite(motor_pin1,LOW);
      digitalWrite(motor_pin2,HIGH);
      digitalWrite(motor_pin3,HIGH);
      digitalWrite(motor_pin4,LOW);

How can I write the codes?


ameyer wrote:Where do you see the code: sdaPin = A4 ?

It is possible this code isn't compatible with this board. (I dont know either way)

plblj wrote:Thank you very much for showing the correct connection, I connected my SDA and SCL to those pins on my Romeo, but there is still noting on the serial monitor, do I also need to replace the code
int sdaPin = A4;
int sclPin = A5;
with some other codes?

ameyer wrote:With the Romeo V2 the I2C pins are not on Analog4/Analog5
Look here: http://www.dfrobot.com/wiki/images/f/fc/RomeoV2R3.png

Connect SDA of the sensor to SDA on the Romeo, and SCL of the sensor to SCL on the Romeo.

plblj wrote:Hi ameyer, Thanks for the library and tutorial! I followed the wiring and uploaded the codes, got a white object in front of the sensor, but my serial monitor showed nothing, I don't know how to get it calibrated. I wonder if it is because of my wiring, I use a different controller : the Romeo V2 all in one controller, which is a ATMega32U4. Could you please tell me how do you calibrate with the Arduino software.
ameyer
Founder
 
Posts: 2981
Joined: Thu Jan 21, 2010 11:59 pm
Location: The Bay Area

Thanks, the code worked well, but when I added a range to it
Code: Select all
if(478<=color.red <=678 && 481<=color.green <=681 && 504<=color.blue  <= 704){
      digitalWrite(motor_pin1,LOW);
      digitalWrite(motor_pin2,HIGH);
      digitalWrite(motor_pin3,HIGH);
      digitalWrite(motor_pin4,LOW);
}else{
       digitalWrite(motor_pin1,LOW);
       digitalWrite(motor_pin2,LOW);
       digitalWrite(motor_pin3,LOW);
       digitalWrite(motor_pin4,LOW);
}

The motors don't stop when the value is out of the range, is there something wrong with the formula I use, or is it because Arduino software does not do a range? Thank you for your patience.

ameyer wrote:I might be missing something, but wouldn't you do this?

Code: Select all

if(color.red <=678 && color.green <=681 && color.blue  <= 704){
      digitalWrite(motor_pin1,LOW);
      digitalWrite(motor_pin2,HIGH);
      digitalWrite(motor_pin3,HIGH);
      digitalWrite(motor_pin4,LOW);
}





plblj wrote:Thank you very much for your connection guide, finally got it working. I want to control the motors with color, could you please help me write a code that controls the motor to run by the 4 RGBC data? The motors will run if the red value is <=678 the green value is <=681 the blue value <= 704

Red Green Blue Clear
Data: 678 681 704 1013

and my code for the motor is
Code: Select all
      digitalWrite(motor_pin1,LOW);
      digitalWrite(motor_pin2,HIGH);
      digitalWrite(motor_pin3,HIGH);
      digitalWrite(motor_pin4,LOW);

How can I write the codes?


ameyer wrote:Where do you see the code: sdaPin = A4 ?

It is possible this code isn't compatible with this board. (I dont know either way)

plblj wrote:Thank you very much for showing the correct connection, I connected my SDA and SCL to those pins on my Romeo, but there is still noting on the serial monitor, do I also need to replace the code
int sdaPin = A4;
int sclPin = A5;
with some other codes?

ameyer wrote:With the Romeo V2 the I2C pins are not on Analog4/Analog5
Look here: http://www.dfrobot.com/wiki/images/f/fc/RomeoV2R3.png

Connect SDA of the sensor to SDA on the Romeo, and SCL of the sensor to SCL on the Romeo.

plblj wrote:Hi ameyer, Thanks for the library and tutorial! I followed the wiring and uploaded the codes, got a white object in front of the sensor, but my serial monitor showed nothing, I don't know how to get it calibrated. I wonder if it is because of my wiring, I use a different controller : the Romeo V2 all in one controller, which is a ATMega32U4. Could you please tell me how do you calibrate with the Arduino software.
plblj
 
Posts: 6
Joined: Mon Jun 17, 2013 8:54 am

Yeah, you have to do it like this (Might need to be all on one line - Just did it like this so you can read it)


Code: Select all
if(
color.red => 478
&&
color.red <=678
&&

color.green => 481
&&
color.green <=681
&&

color.blue => 504
&&
color.blue <=704
){





plblj wrote:Thanks, the code worked well, but when I added a range to it
Code: Select all
if(478<=color.red <=678 && 481<=color.green <=681 && 504<=color.blue  <= 704){
      digitalWrite(motor_pin1,LOW);
      digitalWrite(motor_pin2,HIGH);
      digitalWrite(motor_pin3,HIGH);
      digitalWrite(motor_pin4,LOW);
}else{
       digitalWrite(motor_pin1,LOW);
       digitalWrite(motor_pin2,LOW);
       digitalWrite(motor_pin3,LOW);
       digitalWrite(motor_pin4,LOW);
}

The motors don't stop when the value is out of the range, is there something wrong with the formula I use, or is it because Arduino software does not do a range? Thank you for your patience.

ameyer wrote:I might be missing something, but wouldn't you do this?

Code: Select all

if(color.red <=678 && color.green <=681 && color.blue  <= 704){
      digitalWrite(motor_pin1,LOW);
      digitalWrite(motor_pin2,HIGH);
      digitalWrite(motor_pin3,HIGH);
      digitalWrite(motor_pin4,LOW);
}





plblj wrote:Thank you very much for your connection guide, finally got it working. I want to control the motors with color, could you please help me write a code that controls the motor to run by the 4 RGBC data? The motors will run if the red value is <=678 the green value is <=681 the blue value <= 704

Red Green Blue Clear
Data: 678 681 704 1013

and my code for the motor is
Code: Select all
      digitalWrite(motor_pin1,LOW);
      digitalWrite(motor_pin2,HIGH);
      digitalWrite(motor_pin3,HIGH);
      digitalWrite(motor_pin4,LOW);

How can I write the codes?


ameyer wrote:Where do you see the code: sdaPin = A4 ?

It is possible this code isn't compatible with this board. (I dont know either way)

plblj wrote:Thank you very much for showing the correct connection, I connected my SDA and SCL to those pins on my Romeo, but there is still noting on the serial monitor, do I also need to replace the code
int sdaPin = A4;
int sclPin = A5;
with some other codes?

ameyer wrote:With the Romeo V2 the I2C pins are not on Analog4/Analog5
Look here: http://www.dfrobot.com/wiki/images/f/fc/RomeoV2R3.png

Connect SDA of the sensor to SDA on the Romeo, and SCL of the sensor to SCL on the Romeo.

plblj wrote:Hi ameyer, Thanks for the library and tutorial! I followed the wiring and uploaded the codes, got a white object in front of the sensor, but my serial monitor showed nothing, I don't know how to get it calibrated. I wonder if it is because of my wiring, I use a different controller : the Romeo V2 all in one controller, which is a ATMega32U4. Could you please tell me how do you calibrate with the Arduino software.
ameyer
Founder
 
Posts: 2981
Joined: Thu Jan 21, 2010 11:59 pm
Location: The Bay Area

Hi, I was very happy when I found this code. I don't have a lot of experience with the Auduino board and I need to detect color from an LED ( light source) from a distance on my spectrometer. Iv'e only begun to learn Auduino coding, and I am running short on time. In order to do this I need to turn the on board LED off. Would I just omit lines 14 and 15 from adjd_s311.ino?
Kfai0131
 
Posts: 5
Joined: Tue Jul 09, 2013 10:26 am

Yes

Kfai0131 wrote:Hi, I was very happy when I found this code. I don't have a lot of experience with the Auduino board and I need to detect color from an LED ( light source) from a distance on my spectrometer. Iv'e only begun to learn Auduino coding, and I am running short on time. In order to do this I need to turn the on board LED off. Would I just omit lines 14 and 15 from adjd_s311.ino?
ameyer
Founder
 
Posts: 2981
Joined: Thu Jan 21, 2010 11:59 pm
Location: The Bay Area

Thanks a lot man, with your help on the sensor, my project is almost done, and I have one more question. I want the motor to rotate for 1 second, so I use a delay function; but then the sensor feeds back one value per second, which is not effective, the sensor is supposed to give feedback every 0.1 second, how can I modify the codes so that the motors will delay 1 second and the sensor will delay 0.1 second?

the codes are:
Code: Select all
{
   
  if(color.red <500 && color.red >100 && color.green <=770 && color.green >100 && color.blue  <= 570 && color.blue >100 && color.clear <=950 && color.clear > 100){
      analogWrite (E1,80);
      digitalWrite(M1,LOW);   
      analogWrite (E2,80);   
      digitalWrite(M2,HIGH);
      delay(1000);
     
       analogWrite (E1,80);
      digitalWrite(M1,HIGH);   
      analogWrite (E2,80);   
      digitalWrite(M2,LOW);
        delay(1000); }


ameyer wrote:Yeah, you have to do it like this (Might need to be all on one line - Just did it like this so you can read it)


Code: Select all
if(
color.red => 478
&&
color.red <=678
&&

color.green => 481
&&
color.green <=681
&&

color.blue => 504
&&
color.blue <=704
){





plblj wrote:Thanks, the code worked well, but when I added a range to it
Code: Select all
if(478<=color.red <=678 && 481<=color.green <=681 && 504<=color.blue  <= 704){
      digitalWrite(motor_pin1,LOW);
      digitalWrite(motor_pin2,HIGH);
      digitalWrite(motor_pin3,HIGH);
      digitalWrite(motor_pin4,LOW);
}else{
       digitalWrite(motor_pin1,LOW);
       digitalWrite(motor_pin2,LOW);
       digitalWrite(motor_pin3,LOW);
       digitalWrite(motor_pin4,LOW);
}

The motors don't stop when the value is out of the range, is there something wrong with the formula I use, or is it because Arduino software does not do a range? Thank you for your patience.

ameyer wrote:I might be missing something, but wouldn't you do this?

Code: Select all

if(color.red <=678 && color.green <=681 && color.blue  <= 704){
      digitalWrite(motor_pin1,LOW);
      digitalWrite(motor_pin2,HIGH);
      digitalWrite(motor_pin3,HIGH);
      digitalWrite(motor_pin4,LOW);
}





plblj wrote:Thank you very much for your connection guide, finally got it working. I want to control the motors with color, could you please help me write a code that controls the motor to run by the 4 RGBC data? The motors will run if the red value is <=678 the green value is <=681 the blue value <= 704

Red Green Blue Clear
Data: 678 681 704 1013

and my code for the motor is
Code: Select all
      digitalWrite(motor_pin1,LOW);
      digitalWrite(motor_pin2,HIGH);
      digitalWrite(motor_pin3,HIGH);
      digitalWrite(motor_pin4,LOW);

How can I write the codes?


ameyer wrote:Where do you see the code: sdaPin = A4 ?

It is possible this code isn't compatible with this board. (I dont know either way)

plblj wrote:Thank you very much for showing the correct connection, I connected my SDA and SCL to those pins on my Romeo, but there is still noting on the serial monitor, do I also need to replace the code
int sdaPin = A4;
int sclPin = A5;
with some other codes?

ameyer wrote:With the Romeo V2 the I2C pins are not on Analog4/Analog5
Look here: http://www.dfrobot.com/wiki/images/f/fc/RomeoV2R3.png

Connect SDA of the sensor to SDA on the Romeo, and SCL of the sensor to SCL on the Romeo.

plblj wrote:Hi ameyer, Thanks for the library and tutorial! I followed the wiring and uploaded the codes, got a white object in front of the sensor, but my serial monitor showed nothing, I don't know how to get it calibrated. I wonder if it is because of my wiring, I use a different controller : the Romeo V2 all in one controller, which is a ATMega32U4. Could you please tell me how do you calibrate with the Arduino software.
plblj
 
Posts: 6
Joined: Mon Jun 17, 2013 8:54 am

You need to time it.
This should get you 99% there, but also explain it better than I would: http://arduino.cc/en/Tutorial/BlinkWithoutDelay


plblj wrote:Thanks a lot man, with your help on the sensor, my project is almost done, and I have one more question. I want the motor to rotate for 1 second, so I use a delay function; but then the sensor feeds back one value per second, which is not effective, the sensor is supposed to give feedback every 0.1 second, how can I modify the codes so that the motors will delay 1 second and the sensor will delay 0.1 second?

the codes are:
Code: Select all
{
   
  if(color.red <500 && color.red >100 && color.green <=770 && color.green >100 && color.blue  <= 570 && color.blue >100 && color.clear <=950 && color.clear > 100){
      analogWrite (E1,80);
      digitalWrite(M1,LOW);   
      analogWrite (E2,80);   
      digitalWrite(M2,HIGH);
      delay(1000);
     
       analogWrite (E1,80);
      digitalWrite(M1,HIGH);   
      analogWrite (E2,80);   
      digitalWrite(M2,LOW);
        delay(1000); }


ameyer wrote:Yeah, you have to do it like this (Might need to be all on one line - Just did it like this so you can read it)


Code: Select all
if(
color.red => 478
&&
color.red <=678
&&

color.green => 481
&&
color.green <=681
&&

color.blue => 504
&&
color.blue <=704
){





plblj wrote:Thanks, the code worked well, but when I added a range to it
Code: Select all
if(478<=color.red <=678 && 481<=color.green <=681 && 504<=color.blue  <= 704){
      digitalWrite(motor_pin1,LOW);
      digitalWrite(motor_pin2,HIGH);
      digitalWrite(motor_pin3,HIGH);
      digitalWrite(motor_pin4,LOW);
}else{
       digitalWrite(motor_pin1,LOW);
       digitalWrite(motor_pin2,LOW);
       digitalWrite(motor_pin3,LOW);
       digitalWrite(motor_pin4,LOW);
}

The motors don't stop when the value is out of the range, is there something wrong with the formula I use, or is it because Arduino software does not do a range? Thank you for your patience.

ameyer wrote:I might be missing something, but wouldn't you do this?

Code: Select all

if(color.red <=678 && color.green <=681 && color.blue  <= 704){
      digitalWrite(motor_pin1,LOW);
      digitalWrite(motor_pin2,HIGH);
      digitalWrite(motor_pin3,HIGH);
      digitalWrite(motor_pin4,LOW);
}





plblj wrote:Thank you very much for your connection guide, finally got it working. I want to control the motors with color, could you please help me write a code that controls the motor to run by the 4 RGBC data? The motors will run if the red value is <=678 the green value is <=681 the blue value <= 704

Red Green Blue Clear
Data: 678 681 704 1013

and my code for the motor is
Code: Select all
      digitalWrite(motor_pin1,LOW);
      digitalWrite(motor_pin2,HIGH);
      digitalWrite(motor_pin3,HIGH);
      digitalWrite(motor_pin4,LOW);

How can I write the codes?


ameyer wrote:Where do you see the code: sdaPin = A4 ?

It is possible this code isn't compatible with this board. (I dont know either way)

plblj wrote:Thank you very much for showing the correct connection, I connected my SDA and SCL to those pins on my Romeo, but there is still noting on the serial monitor, do I also need to replace the code
int sdaPin = A4;
int sclPin = A5;
with some other codes?

ameyer wrote:With the Romeo V2 the I2C pins are not on Analog4/Analog5
Look here: http://www.dfrobot.com/wiki/images/f/fc/RomeoV2R3.png

Connect SDA of the sensor to SDA on the Romeo, and SCL of the sensor to SCL on the Romeo.

plblj wrote:Hi ameyer, Thanks for the library and tutorial! I followed the wiring and uploaded the codes, got a white object in front of the sensor, but my serial monitor showed nothing, I don't know how to get it calibrated. I wonder if it is because of my wiring, I use a different controller : the Romeo V2 all in one controller, which is a ATMega32U4. Could you please tell me how do you calibrate with the Arduino software.
ameyer
Founder
 
Posts: 2981
Joined: Thu Jan 21, 2010 11:59 pm
Location: The Bay Area

I got the sketch running, I tried calibrating the on board LED (we decided to leave it running for now) and all I got on the COM3 is "init". I wasn't sure if that meant it was calibrated so I placed a red piece of paper about the same proximity from the color sensor as I did for the white paper/calibration, and got nothing. How do I get the readings off of COM3 that I need (the basic colors)?
-Sorry for all the questions, my professor is out of the country for a few weeks so I can not ask him.
Kfai0131
 
Posts: 5
Joined: Tue Jul 09, 2013 10:26 am

First of all, does it work 100% following the article exactly, LED and all?
If it does, just take out the pin for the LED, and then make sure you start the sketch with it aimed at a white screen so it will calibrate. Then you are done.

If it doesn't work hooked up as shown, there is another issue. Check the wires 3 times at least. Disconnect everything and hook it up again. Im guessing the I2C connections are not connected correctly. Or... your not using an arduino UNO. (other arduinos do not use A4/A5 for the I2C connection)


Kfai0131 wrote:I got the sketch running, I tried calibrating the on board LED (we decided to leave it running for now) and all I got on the COM3 is "init". I wasn't sure if that meant it was calibrated so I placed a red piece of paper about the same proximity from the color sensor as I did for the white paper/calibration, and got nothing. How do I get the readings off of COM3 that I need (the basic colors)?
-Sorry for all the questions, my professor is out of the country for a few weeks so I can not ask him.
ameyer
Founder
 
Posts: 2981
Joined: Thu Jan 21, 2010 11:59 pm
Location: The Bay Area

Hi, ameyer,
I have learned and wrote the codes for all the functions I need, but unexpectedly the system would stuck when it works for a while. As is shown in the video http://www.youtube.com/watch?v=cLvDhvAU6cY, the robot is supposed to turn right and then turn left when it is searching for the color, initially it is working well, but after a while the sensor does not respond and the serial monitor stops printing any sensor value. I guess it can be a problem of my codes, but it might also be a compatible problem of my controller. Can you help me figure out the problem? thanks My codes are


Code: Select all
#include <ADJDS311.h>
#include <Wire.h>

int sensorLed_pin = 4; //LED on the ADJDS-311
ADJDS311 colorSensor(sensorLed_pin);

//if using an RGB LED (Needs PWM Pins)
int dist;
const int E1 = 5;     //M1 Speed Control
const int E2 = 6;     //M2 Speed Control
const int M1 = 4;    //M1 Direction Control
const int M2 = 7;    //M1 Direction Control

int M1State = LOW;
int M2State = HIGH;
long previousMillis = 0;       
long interval = 1000;   
void setup(){
  pinMode(E1,OUTPUT);            // set the motor pins as output
  pinMode(E2,OUTPUT);
  pinMode(M1,OUTPUT);
  pinMode(M2,OUTPUT);

  Serial.begin(9600);

  colorSensor.init();

  //Calibrate white
  //Need to hold white card in front (1-3mm) of it to calibrate from
  colorSensor.calibrate();
}

void loop()
{

  RGBC color = colorSensor.read();
  delay(100); //read the color
  dist = analogRead(1);

  Serial.print(color.red);
  Serial.print(" | ");
  Serial.print(color.green);
  Serial.print(" | ");
  Serial.print(color.blue);
  Serial.print(" | ");
  Serial.println(color.clear);
  if(dist < 426)
  {
    if(color.red = 1023 && color.green >=270 && color.green <= 420 && color.blue >= 110 && color.blue <= 190 && color.clear>=860) {
      analogWrite (E1,100);
      digitalWrite(M1,LOW);   
      analogWrite (E2,100);   
      digitalWrite(M2,LOW);
      delay(100);


    }
    else if (color.red <=100 && color.green <=100 && color.blue<=100 && color.clear<=100) {
      digitalWrite(E1,LOW);   
      digitalWrite(E2,LOW);   
      delay(100);

    }
    else 
    {
      unsigned long currentMillis = millis();

      if(currentMillis - previousMillis > interval) {
        // save the last time you blinked the LED
        previousMillis = currentMillis;   

        // if the LED is off turn it on and vice-versa:
        if (M1State == LOW)
          M1State = HIGH;
        else
          M1State = LOW;
        if (M2State == LOW)
          M2State = HIGH;
        else
          M2State = LOW;
        analogWrite (E1,80);
        digitalWrite(M1,M1State);   
        analogWrite (E2,80);   
        digitalWrite(M2,M2State);
        delay(100);
      }

    } 
  }
  else if (dist > 426)
  {
    digitalWrite(E1,LOW);   
    digitalWrite(E2,LOW);   
    delay(100);
  }   
}

plblj
 
Posts: 6
Joined: Mon Jun 17, 2013 8:54 am

If stops working, i could be the I2C connection (A4/A5) become unstable - Maybe a lose wore, or dirty power supply.
It could also be a memory leak. the arduino will crash if it runs out of ram.
It could also be a power issue, though usually if you draw too much power from the arduino, it will restart over and over.


plblj wrote:Hi, ameyer,
I have learned and wrote the codes for all the functions I need, but unexpectedly the system would stuck when it works for a while. As is shown in the video http://www.youtube.com/watch?v=cLvDhvAU6cY, the robot is supposed to turn right and then turn left when it is searching for the color, initially it is working well, but after a while the sensor does not respond and the serial monitor stops printing any sensor value. I guess it can be a problem of my codes, but it might also be a compatible problem of my controller. Can you help me figure out the problem? thanks My codes are


Code: Select all
#include <ADJDS311.h>
#include <Wire.h>

int sensorLed_pin = 4; //LED on the ADJDS-311
ADJDS311 colorSensor(sensorLed_pin);

//if using an RGB LED (Needs PWM Pins)
int dist;
const int E1 = 5;     //M1 Speed Control
const int E2 = 6;     //M2 Speed Control
const int M1 = 4;    //M1 Direction Control
const int M2 = 7;    //M1 Direction Control

int M1State = LOW;
int M2State = HIGH;
long previousMillis = 0;       
long interval = 1000;   
void setup(){
  pinMode(E1,OUTPUT);            // set the motor pins as output
  pinMode(E2,OUTPUT);
  pinMode(M1,OUTPUT);
  pinMode(M2,OUTPUT);

  Serial.begin(9600);

  colorSensor.init();

  //Calibrate white
  //Need to hold white card in front (1-3mm) of it to calibrate from
  colorSensor.calibrate();
}

void loop()
{

  RGBC color = colorSensor.read();
  delay(100); //read the color
  dist = analogRead(1);

  Serial.print(color.red);
  Serial.print(" | ");
  Serial.print(color.green);
  Serial.print(" | ");
  Serial.print(color.blue);
  Serial.print(" | ");
  Serial.println(color.clear);
  if(dist < 426)
  {
    if(color.red = 1023 && color.green >=270 && color.green <= 420 && color.blue >= 110 && color.blue <= 190 && color.clear>=860) {
      analogWrite (E1,100);
      digitalWrite(M1,LOW);   
      analogWrite (E2,100);   
      digitalWrite(M2,LOW);
      delay(100);


    }
    else if (color.red <=100 && color.green <=100 && color.blue<=100 && color.clear<=100) {
      digitalWrite(E1,LOW);   
      digitalWrite(E2,LOW);   
      delay(100);

    }
    else 
    {
      unsigned long currentMillis = millis();

      if(currentMillis - previousMillis > interval) {
        // save the last time you blinked the LED
        previousMillis = currentMillis;   

        // if the LED is off turn it on and vice-versa:
        if (M1State == LOW)
          M1State = HIGH;
        else
          M1State = LOW;
        if (M2State == LOW)
          M2State = HIGH;
        else
          M2State = LOW;
        analogWrite (E1,80);
        digitalWrite(M1,M1State);   
        analogWrite (E2,80);   
        digitalWrite(M2,M2State);
        delay(100);
      }

    } 
  }
  else if (dist > 426)
  {
    digitalWrite(E1,LOW);   
    digitalWrite(E2,LOW);   
    delay(100);
  }   
}

ameyer
Founder
 
Posts: 2981
Joined: Thu Jan 21, 2010 11:59 pm
Location: The Bay Area

Previous

Return to Blog Posts

Who is online

Users browsing this forum: No registered users and 2 guests