Ardrino Lightsensor Blocking Code

I have a GY-31 Light sensor and I am trying to make a skittle sorting machine. There is a little wheel skittles can drop into then they are turn to 90 degrees read by the light sensor then dropped out the bottom. The problem is the code seems to get stuck after reading the light sensor (it still flows through but won't execute the moving of the servo). As soon as I take out the ReadSensor line the servo works like normal. Do I have to dispose of the Color Sensor some how?

#include <Servo.h>
#include <MD_TCS230.h>
#include <FreqCount.h>

#define  S2_OUT  2
#define  S3_OUT  3
MD_TCS230  CS(S2_OUT, S3_OUT);

Servo myservo;  // create servo object to control a servo 
                // a maximum of eight servo objects can be created 

void setup() 
{ 
  myservo.attach(11);  // attaches the servo on pin 9 to the servo object 
  Serial.begin(57600);
  Serial.println("[TCS230 Simple BLOCKING Example]");
  Serial.println("Move the sensor to different color to see the value");
  Serial.println("Note: These values are being read in without sensor calibration");
  Serial.println("and are likely to be far from reality");

  CS.begin();
  CS.setFilter(TCS230_RGB_X);
  CS.setFrequency(TCS230_FREQ_HI);
  CS.setSampling(100); 
} 

 void readSensor()
{
  uint32_t v;
  v = CS.readSingle();
  Serial.print("Simple value = ");
  Serial.println(CS.readSingle());

}

void loop() 
{ 
  Serial.println("Start");
  myservo.write(0);
  delay(1000);
  myservo.write(90);
  delay(1000);
 readSensor();
  myservo.write(180);
  delay(1000);
}

Sounds like the color sensor library is not working with your color sensor hardware. Could be the library is the wrong one for your hardware, the hardware is wired incorrectly, or the hardware is faulty.

but why would faulty hardware mean that I still get a correct reading, and it still hits the rotate Servo command, but it doesn't execute.

Sorry, I thought it was getting stuck in the library. Now I understand that it completes the library call but gets stuck in the subsequent Servo move.

I would now suspect that the light sensor (or frequency counter) library uses the same timer that the Servo library is trying to use. The only way to fix it is to change one of the conflicting libraries.

HI,
Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png or pdf?

I hope you are not using the 5V supply from the arduino to power the servo, it needs its own supply.
The sensor and servo are probably drawing too much current.

Tom..... :slight_smile:

Just had a quick look through the MD_TCS230 library. Checking out the examples there's a "blocking" and "non blocking" example.

For the NON blocking example your readSensor function should look like this (directly pasted from example)

void readSensor()
{
  static bool waiting;
  if (!waiting)
  {
    CS.read();
    waiting = true;
  }
  else
  {
    if (CS.available())
    {
      colorData rgb;
      CS.getRGB(&rgb);
      Serial.print("RGB [");
      Serial.print(rgb.value[TCS230_RGB_R]);
      Serial.print(",");
      Serial.print(rgb.value[TCS230_RGB_G]);
      Serial.print(",");
      Serial.print(rgb.value[TCS230_RGB_B]);
      Serial.println("]");
      waiting = false;
    }
  }
}

But no sooner do I paste the code into this post than I see a problem. I assume the variable "waiting" should be a global variable. Otherwise it is being read before it's set EVERY time that the function is called.