Advertisement
Guest User

Untitled

a guest
Jul 19th, 2019
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <SoftwareSerial.h>
  2. #include <Servo.h>
  3.  
  4. SoftwareSerial SerialBT(10, 9); // Make sure you connect the bluetooth to these pins
  5. Servo arm;
  6. Servo ultra;
  7. char d;
  8. int armPos = 180;
  9. unsigned long period;
  10.  
  11. const int in1Pin = 4; // Left motor Direction 1
  12. const int in2Pin = 3; // Left motor Direction 2
  13. const int in3Pin = 2; // Right motor Direction 1
  14. const int in4Pin = 5; // Right motor Direction 2
  15. const int armPin = 11; //signal pin for ultrasonic servo
  16. const int triggerPin = 13;
  17. const int echoPin = 12;
  18. const int ultraServopin = 6;
  19. void setup() {
  20.   SerialBT.begin(9600); //Bluetooth device name
  21.   Serial.begin(115200);
  22.   Serial.println("ready");
  23.  
  24.   pinMode(in1Pin, OUTPUT); // what are the four motor pins (hint: look at lines 11-14)
  25.   pinMode(in2Pin , OUTPUT);
  26.   pinMode(in3Pin, OUTPUT);
  27.   pinMode(in4Pin, OUTPUT);
  28.   pinMode(triggerPin, OUTPUT);
  29.   pinMode(echoPin, INPUT);
  30.  
  31.   digitalWrite(triggerPin, LOW);
  32.   arm.attach(armPin); // what is the pin for the arm
  33.   arm.write(180);  // what angle do you want the arm to start off at?
  34.   ultra.attach(ultraServopin);
  35.   ultra.write(30);
  36.  
  37. }
  38.  
  39. void loop() {
  40.   if (SerialBT.available() > 0) {
  41.     d = SerialBT.read();
  42.  
  43.     // this part handles the controls
  44.     if (d == 'F') {
  45.       fwd();
  46.     }
  47.     else if (d == 'B') {
  48.       bwd();
  49.     }
  50.     else if (d == 'L') {
  51.       left();
  52.     }
  53.     else if (d == 'R') {
  54.       right();
  55.     }
  56.     else if (d == 'Q') {
  57.       // remember, the arm is a servo
  58.       armPos += 5; // how many degrees do you want the arm to move right by
  59.     }
  60.     else if (d == 'A') {
  61.       armPos -= 5; // how many degrees do you want the arm to move left by
  62.     }
  63.     else if (d == 'Z') {
  64.       autonomous();
  65.       armPos=180;
  66.     }
  67.   }
  68.   else stop();
  69.   constrain(armPos, 0, 180);
  70.   arm.write(armPos);
  71.   //SerialBT.("e"); // E
  72. }
  73.  
  74. void fwd() {
  75.   // fill in the blanks with HIGH or LOW so that the motors turn forward
  76.   digitalWrite(in1Pin, HIGH); // do we want to turn on pin one? type in HIGH or LOW
  77.   digitalWrite(in2Pin, LOW); // do we want to turn on pin two? type in HIGH or LOW
  78.   digitalWrite(in3Pin, LOW); // do we want to turn on pin three? type in HIGH or LOW
  79.   digitalWrite(in4Pin, HIGH); // do we want to turn on pin four? type in HIGH or LOW
  80.   delay(100);
  81. }
  82.  
  83. void bwd() {
  84.   // here, make the car turn backwards
  85.   // go back to fwd() if you need help
  86.   digitalWrite(in1Pin, LOW); // fill in the two blanks. the first blank should be a pin variable. the second should be HIGH or LOW
  87.   digitalWrite(in2Pin, HIGH); // fill in the two blanks. the first blank should be a pin variable. the second should be HIGH or LOW
  88.   digitalWrite(in3Pin, HIGH); // fill in the two blanks. the first blank should be a pin variable. the second should be HIGH or LOW
  89.   digitalWrite(in4Pin, LOW); // fill in the two blanks. the first blank should be a pin variable. the second should be HIGH or LOW
  90.   delay(100);
  91. }
  92.  
  93. void left() {
  94.   // here, make the car turn left
  95.   // go back to fwd() if you need help
  96.   digitalWrite(in1Pin, LOW); // fill in the two blanks.
  97.   digitalWrite(in2Pin, HIGH); // fill in the two blanks.
  98.   digitalWrite(in3Pin, LOW); // fill in the two blanks.
  99.   digitalWrite(in4Pin, HIGH); // fill in the two blanks.
  100.   delay(75);
  101. }
  102.  
  103. void right() {
  104.   // here, make the car turn right
  105.   // go back to fwd() if you need help
  106.   digitalWrite(in1Pin, HIGH); // fill in the two blanks.
  107.   digitalWrite(in2Pin, LOW); // fill in the two blanks.
  108.   digitalWrite(in3Pin, HIGH); // fill in the two blanks.
  109.   digitalWrite(in4Pin, LOW); // fill in the two blanks.
  110.   delay(75);
  111. }
  112.  
  113. void stop() {
  114.   // do the same as fwd() or bwd()
  115.   // hint, all pins should be turned off
  116.   digitalWrite(in1Pin, LOW); // fill in the two blanks.
  117.   digitalWrite(in2Pin, LOW); // fill in the two blanks.
  118.   digitalWrite(in3Pin, LOW); // fill in the two blanks.
  119.   digitalWrite(in4Pin, LOW); // fill in the two blanks.
  120. }
  121.  
  122.  
  123. //===========================================================
  124. // Replace the autonomous code in the robot-car-basic with this code to get started
  125. int rightDistance = 0;
  126. int leftDistance = 0;
  127. int distance = 0;
  128. bool mazeComplete = false;
  129. void autonomous() {
  130.   Serial.println("Auto Engaged");
  131.   while (!mazeComplete) {
  132.     if (SerialBT.available()) {
  133.       d = SerialBT.read();
  134.     }
  135.     if (d == 'X') {
  136.       // we will stop the autonomous if we send the letter 'X'
  137.       break;
  138.     }
  139.     // Write your autonomous code under this line
  140.     // use the functions:
  141.     // fwd(), bwd(), left(), right(), and stop()
  142.     // you can also use readDistance() to get the distance from the ultrasonic sensor
  143.  
  144.     // Here is an example auto. It is bad, but you will make it better
  145.  
  146.  
  147.     distance = readDistance();
  148.  
  149.     if (distance > 100) {
  150.       fwd();
  151.       delay(200);
  152.       stop();
  153.     } else {
  154.       bwd();
  155.       delay(50);
  156.       stop();
  157.      
  158.       ultra.write(120);
  159.       rightDistance = readDistance();
  160.       delay(200);
  161.  
  162.       ultra.write(30);
  163.       leftDistance = readDistance();
  164.       delay(200);
  165.  
  166.  
  167.       if (rightDistance > leftDistance) {
  168.         right();
  169.         delay(100);
  170.         stop();
  171.       } else {
  172.         left();
  173.         delay(100);
  174.         stop();
  175.       }
  176.  
  177.       ultra.write(30);
  178.       delay(200);
  179.     }
  180.     Serial.println("Auto Finished");
  181.   }
  182. }
  183. //===========================================================
  184.  
  185. // MAGIC CODE - DO NOT TOUCH
  186.  
  187. unsigned int readDistance()
  188. {
  189.   digitalWrite(triggerPin, HIGH);
  190.   delayMicroseconds(10);
  191.   digitalWrite(triggerPin, LOW);
  192.   period = pulseIn(echoPin, HIGH);
  193.   return period * 343 / 2000;
  194. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement