0% found this document useful (0 votes)
3 views

code arduino

The document is a code snippet for controlling two servo motors (horizontal and vertical) using an Arduino, with specific angle limits for each servo. It reads values from four light-dependent resistors (LDRs) to determine the average light intensity from different directions and adjusts the servo angles accordingly to maintain balance. The code includes setup and loop functions to manage the servo movements based on the light sensor readings and defined tolerances.
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

code arduino

The document is a code snippet for controlling two servo motors (horizontal and vertical) using an Arduino, with specific angle limits for each servo. It reads values from four light-dependent resistors (LDRs) to determine the average light intensity from different directions and adjusts the servo angles accordingly to maintain balance. The code includes setup and loop functions to manage the servo movements based on the light sensor readings and defined tolerances.
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 4

#include <Servo.

h>// include Servo library

// servo trục ngang có thể quay từ 5 đến 170 độ


Servo horizontal; // servo ngang

int servoh = 90; // 90; // góc ban đầu

int servohLimitHigh = 170;


int servohLimitLow = 5;

// Servo trục dọc có thể quay từ 5° đến 130°


Servo vertical; // vertical servo

int servov = 45; // 90; // stand vertical servo

int servovLimitHigh = 130;


int servovLimitLow = 5;

// LDR pin connections


// name = analogpin;

int ldrlt = 0; //LDR top left

int ldrrt = 1; //LDR top right

int ldrld = 2; //LDR down left

int ldrrd = 3; //ldr down right

void setup()
{ Serial.begin(9600);

// servo connections

// name.attacht(pin);

horizontal.attach(9);

vertical.attach(10);

horizontal.write(90);

vertical.write(45);

delay(3000);

void loop()
{ int lt = analogRead(ldrlt); // top left // đọc giá trị cảm biến

int rt = analogRead(ldrrt); // top right

int ld = analogRead(ldrld); // down left

int rd = analogRead(ldrrd); // down right

// int dtime = analogRead(4)/20; // read potentiometers


// int tol = analogRead(5)/4;

int dtime = 10; int tol = 50;

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

Serial.print(avt);

Serial.print(" ");

Serial.print(avd);

Serial.print(" ");

Serial.print(avl);

Serial.print(" ");

Serial.print(avr);

Serial.print(" ");

Serial.print(dtime);

Serial.print(" ");

Serial.print(tol);

Serial.println(" ");

if (-1*tol > dvert || dvert > tol) // check if the diffirence is in the tolerance
else change vertical angle

if (avt > avd)

servov = ++servov;
delay(100);

if (servov > servovLimitHigh)

servov = servovLimitHigh;
}

else if (avt < avd)

servov= --servov;
delay(100);

if (servov < servovLimitLow)

servov = servovLimitLow;

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;
delay(100);

if (servoh < servohLimitLow)

servoh = servohLimitLow;

else if (avl < avr)

servoh = ++servoh;
delay(100);

if (servoh > servohLimitHigh)

servoh = servohLimitHigh;
}

else if (avl = avr)

// nothing

horizontal.write(servoh);

delay(dtime);

You might also like