KIT GIRASOL
Dificultad: Fácil
Materiales:
- Arduino
- Protoboard
- Servo motor
- Sensores de luz (digitales)
- Cables para conectar
Código:
Primer paso
Primero declaramos el Servomotor, después el pin análogo que recibirá la señal de las fotoresistencias y una variable para leer el valor del pin análogo.
Segundo paso:
En el void setup creamos la comuncacion externa y ponemos el servomotor en el pin 9.
Tercer paso:
En el void loop creamos la variable que va a leer el valor de la fotoresistencia (valor de 0 a 1023).
Creamos la escala que sera usada con el servomotor (valor de 0 a 180).
Agregamos una variable que marca el valor del servomotor de acuerdo al valor de la escala.
Agregamos una variable que al momento del que el servo llega a su posición deja una espera (un paro) del tiempo deseado.
Aqui les dejo el codigo :
#include <Servo.h>
Servo myservo;
int pos = 0; // Variable to store the servo position.
int inputPhotoLeft = 1; // Fotorresistencia pin analogico 0 de arduino
int inputPhotoRight = 0;// Fotorresistencia pin analogico 1 de arduino
int Left = 0; // Store readings from the photoresistors.
int Right = 0; // Store readings from the photoresistors.
void setup()
{
Serial.begin(9600);
myservo.attach(9); // Attach servo to pin 9.
}
void loop()
{
// Reads the values from the photoresistors to the Left and Right variables.
Left = analogRead(inputPhotoLeft);
Right = analogRead(inputPhotoRight);
Serial.println(Left);
Serial.println(Right);
// Checks if right is greater than left, if so move to right.
if (Left > (Right +20))
// +20 is the deadzone, so it wont jiggle back and forth.
{
if (pos < 179)
pos++;
myservo.write(pos);
}
// Checks if left is greater than right, if so move to left.
if (Right > (Left +20))
// +20 is the deadzone, so it wont jiggle back and forth.
{
if (pos > 1)
pos -= 1;
myservo.write(pos);
}

Comentarios
Publicar un comentario