Go Down

### Topic: Photosensitive Resistance's controlling the angle of my servo (Read 3080 times)previous topic - next topic

#### Atjeh

##### Jul 09, 2013, 02:00 pm
Hi there,

First of all, sorry I'm new to programming, I learned my self so my knowledge is still expanding. Google is a mess for finding a solution, so I turn to you guys.

I have an small project thats redirect the sun from my balcony in to my living room with a servo controlled mirror. The function of the servo is that when the sun moves, the servo corrects the angle of the mirror. The whole setup is already done, I "think" that my code is almost done but a small problem; my servo keeps shaking :0.

I use 5 x Photosensitive detection switch

Angled at 45 ° till 135 °

Code: [Select]
`#include <Servo.h> Servo myservo;  // create servo object to control a servo int PhotoPin1 = 5;     // the pin number of the 1st Photosensitive Resistanceint PhotoPin2 = 6;     // the pin number of the 2nd Photosensitive Resistanceint PhotoPin3 = 7;     // the pin number of the 3th Photosensitive Resistanceint PhotoPin4 = 8;     // the pin number of the 4th Photosensitive Resistanceint PhotoPin5 = 9;     // the pin number of the 5th Photosensitive Resistanceint angle = 90;    // variable to store the servo positionint PhotoPin1State = 0;       // variables for reading the Photosensitive Resistance status'sint PhotoPin2State = 0;int PhotoPin3State = 0;int PhotoPin4State = 0;int PhotoPin5State = 0;void setup() {   myservo.attach(2);  // attaches the servo on pin 2 to the servo object   pinMode(PhotoPin1, INPUT);     //initialize the PhotoPin's as output  pinMode(PhotoPin2, INPUT);  pinMode(PhotoPin3, INPUT);   pinMode(PhotoPin4, INPUT);  pinMode(PhotoPin5, INPUT); } void loop() {   PhotoPin1State = digitalRead(PhotoPin1);    // read the states of the PhotoPin's  PhotoPin2State = digitalRead(PhotoPin2);  PhotoPin3State = digitalRead(PhotoPin3);  PhotoPin4State = digitalRead(PhotoPin4);  PhotoPin5State = digitalRead(PhotoPin5);    if (PhotoPin1State == HIGH)         {      if (angle > 0)      {        angle -= 5;      }    myservo.write(angle);                  // sets the servo position according to the scaled value     delay(15);      // waits for the servo to get there     }  if (PhotoPin2State == HIGH)         {      if (angle > 0)      {        angle -= 1;      }    myservo.write(angle);                  // sets the servo position according to the scaled value     delay(15);      // waits for the servo to get there     }  if (PhotoPin3State == HIGH)         {      if (angle > 90)      {        angle += 0;      }    myservo.write(angle);                  // sets the servo position according to the scaled value     delay(15);      // waits for the servo to get there     }  if (PhotoPin4State == HIGH)         {      if (angle < 180)      {        angle += 1;      }    myservo.write(angle);                  // sets the servo position according to the scaled value     delay(15);      // waits for the servo to get there     }  if (PhotoPin5State == HIGH)         {      if (angle < 180)      {        angle += 5;      }    myservo.write(angle);                  // sets the servo position according to the scaled value     delay(15);      // waits for the servo to get there     }      } `

I hope that someone can give me an "aha moment"

Alex.

#### PaulS

#1
##### Jul 09, 2013, 02:09 pm
Quote
my servo keeps shaking

Servos are electric motors. Electric motors rotate. Unless the rotor is out of balance, they don't "shake". You need to define what "shake" means.

Code: [Select]
`      if (angle > 90)      {        angle += 0;      }`
How useful!

How is the servo powered?

#### Atjeh

#2
##### Jul 09, 2013, 02:19 pmLast Edit: Jul 09, 2013, 02:21 pm by Atjeh Reason: 1
The servo (Tower Pro, Micro servo 9g sg90) is powered by my arduino nano connected at 5v, ground and pin 2

and the shaking looks like a "search for position or and double command

And my idea with "angle += 0;" is eventually be an hold option  :*

#### MichaelMeissner

#3
##### Jul 09, 2013, 02:45 pm

And my idea with "angle += 0;" is eventually be an hold option  :*

Which the compiler will likely delete during optimization, along with the if statement that now is useless.

#### Robin2

#4
##### Jul 09, 2013, 02:55 pm
In your photo (which looks very nice) all the LDRs seem to be facing the same way so I don't understand why you read them and react to them separately.

I suspect the vibration in your servo has one of two causes

the software may be asking it to go beyond the physical limit of its travel - 0degrees and 180degrees are only approximate and you may need to experiment to find what suits your particular servo.

the commands generated by your LDRs may be asking it to move this way and that way. This may have something to do with the fact that you read each LDR separately - they will not all behave identically. In any case the sun doesn't move fast so it would not be necessary to update the servo position more than once every minute or ten.

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

#### PaulS

#5
##### Jul 09, 2013, 03:17 pm
The other thing that makes a servo "shake" is being underpowered. Powering a servo from the Arduino is, generally, a bad idea. Any but the smallest servos doing nothing draw too much current.

Get a real power supply to power the servo, and I'm willing to bet that the problems go away.

#### Grumpy_Mike

#6
##### Jul 09, 2013, 03:49 pm
Print out the value for angle in each loop and see what it is doing. You might find that it is shaking.

#### Atjeh

#7
##### Jul 11, 2013, 04:20 pm

Even when the PhotoPins are low I found that the servo still receive the commands (-= 5 and +=5), and because + and - is in conflict its looks like an "out of ballance shake".

Why, I still dont know but I found that the problem lies with in the "+/- angle" code.

This week I'll hope to solved it all and maybe an pic of my project

Alex.

#### Robin2

#8
##### Jul 11, 2013, 04:43 pm
What's the resistance of your LDRs? If it's over 10k ohms it may be that the ADC is not able to measure the voltage correctly. I read on another thread that it's a good idea with higher resistance potentiometers to read the same measurement twice in succession and just use the second reading, but I have not tried this. And the output from an LDR can vary very quickly so the idea may not be relevant to them.

Another possibility may be to treat the LDRs as "digital" devices and just take an on/off signal from them depending on whether the voltage is above or below some number - in other words don't bother with fine resolution.

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

#### AWOL

#9
##### Jul 11, 2013, 04:46 pm
Quote
And the output from an LDR can vary very quickly

...if you think tens of milliseconds is "very quickly"
"Pete, it's a fool (who) looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.
I speak for myself, not Arduino.

Go Up