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
kE9s4,BuQWBRwB,24jCg~~60_3.JPG)
Angled at 45 ° till 135 °
#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.