servo motor and photoresistor

Hello I am very new to arudino and coding so don't really know anything. I am trying to have my servo constantly be moving if the photoresistor senses light and not move if there is no light.
I found a tutorial to start off with which works. (I connected mine the same way but used items from the elegoo super starter kit)

I tried to modify the coding so the servo is constantly moving if it senses light by using coding from the 'Sweep' build in example but it did not work.

//include the servo library

#include<Servo.h>

//create a servo object called servo1 
Servo servo1;
int pos = 0;    // variable to store the servo position

void setup() { 
  // put your setup code here, to run once:

// set the servo pin, pin 9, as an servo output pin 
servo1.attach(9); 
}

void loop() { 
  // put your main code here, to run repeatedly:
  //Read the light value from photoresistor 
  int lightValue = analogRead(A0);
 
 for (pos = 0; pos <= 180; pos += 1) { // goes from 0 degrees to 180 degrees
    // in steps of 1 degree
    servo1.write(pos);              // tell servo to go to position in variable 'pos'
    delay(15);                       // waits 15ms for the servo to reach the position
  }
  for (pos = 180; pos >= 0; pos -= 1) { // goes from 180 degrees to 0 degrees
    servo1.write(pos);              // tell servo to go to position in variable 'pos'
    delay(15);                       // waits 15ms for the servo to reach the position
  }


// map the light readings to the angle possible by the servo motor 
lightValue = map (lightValue, 0, 500, 0, 180);

// control the servo motor based on the light value read, adjust linearly by angles 
servo1.write (lightValue); 
}

I don't know how to code it to do what I want or if it's possible.
Any help would be great!

The first thing to do is to experiment with the photo resistor code alone, and determine the range of numbers that you get under different lighting conditions.

Then you need to decide what to do with that range of numbers, for example, how to map that range to servo positions 0 to 180 (or less).

It does not make sense to leave the "sweep" code in place. An outline for the loop function might be the following:

void loop() {
  int lightValue, pos;

  //Read the light value from photoresistor
  lightValue = analogRead(A0);
 
 // map the RANGE of light readings to the angle RANGE of the servo
  pos = map (lightValue, 200, 750, 10, 160);

// control the servo motor based on the light value read, adjust linearly by angles
servo1.write (pos);

delay(300); //wait a bit, allow the servo to reach position
}

Note: the map function does not check for out of range values. Use constrain() for that, to avoid damaging the servo. Finally, don't try to use the Arduino 5V output to power the servo, as that can damage the Arduino. Use a 4xAA battery pack to power the servo instead, and connect all the grounds.

Thanks it seems to work but the servo only moves if there is a change in light. I should have been more specific but I wanted the servo to move constantly back and fourth when the light is on and the light source states the same if that makes sense. Sorry that might be a bad explanation.
I decided to use this code which seems to work

#include <Servo.h>

Servo myservo;  // create servo object to control a servo
// twelve servo objects can be created on most boards

int pos = 0;    // variable to store the servo position
void setup() {
  myservo.attach(9);  // attaches the servo on pin 9 to the servo object
}

void loop() {
 if(analogRead(A0)>200) {     //
  for (pos = 0; pos <= 180; pos += 1) { // goes from 0 degrees to 180 degrees
    // in steps of 1 degree
    myservo.write(pos);              // tell servo to go to position in variable 'pos'
    delay(10);                       // waits 15ms for the servo to reach the position
  }
  for (pos = 180; pos >= 0; pos -= 1) { // goes from 180 degrees to 0 degrees
    myservo.write(pos);              // tell servo to go to position in variable 'pos'
    delay(10);                       // waits 15ms for the servo to reach the position
  }  }
}

Thanks I will use a AA battery pack instead

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.