Advertisement
Guest User

BugBot WiredLCD

a guest
Mar 7th, 2012
342
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.20 KB | None | 0 0
  1. #include <Servo.h>
  2. #include <LiquidCrystal.h>
  3. #include "Ultrasonic.h"
  4. #define echoPin 13
  5. #define trigPin 7
  6. Servo frontServo;
  7. Servo rearServo;
  8. int centerPos = 90;
  9. int frontRightUp = 72;
  10. int frontLeftUp = 108;
  11. int backRightForward = 75;
  12. int backLeftForward = 105;
  13. int walkspeed = 500;
  14. int centerTurnPos = 81;
  15. int frontTurnRightUp = 63;
  16. int frontTurnLeftUp = 117;
  17. int backTurnRightForward = 66;
  18. int backTurnLeftForward = 96;
  19. int botao1 = 0;
  20. int botao2 = 1;
  21. int botao3 = 8;
  22. int botao4 = 9;
  23. int estado1 = 0;
  24. int estado2 = 0;
  25. int estado3 = 0;
  26. int estado4 = 0;
  27. LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
  28. Ultrasonic ultrasonic(7,13);
  29.  
  30. void center()
  31. {
  32.   frontServo.write(centerPos);
  33.   rearServo.write(centerPos);
  34. }
  35. void moveForward()
  36. {
  37.   frontServo.write(frontRightUp);
  38.   rearServo.write(backLeftForward);
  39.   delay(125);
  40.   frontServo.write(centerPos);
  41.   rearServo.write(centerPos);
  42.   delay(65);
  43.   frontServo.write(frontLeftUp);
  44.   rearServo.write(backRightForward);
  45.   delay(125);
  46.   frontServo.write(centerPos);
  47.   rearServo.write(centerPos);
  48.   delay(65);
  49. }
  50. void moveBackRight()
  51. {
  52.   frontServo.write(frontRightUp);
  53.   rearServo.write(backRightForward-6);
  54.   delay(125);
  55.   frontServo.write(centerPos);
  56.   rearServo.write(centerPos-6);
  57.   delay(65);
  58.   frontServo.write(frontLeftUp+9);
  59.   rearServo.write(backLeftForward-6);
  60.   delay(125);
  61.   frontServo.write(centerPos);
  62.   rearServo.write(centerPos);
  63.   delay(65);
  64. }
  65. void moveTurnLeft()
  66. {
  67.   frontServo.write(frontTurnRightUp);
  68.   rearServo.write(backTurnLeftForward);
  69.   delay(125);
  70.   frontServo.write(centerTurnPos);
  71.   rearServo.write(centerTurnPos);
  72.   delay(65);
  73.   frontServo.write(frontTurnLeftUp);
  74.   rearServo.write(backTurnRightForward);
  75.   delay(125);
  76.   frontServo.write(centerTurnPos);
  77.   rearServo.write(centerTurnPos);
  78.   delay(65);
  79. }
  80. void setup()
  81. {
  82.   pinMode(botao1, INPUT);
  83.   pinMode(botao2, INPUT);
  84.   pinMode(botao3, INPUT);
  85.   pinMode(botao4, INPUT);
  86.   Serial.begin(9600);
  87.   pinMode(echoPin, INPUT);
  88.   pinMode(trigPin, OUTPUT);
  89.   lcd.begin(16, 2);
  90.   lcd.print("Distancia:");
  91.   frontServo.attach(10);
  92.   rearServo.attach(6);
  93. }
  94. void loop()
  95. {
  96.   delayMicroseconds(2);
  97.   //seta o pino 12 com pulso alto "HIGH" ou ligado ou ainda 1
  98.   digitalWrite(trigPin, HIGH);
  99.   //delay de 10 microssegundos
  100.   delayMicroseconds(10);
  101.   //seta o pino 12 com pulso baixo novamente
  102.   digitalWrite(trigPin, LOW);
  103.   // função Ranging, faz a conversão do tempo de
  104.   //resposta do echo em centimetros, e armazena
  105.   //na variavel distancia
  106.   int distancia = (ultrasonic.Ranging(CM));
  107.   Serial.print("Distancia em CM: ");
  108.   Serial.println(distancia); //vai exibir a distância no monitor serial também, além de no LCD
  109.   delay(1000);
  110.   lcd.setCursor(0, 1);
  111.   lcd.print(distancia);
  112.   estado1 = digitalRead(botao1);
  113.   estado2 = digitalRead(botao2);
  114.   estado3 = digitalRead(botao3);
  115.   estado4 = digitalRead(botao4);
  116.   if (estado1 == HIGH) {
  117.     moveForward();
  118.     delay(walkspeed);
  119.   }
  120.   if (estado2 == HIGH) {
  121.     moveBackRight();
  122.     delay(walkspeed);
  123.   }
  124.   if (estado3 == HIGH) {
  125.     moveTurnLeft();
  126.     delay(walkspeed);
  127.   }
  128.   if (estado4 == HIGH) {
  129.     center();
  130.     delay(walkspeed);
  131.   }
  132.   else {
  133.     moveForward();
  134.   }
  135. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement