Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <Ultrasonic.h>
- #define MOTOR_1_FORWARD 5
- #define MOTOR_1_BACK 6
- #define MOTOR_2_FORWARD 9
- #define MOTOR_2_BACK 10
- #define TRIG 3
- #define ECHO 2
- #define OBSTRACLE_DISTANCE 30.0
- #define TURN_DELAY 200
- Ultrasonic ultrasonic(TRIG, ECHO);
- float distance = 0;
- int estado = 'g';
- void setup()
- {
- pinMode(MOTOR_1_FORWARD, OUTPUT);
- pinMode(MOTOR_1_BACK, OUTPUT);
- pinMode(MOTOR_2_FORWARD, OUTPUT);
- pinMode(MOTOR_2_BACK, OUTPUT);
- stopMove();
- }
- void loop()
- {
- checkDistance();
- moove();
- delay(10);
- }
- void checkDistance()
- {
- distance = ultrasonic.Ranging(CM);
- }
- void moove()
- {
- if( distance > OBSTRACLE_DISTANCE )
- {
- if(Serial.available()>0){
- estado = Serial.read();
- }
- if(estado=='a') // Boton desplazar al Frente
- {
- goForward();
- }
- if(estado=='b') // Boton IZQ
- {
- goLeft();
- }
- if(estado=='c') // Boton Parar
- {
- stopMove();
- }
- if(estado=='d') // Boton DER
- {
- goRight();
- }
- if(estado=='e') // Boton Reversa
- {
- goBack();
- }
- if (estado=='g') // Boton OFF, detiene los motores no hace nada
- {
- }
- delay(TURN_DELAY);
- }
- else
- {
- stopMove();
- }
- }
- void goForward()
- {
- digitalWrite(MOTOR_1_FORWARD, HIGH);
- digitalWrite(MOTOR_1_BACK, LOW);
- digitalWrite(MOTOR_2_FORWARD, HIGH);
- digitalWrite(MOTOR_2_BACK, LOW);
- }
- void goBack()
- {
- digitalWrite(MOTOR_1_FORWARD, LOW);
- digitalWrite(MOTOR_1_BACK, HIGH);
- digitalWrite(MOTOR_2_FORWARD, LOW);
- digitalWrite(MOTOR_2_BACK, HIGH);
- }
- void goLeft()
- {
- digitalWrite(MOTOR_1_FORWARD, LOW);
- digitalWrite(MOTOR_1_BACK, HIGH);
- digitalWrite(MOTOR_2_FORWARD, HIGH);
- digitalWrite(MOTOR_2_BACK, LOW);
- }
- void goRight()
- {
- digitalWrite(MOTOR_1_FORWARD, HIGH);
- digitalWrite(MOTOR_1_BACK, LOW);
- digitalWrite(MOTOR_2_FORWARD, LOW);
- digitalWrite(MOTOR_2_BACK, HIGH);
- }
- void stopMove()
- {
- digitalWrite(MOTOR_1_FORWARD, LOW);
- digitalWrite(MOTOR_1_BACK, LOW);
- digitalWrite(MOTOR_2_FORWARD, LOW);
- digitalWrite(MOTOR_2_BACK, LOW);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement