if loop servo motor light sesnor !

Hi guys,
I'm designing a project where I'm using a light sensor with a servo motor that would control a window's blind in a room, but I want to control the motor to move with 90 deg, 180 deg and 270 deg based on the detection of the light sensor, so am trying to code a specific angle with a specific lux (brightness) value . for example when the light sensor detect 255 lux then I want the motor to move 90 deg and when the sensor detect let say 680 lux then i want the motor to move 180 deg but all I know is how to set the motor to different angle with delay like this loop

myservo.write(90)
delay(1000)
myservo.write(180)
delay(1000)

any help will be appreciated
Thanks

So are you asking for code for your light sensor? I guess it would depend on what sensor you have..

I got it to work :slight_smile: but my problem is that the motor doesnt stop when it gets to the desird angle insteas it rotates to the other direction , I want it to stop guys

any help please

You could use a LDR, map the values it gives to the range and positions of the servo you want to actuate and use myservo.write function with the mapped variable so you have smooth transition and not sudden jumps.

What motor do you have?

I have servo motor sir

kaznov:
I got it to work :slight_smile: but my problem is that the motor doesnt stop when it gets to the desird angle insteas it rotates to the other direction , I want it to stop guys

any help please

You need to rethink this. If you ask the servo to move to a specific angle it starts moving. If you then during the move detect that this is where you want to stop you do not know what angle it is in right now. This should give you a clue how to solve the problem. Half the solution is getting the problem spec right.

Your idea is:

  1. Start moving to far angle.
  2. Measure and when right luminance stop servo.

You could do it in small steps:

  1. Measure and check if the blinds need adjusting.
  2. If need adjusting move a small step in the calculated direction and loop back to 1.

To improve and remove irritation of moving servo there should be a threshold before adjusting.

Cheers.

There are now 2 threads on the same subject. This could get confusing.

This could get confusing.

It was confusing enough with one thread, no code, vague requirements, and not a lot of detail as to what was working and what was not.

Seems to me that you just need some code to read the sensor, calculate the light level and decide which position the blinds should be in based on the light level, finally move the servo to that position. None of these things seem especially hard. Which part is causing you trouble?

Well, you say:

the motor doesnt stop when it gets to the desird angle insteas it rotates to the other direction , I want it to stop guys
any help please

And as PeterH says:

Seems to me that you just need some code to read the sensor, calculate the light level and decide which position the blinds should be in based on the light level, finally move the servo to that position. None of these things seem especially hard. Which part is causing you trouble?

SO help us help you by posting your code

If it moves too far the opposite direction then you are thinking about the angles incorrectly. First off, is it a motor or a servo. Most servos only rotate 180 degrees (I think some go 360, but I never used any).

If your starting point is 180 then try going only 45 degrees, then 90, then 180. Make sure your calculations for commanding the move are correct.

It would be great help if you posted the entire code. A piece of it is really no help if we can't see how you initialize everything and the full instructions. Without seeing everything, you leave us in the wind. It's like getting an Iron Man suit without any instructions. :astonished: