Hello, im new to Arduino and havent recieved it yet, i wrote some code and was curious to know if it would work. The code is supposed to be for a light following device, it will have 4 ldr that are attached to the top of a servo thats connected to another servo, this is the coding i have wrote , could someone tell me if it will do what i think it will do, thank you
#include <Servo.h> // do i need this twice, for two servos?
Servo servo_1;
Servo servo_2;
int servo_1_position = 90; // servo is from 0 to 180
int servo_2_position = 90;
int ldr_up = 0;
int ldr_down = 0;
int ldr_left = 0;
int ldr_right = 0;
void setup()
{
servo_1.attach(9); //servo input at pin 9
servo_2.attach(10);
Serial.begin(9600);
}
void loop()
{
ldr_up=analogRead(ldr_up); //gets analog readings from ldr
Serial.println(ldr_up); //print to screen values (i think)
ldr_down = analogRead(ldr_down);
Serial.println(ldr_down);
ldr_right = analogRead(ldr_right);
Serial.println(ldr_right);
ldr_left = analogRead(ldr_left);
Serial.println(ldr_left);
int vertical = ldr_up - ldr_down;
int horizontal = ldr_right - ldr_left;
/////// Vertical //////// (Servo 2) //////
if (vertical <= 50)
{
servo_1_position = --servo_1_position;
}
else if (vertical >= -50)
{
servo_1_position = ++servo_1_position;
}
else if (vertical == 0)
{
// do nothing
}
/////// Horizontal ////// (Servo 2) //////
if (horizontal <= 50)
{
servo_2_position = --servo_2_position; // not sure about the direction
}
else if ( horizontal >= -50)
{
servo_2_position = ++servo_2_position;
}
else if ( horizontal == 0);
{
//do nothing
}
delay(1000);
}