Coche Robot, con servomotores continuos y dos LDR
Material
2 Servos Continuos
2 LDR
2 Resistencias 10 Kohm
#include <Servo.h>
#include <math.h>
Servo rightServo;
Servo leftServo;
long ultrasound_distance_1() {
long duration, distance;
digitalWrite(8,LOW);
delayMicroseconds(2);
digitalWrite(8, HIGH);
delayMicroseconds(10);
digitalWrite(8, LOW);
duration = pulseIn(9, HIGH);
distance = duration/58;
return distance;
}
int rightSpeed = 0;
int leftSpeed = 0;
void motorControl2(int rightSpeed, int leftSpeed, int stepDelay) {
rightServo.write(90 + rightSpeed); leftServo.write(90 - leftSpeed);
delay(stepDelay*1000);}
void setup() {
pinMode(8, OUTPUT);
pinMode(9, INPUT);
rightServo.write(90);
leftServo.write(90);
delay(1000);
leftServo.attach(2);
rightServo.attach(3);
}
void loop() {
if (analogRead(A0) < 700 & analogRead(A0) < 700) {
motorControl2 (-45, -45, 1 );
} else {
if (analogRead(A0) < 700) {
motorControl2 (-45, 45, 1 );
} else {
if (analogRead(A1) < 700) {
motorControl2 (45, -45, 1 );
}
}
}
}