0% found this document useful (0 votes)
14 views5 pages

Robot

The document defines functions to control a servo motor and read analog sensor values. It uses if/else statements to control LEDs and the servo motor based on the sensor readings.

Uploaded by

alvaritor556
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)
14 views5 pages

Robot

The document defines functions to control a servo motor and read analog sensor values. It uses if/else statements to control LEDs and the servo motor based on the sensor readings.

Uploaded by

alvaritor556
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/ 5

#include <Servo.

h>

Servo myservo; // create servo object to control a servo


// twelve servo objects can be created on most boards

int pos = 0;

void setup() {
// put your setup code here, to run once:
myservo.attach(11);
pinMode(2, OUTPUT);
pinMode(3, OUTPUT);
pinMode(4, OUTPUT);
pinMode(5, OUTPUT);
pinMode(6, OUTPUT);
pinMode(A0, INPUT);
pinMode(A1, INPUT);
pinMode(A2, INPUT);
Serial.begin(9600);
digitalWrite(6, HIGH);
}

void loop() {
// put your main code here, to run repeatedly:

int a = analogRead(A0);
int b = analogRead(A1);
int c = analogRead(A2);
Serial.print(a);
Serial.print(" ");
Serial.print(b);
Serial.print(" ");
Serial.print(c);
Serial.println(" ");
delay(50);

if (a<=250)

{
digitalWrite(2, LOW);
digitalWrite(3, LOW);
digitalWrite(4, LOW);
digitalWrite(5, LOW);

digitalWrite(6, LOW);

for (pos = 60; pos <= 120; pos += 1) { // goes from 0 degrees to 180 degrees
// in steps of 1 degree
myservo.write(pos); // tell servo to go to position in variable
'pos'
delay(15); // waits 15 ms for the servo to reach the
position
}
for (pos = 120; pos >= 60; pos -= 1) { // goes from 180 degrees to 0 degrees
myservo.write(pos); // tell servo to go to position in variable
'pos'
delay(15); // waits 15 ms for the servo to reach the
position
}
for (pos = 60; pos <= 120; pos += 1) { // goes from 0 degrees to 180 degrees
// in steps of 1 degree
myservo.write(pos); // tell servo to go to position in variable
'pos'
delay(15); // waits 15 ms for the servo to reach the
position
}
for (pos = 120; pos >= 60; pos -= 1) { // goes from 180 degrees to 0 degrees
myservo.write(pos); // tell servo to go to position in variable
'pos'
delay(15); // waits 15 ms for the servo to reach the
position
}
for (pos = 60; pos <= 120; pos += 1) { // goes from 0 degrees to 180 degrees
// in steps of 1 degree
myservo.write(pos); // tell servo to go to position in variable
'pos'
delay(15); // waits 15 ms for the servo to reach the
position
}
for (pos = 120; pos >= 60; pos -= 1) { // goes from 180 degrees to 0 degrees
myservo.write(pos); // tell servo to go to position in variable
'pos'
delay(15); // waits 15 ms for the servo to reach the
position
}
//delay(200);

else if(b<=450)

digitalWrite(2, LOW);
digitalWrite(3, LOW);
digitalWrite(4, LOW);
digitalWrite(5, LOW);

digitalWrite(6, LOW);
for (pos = 60; pos <= 120; pos += 1) { // goes from 0 degrees to 180 degrees
// in steps of 1 degree
myservo.write(pos); // tell servo to go to position in variable
'pos'
delay(15); // waits 15 ms for the servo to reach the
position
}
for (pos = 120; pos >= 60; pos -= 1) { // goes from 180 degrees to 0 degrees
myservo.write(pos); // tell servo to go to position in variable
'pos'
delay(15); // waits 15 ms for the servo to reach the
position
}
for (pos = 60; pos <= 120; pos += 1) { // goes from 0 degrees to 180 degrees
// in steps of 1 degree
myservo.write(pos); // tell servo to go to position in variable
'pos'
delay(15); // waits 15 ms for the servo to reach the
position
}
for (pos = 120; pos >= 60; pos -= 1) { // goes from 180 degrees to 0 degrees
myservo.write(pos); // tell servo to go to position in variable
'pos'
delay(15); // waits 15 ms for the servo to reach the
position
}
for (pos = 60; pos <= 120; pos += 1) { // goes from 0 degrees to 180 degrees
// in steps of 1 degree
myservo.write(pos); // tell servo to go to position in variable
'pos'
delay(15); // waits 15 ms for the servo to reach the
position
}
for (pos = 120; pos >= 60; pos -= 1) { // goes from 180 degrees to 0 degrees
myservo.write(pos); // tell servo to go to position in variable
'pos'
delay(15); // waits 15 ms for the servo to reach the
position
}

}
else if(c<=250)

digitalWrite(2, LOW);
digitalWrite(3, LOW);
digitalWrite(4, LOW);
digitalWrite(5, LOW);

digitalWrite(6, LOW);
for (pos = 60; pos <= 120; pos += 1) { // goes from 0 degrees to 180 degrees
// in steps of 1 degree
myservo.write(pos); // tell servo to go to position in variable
'pos'
delay(15); // waits 15 ms for the servo to reach the
position
}
for (pos = 120; pos >= 60; pos -= 1) { // goes from 180 degrees to 0 degrees
myservo.write(pos); // tell servo to go to position in variable
'pos'
delay(15); // waits 15 ms for the servo to reach the
position
}
for (pos = 60; pos <= 120; pos += 1) { // goes from 0 degrees to 180 degrees
// in steps of 1 degree
myservo.write(pos); // tell servo to go to position in variable
'pos'
delay(15); // waits 15 ms for the servo to reach the
position
}
for (pos = 120; pos >= 60; pos -= 1) { // goes from 180 degrees to 0 degrees
myservo.write(pos); // tell servo to go to position in variable
'pos'
delay(15); // waits 15 ms for the servo to reach the
position
}
for (pos = 60; pos <= 120; pos += 1) { // goes from 0 degrees to 180 degrees
// in steps of 1 degree
myservo.write(pos); // tell servo to go to position in variable
'pos'
delay(15); // waits 15 ms for the servo to reach the
position
}
for (pos = 120; pos >= 60; pos -= 1) { // goes from 180 degrees to 0 degrees
myservo.write(pos); // tell servo to go to position in variable
'pos'
delay(15); // waits 15 ms for the servo to reach the
position
}

else if(a>=251 && a<=700)

{
digitalWrite(6, HIGH);

digitalWrite(2, LOW);
digitalWrite(3,HIGH);
digitalWrite(4, HIGH);
digitalWrite(5, LOW);

delay(500);

digitalWrite(6, HIGH);

digitalWrite(2, HIGH);
digitalWrite(3, LOW);
digitalWrite(4, HIGH);
digitalWrite(5, LOW);
delay(500);

else if(b>=251 && b<=800)

digitalWrite(2, LOW);
digitalWrite(3,HIGH);
digitalWrite(4, HIGH);
digitalWrite(5, LOW);

digitalWrite(6, HIGH);
}

else if(c>=251 && c<=800)

digitalWrite(2, HIGH);
digitalWrite(3, LOW);
digitalWrite(4, LOW);
digitalWrite(5, HIGH);

digitalWrite(6, HIGH);
delay(500);
digitalWrite(6, HIGH);

digitalWrite(2, HIGH);
digitalWrite(3, LOW);
digitalWrite(4, HIGH);
digitalWrite(5, LOW);
delay(500);

else

{
digitalWrite(6, HIGH);

digitalWrite(2, HIGH);
digitalWrite(3, LOW);
digitalWrite(4, HIGH);
digitalWrite(5, LOW);

You might also like