Controlling qtr-rc3 and qtr rc1 without library w/ Arduino

/*Hello I am a High School Shop teacher
I am using an arduino mega 2560 an LED and a qtr rc sensor to turn the light on and off when a white and black paper goes moves in front of the sensor. I checked the mega board to make sure the pins are working correctly prior to posting this comment
I have the sensor at pin 2 and the led at pin 13
I am writing my own code to control the qtr rc1 with the following code but I am having some issues

The light does not turn off when the paper is moving in front of the sensor even though on the serial monitor the values are adjusting from 300 when the black paper is in front of the sensor. WHEN THE White paper is in front of the sensor the serial monitor values drop below 50 but the light does not go off I am not ot sure what is wrong with my code. Any help or suggestions are supper appreciated so I can teach these sensors without the qtr library.

my question is do I have the Loop and the Setup oraganized correctly to alternate the


[code]const int D13ledPin = 13;
int sensorIn = 2;

void setup() {

long RCTime(int sensorIn){
long duration = 0;
pinMode(sensorIn, OUTPUT); // Make pin OUTPUT
digitalWrite(sensorIn, HIGH); // Pin HIGH (discharge capacitor)
delay(3); // Wait 3ms
pinMode(sensorIn, INPUT); // Make pin INPUT
digitalWrite(sensorIn, LOW); // Turn off internal pullups
while(digitalRead(sensorIn)){ // Wait for pin to go LOW
return duration;

void loop() {
Serial.println(RCTime(2)); // Connect to pin 2, display results
delay(5); // Wait 3 ms
if (digitalRead(sensorIn)==LOW){
digitalWrite(D13ledPin, HIGH);
} else {
digitalWrite(D13ledPin, LOW);



In your code, it looks like the status of your LED is determined after the sensor discharges to the point it is reading LOW. This means the if statement controlling the LED is always true, resulting in your LED always being on. To have your LED respond to the sensor, the if statement should directly use the results of RCTime.

- Grant