I found this sketch in "Instructables," built the sketch and find it very erratic at best. Is this because I have to modify the motor to rotate 360? You will notice at the bottom several references to "180".
#include <Servo.h> // include Servo library
Servo horizontal; // horizontal servo
int servoh = 90; // stand horizontal servo
Servo vertical; // vertical servo
int servov = 90; // stand vertical servo
// LDR pin connections
// name = analogpin;
int ldrlt = 0; //LDR top left
int ldrrt = 1; //LDR top rigt
int ldrld = 2; //LDR down left
int ldrrd = 3; //ldr down rigt
void setup()
{
Serial.begin(9600);
// servo connections
// name.attacht(pin);
horizontal.attach(9);
vertical.attach(10);
}
void loop()
{
int lt = analogRead(ldrlt); // top left
int rt = analogRead(ldrrt); // top right
int ld = analogRead(ldrld); // down left
int rd = analogRead(ldrrd); // down rigt
int dtime = analogRead(4)/20; // read potentiometers
int tol = analogRead(5)/4;
int avt = (lt + rt) / 2; // average value top
int avd = (ld + rd) / 2; // average value down
int avl = (lt + ld) / 2; // average value left
int avr = (rt + rd) / 2; // average value right
int dvert = avt - avd; // check the diffirence of up and down
int dhoriz = avl - avr;// check the diffirence og left and rigt
if (-1*tol > dvert || dvert > tol) // check if the diffirence is in the tolerance else change vertical angle
{
if (avt > avd)
{
servov = ++servov;
if (servov > 90)
{
servov = 90;
}
}
else if (avt < avd)
{
servov= --servov;
if (servov < 0)
{
servov = 0;
}
}
vertical.write(servov);
}
if (-1*tol > dhoriz || dhoriz > tol) // check if the diffirence is in the tolerance else change horizontal angle
{
if (avl > avr)
{
servoh = --servoh;
if (servoh < 0)
{
servoh = 0;
}
}
else if (avl < avr)
{
servoh = ++servoh;
[color=red][font=Verdana]if (servoh > 180)[/font][/color]
{
[color=red]servoh = 180;[/color]
}
}
else if (avl = avr)
{
// nothing
}
horizontal.write(servoh);
}
delay(dtime);
}
You have codes that do not belong in the sketch.
Have you copied it directly in your sketch and tried to upload it?
Did that work...
Is this because you pasted it here?
Yes, I pasted the code into the sketch, assuming it was exactly what is needed. Sorry, newbie here. What part of the code does not belong? I tried to replace the numbers - 90 and 180 with 45/90 hoping that the original sketch was intended for a freely rotating servo but these didn't work either. I am sure the wiring is correct because the motors do respond (sometimes) to light light but only for a moment.
This video of the tilt/pan sun tracker is exactly what I want. Somehow I have managed to corrupt either code or wiring but since the servos do respond to light, I don't want to believe the circuit is faulty. http://www.instructables.com/id/Arduino-Solar-Tracker/
Somehow I have managed to corrupt either code or wiring but since the servos do respond to light, I don't want to believe the circuit is faulty.
If it isn't the hardware, it must be the software. It's easy enough to check. Print out the values that you get (Serial.print() --> Serial Monitor) from the 6 sensors. If they match expectations, then the input side is fine.
Print out the values that you expect to move the servos to, based on that input. If the values are reasonable, and the servos go to those positions, the output side is fine.
Where the reality differs from fantasy (your expectations :)) you can begin troubleshooting.
void loop()
{
int lt = analogRead(ldrlt); // top left 590
int rt = analogRead(ldrrt); // top right 560
int ld = analogRead(ldrld); // down left 450
int rd = analogRead(ldrrd); // down rigt 540
int dtime = analogRead(4)/20; // read potentiometers
int tol = analogRead(5)/4;
int avt = (lt + rt) / 2; // average value top 565
int avd = (ld + rd) / 2; // average value down 480
int avl = (lt + ld) / 2; // average value left 520
int avr = (rt + rd) / 2; // average value right 540
int dvert = avt - avd; // check the diffirence of up and down 86
int dhoriz = avl - avr;// check the diffirence og left and rigt -25
if (-1*tol > dvert || dvert > tol) // check if the diffirence is in the tolerance else change vertical angle
{
if (avt > avd)
{
servov = ++servov;
if (servov > 90)
{
servov = 90;
}
}
else if (avt < avd)
{
servov= --servov;
if (servov < 0)
{
servov = 0;
}
}
vertical.write(servov);
}
if (-1*tol > dhoriz || dhoriz > tol) // check if the diffirence is in the tolerance else change horizontal angle
{
if (avl > avr)
{
servoh = --servoh;
if (servoh < 0)
{
servoh = 0;
}
}
else if (avl < avr)
{
servoh = ++servoh;
[color=red][font=Verdana]if (servoh > 180)[/font][/color]
{
[color=red]servoh = 180;[/color]
}
}
else if (avl = avr)
{
// nothing
}
horizontal.write(servoh);
}
delay(dtime);
}
I have to chase this turkey until successful even though it it just a bit beyond my grasp. Tilt and pan are essential to a series of sculptures I am designing. I appreciate the help.
As PaulS pointed out, the schema has some formatting tags like these: [/font][/color] that don't belong to the actual code. I guess it doesn't even compile. Remove those tags before you continue.
This sketch compiles but the results only moves the horizontal motor slightly, the vertical none at all. I have rebuilt the circuit three times. If I fully understood the conditional statements, the values would make more sense.
#include <Servo.h> // include Servo library
Servo horizontal; // horizontal servo
int servoh = 90; // stand horizontal servo
Servo vertical; // vertical servo
int servov = 90; // stand vertical servo
// LDR pin connections
// name = analogpin;
int ldrlt = 0; //LDR top left
int ldrrt = 1; //LDR top rigt
int ldrld = 2; //LDR down left
int ldrrd = 3; //ldr down rigt
void setup()
{
Serial.begin(9600);
// servo connections
// name.attacht(pin);
horizontal.attach(9);
vertical.attach(10);
}
void loop()
{
int lt = analogRead(ldrlt); // top left
int rt = analogRead(ldrrt); // top right
int ld = analogRead(ldrld); // down left
int rd = analogRead(ldrrd); // down rigt
int dtime = analogRead(4)/20; // read potentiometers
int tol = analogRead(5)/4;
int avt = (lt + rt) / 2; // average value top
int avd = (ld + rd) / 2; // average value down
int avl = (lt + ld) / 2; // average value left
int avr = (rt + rd) / 2; // average value right;
delay(1000);
int dvert = avt - avd; // check the diffirence of up and down
int dhoriz = avl - avr;// check the diffirence og left and rigt
if (-1*tol > dvert || dvert > tol) // check if the diffirence is in the tolerance else change vertical angle
{
if (avt > avd)
{
servov = ++servov;
if (servov > 90)
{
servov = 90;
}
}
else if (avt < avd)
{
servov= --servov;
if (servov < 0)
{
servov = 0;
}
}
vertical.write(servov);
}
if (-1*tol > dhoriz || dhoriz > tol) // check if the diffirence is in the tolerance else change horizontal angle
{
if (avl > avr)
{
servoh = --servoh;
if (servoh < 0)
{
servoh = 0;
}
}
else if (avl < avr)
{
servoh = ++servoh;
if (servoh > 180)
{
servoh = 180;
}
}
else if (avl = avr)
{
// nothing
}
horizontal.write(servoh);
}
delay(dtime);
}
I have to admit, I am not sure what the optimum range of values should look like. Are those posted above "logical"? I am wondering if the pot values are appropriate? Again, the code governing the math processing the sensor input is difficult to follow for me.
Seems the bloody thing was working all the time but so slowly, I didn't notice. After changing the pot values, the motors are now responding to light. Thanks for the tip (i.e. recording the output of the sensors).
int dtime = analogRead(4)/2000; // read potentiometers (original value = 20)