Automatic solar panel servo limit

hello,

As a project I am making an automatic solar panel.
I just have a little problem. how do you set a limit on your servo's?
the servo_10 has to be between 50 and 120 degrees.

thanks in advance.

here's my program:

#include <Servo.h>
//Initialize variables 
int topLeftLight = 0;     
int topRightLight = 0;
int bottomLeftLight = 0;
int bottomRightLight = 0;
int LeftLight = 0;
int RightLight = 0;
int TopLight = 0;
int BottomLight = 0;
//Declare two servos
Servo servo_9;
Servo servo_10;



void setup()
{

  pinMode(A3, INPUT);   //Light sensor up - left
  pinMode(A2, INPUT);   //Light sensor up - right
  pinMode(A1, INPUT);   //Light sensor bottom - left
  pinMode(A0, INPUT);   //Light sensor bottom - right
  servo_9.attach(9);    //Servo motor right - left movement
  servo_10.attach(10);  //Servo motor up - down movement
  servo_9.write(100);    //Servo motor right - left movement
  servo_10.write(60);  //Servo motor up - down movement
  delay(1000);
}

void loop()
{


    topLeftLight = map(analogRead(A3),50,980,0,100);
    topRightLight = map(analogRead(A2),200,990,0,100);
    bottomLeftLight = map(analogRead(A1),170,970,0,100);
    bottomRightLight = map(analogRead(A0),250,1000,0,100);
    //Calculate the average light conditions
    TopLight = ((topRightLight + topLeftLight) / 2);
    BottomLight = ((bottomRightLight + bottomLeftLight) / 2);
    LeftLight = ((topLeftLight + bottomLeftLight) / 2);
    RightLight = ((topRightLight + bottomRightLight) / 2);
    //Rotate the servos if needed
    if (abs((RightLight - LeftLight)) > 4) {    //Change position only if light difference is bigger then 4%
      if (RightLight < LeftLight) {
        if (servo_9.read() < 180) {
          servo_9.write((servo_9.read() + 1));
        }
      }
      if (RightLight > LeftLight) {
        if (servo_9.read() > 0) {
          servo_9.write((servo_9.read() - 1));
        }
      }
    }
    if (abs((TopLight - BottomLight)) > 4) {  //Change position only if light difference is bigger then 4%
      if (TopLight < BottomLight) {
        if (servo_10.read() < 120) {
          servo_10.write((servo_10.read() - 1));
        }
      }
      if (TopLight > BottomLight) {
        if (servo_10.read() > 50) {
          servo_10.write((servo_10.read() + 1));
        }
      }
    }
   delay(500);
}

servo_9.write(constrain(servo_9.read() + 1, 50, 120));

thanks for replying, i will try this.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.