Controlling servo motor using photoresistor

Hello i am trying to create an electronic blinds project with the arduino uno board. I want the photo resister to control the rotation of the servo motor. When its dark for instance i want the servo motor to be at 0 degrees, when sunlight is shining i want the servo to move to 90 degrees and when there is to much light that will blind people i want the motor to turn to 135degrees. i have created a circuit with a servo motor, photo resister, 220ohm resister and jumper wires.

The code that i have used just allows the servo motor to move when its dark and move back when its light. i was wondering if anybody could help me with how to set the angles etc.

//The code

#include <Servo.h>

//declare Servo Motor

Servo servomotor;

const int photoresistor = 1;

const int motor = 13;

int photoresistorValue = 0;

int outputValue = 0;

float nice = 0.09;
float myValue;

void setup() {

Serial.begin(9600);

servomotor.attach(motor);

}

void loop() {

photoresistorValue = analogRead(photoresistor);

myValue = float(map(photoresistorValue, 0, 100, 0, 180));

outputValue += int(myValue - outputValue) *nice;

Serial.print("sensor = ");

Serial.print(photoresistorValue);

Serial.print("\t output = ");

Serial.println(outputValue);

servomotor.write(outputValue);

delay (10);

}

Any help would be much appreciated thank you .

Please read How to use this Forum to learn how to use code tags so your code looks like this. And lose all the unnecessary blank lines.

I suspect the map() function isn't really suitable.

I would try to figure out a few ranges of LDR values and associate them with specific servo angles. For example (and my numbers are probably unrealistic) from 0 to 200 use 10deg, 201-400 use 40deg etc.

The trick here is a series of IF statements that start with the highest value.

if (ldr > 900) {
  angle = 160;
}
if (ldr > 700) {
  angle = 120;
}

...R

thanks for the help. i am pretty new to all of this. where about in my code would these IF statements go and would i have to change my code at all ? i can’t figure out how to change the motor to the angles also i don’t know how to do it so that it is capable to turn the bar that controls the blinds.

I think the best thing from your point of view is to have a go, and if you need more help post your new sketch here.

...R

I have now created the motor and it turns in the right directions but when the light resister detects light the motor shakes and emits a sound but when its dark it turns and does not make the noise when in the position i want it in.

my code is below …

// My code

#include <Servo.h> // includes the servo libary

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

const int ldr = 1; // analog pin used to connect the ldr
int val = 0; // variable to read the value from the analog pin

void setup()
{
myservo.attach(13); // attaches the servo on pin 9 of the arduino board
}

void loop()
{
val = analogRead(ldr); // reads the value of the ldr (value between 0 and 1023)
val = map(val, 0, 179, 0, 1023); // scale it to use it with the servo (value between 0 and 180)
Serial.println(val); // controlling the servo depending on the light value
if (ldr > 200) {
val = 90;
}
if (ldr > 800) {
val = 180;
}
myservo.write(val); // moves the servo to the angle specified
delay(15); // gives servo time to move to position
}

Why are you checking the value of "ldr" which holds the number of an arduino pin when the value you want to check is in "val"?

Why have you mapped the reading from 0-1023 to 0-179 and then check to see if it exceeds 800?

A great way to find errors such as these is to go through your code line by line recording the values with paper and pencil as though your brain is the Arduino.

Don't connect your LDR (or anything else) to pins 0 or 1 as they are used for serial communications.

...R

Could you give me a circuit diagram that can helping me make this project?..