How can I use Arduino to control the Full Rotational Servo Motor to let it only rotate 180?
Here is my code, can any one help me to troubleshoot it? hservo is using 360 degree servo motor, but I only want it to turn a bit.
#define BOTTOM 1
#define TOP 0
#define RIGHT 2
#define LEFT 3
#define CENTRE 4#include <Servo.h>
#include "math.h"Servo hservo;
Servo vservo;int topsense;
int bottomsense;
int leftsense;
int rightsense;int centresense;
int tavg;
int tavg1;
int diff;
int diff1;
int spd;
int spd1;
int divisor;
int sensitivity;void setup ()
{
hservo.writeMicroseconds(1500);
vservo.attach(9);
hservo.attach(10); // attaches the servo on pin 10 to the servo object
divisor = 10; // this controls the speed of the servo. lower number = higher speed
sensitivity = 5; ; // this controls the sensitivity of the tracker. lower number = higher sensitivity. if your tracker is constantly jittering back and forth increase the numberpinMode(BOTTOM, INPUT); // set the inputs
pinMode(TOP, INPUT);
pinMode(LEFT, INPUT);
pinMode(RIGHT, INPUT);pinMode(CENTRE, INPUT);
}
void loop ()
{topsense = analogRead(TOP); // read the light sensors
bottomsense = analogRead(BOTTOM);
rightsense = analogRead(RIGHT);
leftsense = analogRead(LEFT);centresense = analogRead(CENTRE);
tavg = (topsense + bottomsense)/2; // get an average value for the top 2 sensors
diff = abs(tavg - (centresense)); // this judges how far the tracker must turn
spd = diff/divisor; // and adjusts the speed of the reaction accordingly
spd = max(spd, 1); // sets the minimum speed to 1tavg1 = (rightsense + leftsense)/2;
diff1 = abs(tavg1 - leftsense);
spd1 = diff1/divisor;
spd1 = max(spd1, 1);if((rightsense < leftsense) && (diff > sensitivity))
{ // if the average value of the top sensors is smaller (more light) than the bottom sensor
vservo.write(90 - spd1); // send servo command to turn upward plus add speed
}else if((rightsense > leftsense) && (diff > sensitivity))
{ // if the value of the bottom sensor is smaller (more light) than the average value of the top sensors and the tilt sensor is in the correct rangevservo.write(90 + spd1);
// send servo command to turn downward plus add speed
}else
{ // for every other instance
vservo.write(90); // stop the y-axis motor
}topsense = analogRead(TOP); // read the top 2 sensors again because they have probably changed
bottomsense = analogRead(BOTTOM);
//trsense = trsense * 1.03; // again I had to adjust the value of one sensor to make the tracker more accurate
diff = abs(topsense - bottomsense); // reset the diff variable for the new values
spd = diff/divisor; // and generate a speed accordingly
spd = max(spd, 1); // set the minimum speed to 1if((topsense < bottomsense) && (diff > sensitivity))
{ // if the top left sensor value is smaller (more light) than the top right sensor
if ( (90+spd)<110)
{
hservo.write(90 + spd); // send servo command to turn lef
}
else
{
hservo.write(90);
}
}
else if((topsense > bottomsense) && (diff > sensitivity))
{ // if the top left sensor value
}
else
{ // for every other instance
hservo.write(90); // stop the x-axis motor
}delay(10); // delay 10ms
}