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

Code

This document controls two servo motors and an LCD display based on sensor input. It includes code to: 1) Initialize two servo motors and an LCD display to control and monitor the servos. 2) Read sensor values on four analog pins and calculate averages. 3) Move the first servo up/down depending on the top/bottom sensor average. 4) Move the second servo left/right depending on the left/right sensor average. 5) Continuously monitor the sensors and update the servo positions as needed.

Uploaded by

Jhanavi Reddy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
39 views

Code

This document controls two servo motors and an LCD display based on sensor input. It includes code to: 1) Initialize two servo motors and an LCD display to control and monitor the servos. 2) Read sensor values on four analog pins and calculate averages. 3) Move the first servo up/down depending on the top/bottom sensor average. 4) Move the second servo left/right depending on the left/right sensor average. 5) Continuously monitor the sensors and update the servo positions as needed.

Uploaded by

Jhanavi Reddy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

#include <LiquidCrystal.

h>
#include <Servo.h>

void UpDown();
void LeftRight();
Servo servo1;
Servo servo2;
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
void setup() {

lcd.begin(16,2);
lcd.print("servo1 ");
lcd.setCursor(0,1);
lcd.print("servo2 ");
servo1.attach(9);
servo2.attach(10);

servo1.write(90);
servo2.write(90);
}

void loop(){

int sensorTop = analogRead(A0);


int sensorBottom = analogRead(A1);
int sensorLeft = analogRead(A3);
int sensorRight = analogRead(A4);

int avgT=(sensorTop+sensorBottom)/2;
int avgB=(sensorLeft+sensorRight)/2;
int avgL=(sensorTop+sensorLeft)/2;
int avgR=(sensorBottom+sensorRight)/2;

if (avgT > avgB)


{
UpDown(sensorTop, sensorBottom);
}
if(avgT < avgB)
{
UpDown(sensorTop, sensorBottom);
}
if(avgL > avgR)
{
LeftRight(sensorLeft, sensorRight);
}
if(avgL < avgR)
{
LeftRight(sensorLeft, sensorRight);
}
delay(10);
}
void UpDown(int avgT, int avgB){
int pos1= servo1.read();

if(avgT < avgB){


pos1 = --pos1;
}
else
{
pos1 = ++pos1;
}
servo1.write(pos1);
lcd.setCursor(12,0);
lcd.print(pos1);
}
void LeftRight(int avgL, int avgR){
int pos2= servo2.read();
if(avgL < avgR)
{
pos2 = --pos2;
}
else
{
pos2 = pos2 + 1;
}
servo2.write(pos2);
lcd.setCursor(12,1);
lcd.print(pos2);
}

You might also like