Help! How to have a photoresistor constantly read amount of light?

Hello,

Currently working on a “robot” project that programs a photoresistor to turn on a DC motor. We are using this code in conjunction with a servo motor where we use two for statements. When we isolate the photoresistor/DC motor code, it operates correctly. When we add the servo motor code, it stops working. We want the photoresistor to read for light or lack thereof after the first for statement, and continue to read until after the second for statement/and its delay. We have moved the photoresistor code all over the place, but still wont work. I have bolded the section of our code where we want the photoresistor to be reading. Where should the photoresistor then go? Please help!
Here is our code:

#include <Servo.h> // include the Servo library

//variable
int value;

//constants
const int pResistor = A0;
const int DCMotor = 13;

const int sensorMin = 600;
const int sensorMax = 900;

const long delayTime = 1000;

Servo tiltservo; //create the servo object
int pos2=0; //variable to store the servo position
void setup()

{
pinMode(DCMotor, OUTPUT);
pinMode (pResistor, INPUT);
Serial.begin(9600);

tiltservo.attach(9); // attaches the servo on pin 9 to a servo object
}

void loop()
{
tiltservo.write(0);

for (pos2=0;pos2<=180;pos2+=3) { // goes from 0 degrees to 180 deg in steps of 1 deg
tiltservo.write(pos2); // tell servo to go to position in variable ‘pos’
delay(15); // waits 15 ms for the servo to reach the position

}

delay(4000);
for (pos2=180;pos2>=0;pos2-=3) { // goes from 180 degrees to 0 degrees
** tiltservo.write(pos2); // tell servo to go to position in variable ‘pos’**
** delay(15); // waits 15ms for the servo to reach the position**
** value = analogRead(pResistor);**
** Serial.println(value);**
** value = map (value, sensorMin, sensorMax, 600, 900);**
** value = constrain(value, 600, 900);**
}

value = analogRead(pResistor);
if (value <900){
digitalWrite(DCMotor, HIGH); //Turn DC Motor on]
delay(3000);
digitalWrite(DCMotor, LOW);
}
else{
digitalWrite(DCMotor, LOW); //Turn DC Motor off
}
delay(7000);

}

Please use code tags to post code ("</>" button).

Get rid of all the delays – they are completely unnecessary. Study the “blink without delay” example that comes with the Arduino IDE to see how to replace delays with timed actions. Also see “how to do several things at a time”.

Blocking code has a job to do that takes some time and insists on completing the entire task before returning control to the loop so other things can happen. delay and the use of for loops to go all the way through the motion of the servo are examples of this.

Non-blocking code takes one little step at a time and lets all the other things in the program take their one little steps. It relies on the loop function to do the looping and being called over and over and over again, each time checking to see if it is time to make a move and if so making it. Take a look at the Several Things at Once thread and of course the Blink Without Delay example for some inspiration on writing non-blocking code.

Hi,
Welcome to the forum.

Please read the first post in any forum entitled how to use this forum.
http://forum.arduino.cc/index.php/topic,148850.0.html then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.

You are using the delay(), statement, what it does is STOPS ALL PROCESSING for the delay time, in your case 4Seconds and 7Seconds.

Look at the IDE, Examples and locate "Blink without Delay" and see how it uses millis to delay without stopping the code

Tom.. :slight_smile:

Hi,
Can you tell us STEP by step, what your code is supposed to do.

What do you want the photoresistor to control?
What do you want the servo to do?
What do you want the motor to do?

Thanks.. Tom.. :slight_smile: