Optical sensor moving based on color

We are trying to use the new optical sensors to determine a motor’s direction. In the code, everything seems fine, however nothing comes of it. Any suggestions of how to fix this? code attached below:

while(1){

O1.setLightPower(100,percent);

if(O1.isNearObject()){
if(O1.hue() == 230){
M1.spin(forward, 200,rpm);
}
if(O1.hue() == 350){
M1.spin(reverse,200,rpm);
}
else{
M1.stop();
}
}

}

You are looking for an exact hue. Your are probably better off looking for a range of hues. So rather than 230, look for the range of hues between 220 and 240

9 Likes

Are you able to elaborate on how to include a range of numbers rather than just the one number then?

The == checks for equality. What do you think you would use to check for an inequality? The && operator is the “Logical And” operations (e.g. true && true evaluates to true while false && true, true && false, and false && false evaluates to false). How can you use this information to create an if statement that checks for values in a range?

4 Likes