servo controlled by photocell-Ardino uno

hello all, I am trying to get a servo to operate via a photocell.
all attempts have failed any advice would be helpful.
I am using the arduino uno and I have a basic sketch but I think its not working.

here it is.

[code] #include <Servo.h>  
Servo myservo;  // create servo object to control a servo 
                // a maximum of eight servo objects can be created 
int pos = 0;    // variable to store the servo position 
int dest = 0;   // Servo destination depending on photocell reading
int spd = 50;   // how fast should the servo move? 50 is quier

int servoPin=0; //havent used this yet
int photocellPin = 0; // the cell and 10K pulldown are connected to a0
int photocellReading; // the analog reading from the analog resistor divider

int state = 0; //Keep track of state so we don't send signal to the servo without readon, better on battery life
int prevstate = 0;

int debug = 0; //Set this to 1 for serial debug output
void setup(void) {
//    Serial.begin(9600); //Comment out this line if you don't want debugging enabled
}


void loop(void) {

    photocellReading = analogRead(photocellPin); //Query photo cell
      debug and Serial.print("Light Reading :");
      debug and Serial.print(photocellReading); // the raw analog reading
      debug and Serial.print(" | Position: ");
      debug and Serial.print(pos);    
      debug and Serial.print(" | State: ");

    //Define the modes based on how bright it is, and set corresponding servo position
    if (photocellReading < 400) {
        debug and Serial.println("Night");
      dest=180;      
      state=1;
    } 
    else if (photocellReading < 600) {
        debug and Serial.println("Dusk");
      dest=135;      
      state=2;           
    } 
    else if (photocellReading < 950) {
        debug and Serial.println("Day");
      dest=85;
      state=3;
      
    } 
    else if (photocellReading < 1023) {
        debug and Serial.println("Very Bright Day");
      dest=20;
      state=4;
    } 
    else {
        debug and Serial.println("No reading");      
    }
    


    if (state != prevstate){ //IF the photocell reading is different from last sample then execute servo controls
          debug and Serial.println("State Change");      
        myservo.attach(9);                     //Connect to servo
        if (pos > dest){  // If the current position is great than the destination then we must subtract
              //for(pos = pos; pos>=dest; pos-=1)     // Change current position to desired position, one degree at a time.
              while (pos > dest)
              {                                
                  debug and Serial.print("Was :");
                  debug and Serial.print(pos);
                myservo.write(pos);              // tell servo to go to position in variable 'pos' 
                delay(spd);                 // waits 15ms for the servo to reach the position 
                pos--;
                  debug and Serial.print(" | Is :");
                  debug and Serial.println(pos);
              } 
            myservo.detach();                //Detach from Servo
        } 
        else {  // If the current position is great than the destination then we must add
            myservo.attach(9);
              //for(pos = pos; pos <= dest; pos+=1)     // goes from 180 degrees to 0 degrees 
              while (pos < dest)
              {                        
                  debug and Serial.print("Was :");
                  debug and Serial.print(pos);        
                myservo.write(pos);              // tell servo to go to position in variable 'pos' 
                delay(spd);                     // waits 15ms for the servo to reach the position 
                pos++;
                  debug and Serial.print(" | Is :");
                  debug and Serial.println(pos);
              } 
        }
        myservo.write(pos); // Doing a write out side of the loop because I had a feeling the last position value was being skipped. I think I'm wrong though
        delay(spd); 
        myservo.detach();
    }
    prevstate = state; //Remember state so we can compare it again next round
    delay(1000); //Optional delay, this probalby needs to be removed when IR receiver code get's added.    
}

[/code]

You need to use code tags when posting code, to stop the forum software from mangling it.

What's the code doing? How is this different to what you want it to do?

it is controlling wood slats in an attic. all it does is bring the servo to a 70 degree angle and it acts like the photocell is not even hooked up, im using a 10k pull down for the cell

A number of points.

Firstly, go back to your original submission and choose "modify", highlight your code section and click on the "hash" button above the edit window which prompts as "insert code". Then we will be able to read your code better.

Secondly, you are using "delay" calls in your code. I am pretty sure they will prevent the servo from working.

I haven't researched what "myservo.detach();" does, but it sounds like something which will also stop the servo from working.

And finally, you refer to a pull-down. It will work, but it is generally better practice to have input devices such as a a photocell, referenced to ground, particularly when they are external to the Arduino enclosure, and with a pull-up at the Arduino.

Do your debug prints show that the photocell is acting as you expect?

Why are you detaching the servo after moving it? Saving power perhaps?

You can dispense with the loops that move the servo one degree at a time; if state has changed, just use a single write to send the servo to its new position, with the attach & detach if necessary. Before the detach, delay long enough for the servo to do its move. Much simpler.

Does your debug output show that you are getting the light sensor input values you expected, recognising the lighting threshold conditions correctly, and attempting to drive the servo to the corresponding position?

Hi, can you post a copy of your circuit and the power supply, it will help if you have hardware problems that we can’t see.

A photograph of a hand drawn circuit or a CAD diagram would be fine, in jpg or png.

Thanks Tom… :slight_smile: Hope we can help.

Paul__B:
And finally, you refer to a pull-down. It will work, but it is generally better practice to have input devices such as a a photocell, referenced to ground, particularly when they are external to the Arduino enclosure, and with a pull-up at the Arduino.

Interesting, didn’t know that… why’s that Paul? Would that explain why the built-in resistors (as enabled by pinMode) are pull-ups not pull-downs?

JimboZA:

Paul__B: And finally, you refer to a pull-down. It will work, but it is generally better practice to have input devices such as a a photocell, referenced to ground, particularly when they are external to the Arduino enclosure, and with a pull-up at the Arduino.

Interesting, didn't know that.... why's that Paul? Would that explain why the built-in resistors (as enabled by pinMode) are pull-ups not pull-downs?

It certainly does.

Safety and convenience. Running supply voltages out to external areas creates a risk that they can be shorted out causing either damage to those external devices, or damage or at least malfunction to the control system. If a sensor is only referenced to ground, at worst a short will indicate as the sensor being actuated when it is not.

So that's where the notion of Active Low comes from?

That noise you heard was The Penny Dropping.... thanks Paul Underscore Underscore B. That's worth a Karma++.