Hello everybody,
I wrote a simple sketch to follow a light source using 3 sensors (LDR or phototransistors) and two hobby servos. I'm share with you because it could be used on other didatics applications. Any question, please let me know... Good luck and have fun.
Guto
Matão-SP
gutosan@terra.com.br
The Sketch:
// Controlling a solar panel with two servos and three photo-transistors
// I suggest high-torque servos to straight control of your solar panel.
//
// By José Augusto Santaella - Concluding degree in Mechatronics Engineering
// Anhanguera Matão-SP
// e-mail address: gutosan@terra.com.br
//
// Example for the cientific project of Mr. Carlos Eduardo Servidoni - SENAI Araraquara-SP
// This sketch could be used for education and other purposes
// void loop instruction in English and Portuguese.
#include <Servo.h>
Servo servo_x; // create servo object to control a servo
Servo servo_y; // create servo object to control a servo
int sensor0 = 0; // analog pin used to connect the phototransitor 0
int sensor1 = 1; // analog pin used to connect the phototransitor 1
int sensor2 = 2; // analog pin used to connect the phototransitor 2
int fotocel = 3; // analog pin used to connect the solar panel
int var_a; // variable to read the value from the analog pin 0
int var_b; // variable to read the value from the analog pin 1
int var_c; // variable to read the value from the analog pin 2
int volts; // variable to read the value from the analog pin 3
float V; // variable that will be used with floar numbers
int eixo_x=90; // variable to manipulate x-axis position (starting on 90 degrees)
int eixo_y=90; // variable to manipulate x-axis position (starting on 90 degrees)
void setup()
{
servo_y.attach(9); // attaches the servo on pin 9 to the servo object
servo_x.attach(10); // attaches the servo on pin 10 to the servo object
Serial.begin(9600); // Start serial communication
}
void loop()
{
var_a = analogRead(sensor0); // reads the value of the photo-transitor (value between 0 and 1023)
var_a = map(var_a, 0, 1023, 0, 100); // scale it to use it with the servo position calculations (value between 0 and 100)
var_b = analogRead(sensor1); // reads the value of the photo-transitor (value between 0 and 1023)
var_b = map(var_b, 0, 1023, 0, 100); // scale it to use it with the servo position calculations (value between 0 and 100)
var_c = analogRead(sensor2); // reads the value of the photo-transitor (value between 0 and 1023)
var_c = map(var_c, 0, 1023, 0, 100); // scale it to use it with the servo position calculations (value between 0 and 100)
volts = analogRead(fotocel); // reads the solar panel voltage
volts = map(volts, 0, 1023, 0, 500); // scale it to use it with command serial print (value between 0 a 500)
// Portuguese:
if (var_a > var_b) // comparamos a origem com o eixo y
{
eixo_y = eixo_y + 1; // se a > b então aumenta um grau
eixo_y = constrain(eixo_y, 0, 179); // estabelece a faixa entre 0 e 179 grau para não estourar a contagem
servo_y.write(eixo_y ); // escreve o valor calculado na variável do eixo y
delay(10); // temporiza 10 milisegundos (faixa testada entre 10~100)
}
else
{
// English:
if (var_a < var_b) // comparing the origin with the y-axis
eixo_y = eixo_y - 1; // if a < b then a degree decreases
eixo_y = constrain(eixo_y, 0, 179); // Establishes the range Between 0 and 179 degree not to over count
servo_y.write(eixo_y); // writes the calculated value in the y-axis variable
delay(10); // temporize 10 milliseconds (range tested between 10 ~ 100)
}
if (var_a > var_c) // o mesmo para o eixo x
{
eixo_x = eixo_x + 1;
eixo_x = constrain(eixo_x, 0, 179);
servo_x.write(eixo_x);
delay(10);
}
else
{
if (var_a < var_c) // the same for the x-axis
eixo_x = eixo_x - 1;
eixo_x = constrain(eixo_x, 0, 179);
servo_x.write(eixo_x);
delay(10);
}
Serial.print("Eixo X = " );
Serial.print(eixo_x);
Serial.print(" Eixo Y = ");
Serial.println(eixo_y);
Serial.print("Tensao = ");
V = float (volts) / 100.00; //using decimal point
Serial.println(V);
Serial.print(" Volts");
}