Pages: [1]   Go Down
Author Topic: Photosensitive Resistance's controlling the angle of my servo  (Read 908 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 6
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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 smiley-mad.

I use 5 x Photosensitive detection switch

Angled at 45 ° till 135 °


Code:
#include <Servo.h>
 
Servo myservo;  // create servo object to control a servo
 
int PhotoPin1 = 5;     // the pin number of the 1st Photosensitive Resistance
int PhotoPin2 = 6;     // the pin number of the 2nd Photosensitive Resistance
int PhotoPin3 = 7;     // the pin number of the 3th Photosensitive Resistance
int PhotoPin4 = 8;     // the pin number of the 4th Photosensitive Resistance
int PhotoPin5 = 9;     // the pin number of the 5th Photosensitive Resistance
int angle = 90;    // variable to store the servo position

int PhotoPin1State = 0;       // variables for reading the Photosensitive Resistance status's
int 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.

Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
      if (angle > 90)
      {
        angle += 0;
      }
How useful!

How is the servo powered?
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 6
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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  smiley-kiss  
« Last Edit: July 09, 2013, 07:21:41 am by Atjeh » Logged

Ayer, Massachusetts, USA
Offline Offline
Edison Member
*
Karma: 53
Posts: 1832
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

And my idea with "angle += 0;" is eventually be an hold option  smiley-kiss  
Which the compiler will likely delete during optimization, along with the if statement that now is useless.
Logged

UK
Offline Offline
Tesla Member
***
Karma: 101
Posts: 6278
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Manchester (England England)
Offline Offline
Brattain Member
*****
Karma: 602
Posts: 33371
Solder is electric glue
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Print out the value for angle in each loop and see what it is doing. You might find that it is shaking.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 6
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thnx for your help guys!,

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.
Logged

UK
Offline Offline
Tesla Member
***
Karma: 101
Posts: 6278
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 290
Posts: 25770
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
And the output from an LDR can vary very quickly
...if you think tens of milliseconds is "very quickly"
Logged

"Pete, it's a fool 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.

Pages: [1]   Go Up
Jump to: