Simple servo / pump control

Hi, im new to to arduino and only worked with arduino in combination with processing.

Now i want to control a servo and later on a pump with my arduino Duemilanove. The servo should be put on or off based on a photoresistor. input. I followed some basic instructions and got something working but it does not respond well, although the servo spins harder wehen i fully cover the photoresistor.

Can someone help me? thanks

#include <Servo.h>

Servo myservo; // create servo object to control a servo

int lightPin = 0; // analog pin used to connect the potentiometer
//int val; // variable to read the value from the analog pin
int motorPin = 9;

void setup()
myservo.attach(9); // attaches the servo on pin 9 to the servo object

void loop()

int lightLevel = analogRead(lightPin);
lightLevel = map(lightLevel, 0, 900, 0, 255);
//adjust the value 0 to 900 to
//span 0 to 255
lightLevel = constrain(lightLevel, 0, 255);
//make sure the
//value is betwween
//0 and 255

int threshold = 280;
if(analogRead(lightLevel) > threshold){
digitalWrite(motorPin, HIGH);
digitalWrite(motorPin, LOW);}


You've declared and attached a servo to pin 9, yet you're using "digitalWrite"s to that pin. That's not sensible.

Hm, with digitalWrite it works, the photoresistor controlls the servo. When i make it an analogWrite is does nothing at all.

But it only works when i fully cover the photoresist. In another test i did the photoresist worked more responsive, like when i cast a shadow on it with my hand from about 30Cm above.

Thx for the reply

with digitalWrite it works...When i make it an analogWrite is does nothing at all


Yes i think its strange to, but its working so far. I think i will start over from scratch in stead of pulling things together from the Experimenters guide :D

Tips for controlling pumps?

Appropriate relay, a transistor, a resistor, and a digital pin.

Tips for controlling pumps?

Make sure they’re laced up tightly? ;D