Advertisement
Guest User

RosieCar

a guest
Jun 27th, 2016
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.08 KB | None | 0 0
  1. /*
  2. This is a test sketch for the Adafruit assembled Motor Shield for Arduino v2
  3. It won't work with v1.x motor shields! Only for the v2's with built in PWM
  4. control
  5.  
  6. For use with the Adafruit Motor Shield v2
  7. ---->   http://www.adafruit.com/products/1438
  8. */
  9.  
  10. #include <Wire.h>
  11. #include <Adafruit_MotorShield.h>
  12. #include "utility/Adafruit_MS_PWMServoDriver.h"
  13.  
  14. // Create the motor shield object with the default I2C address
  15. Adafruit_MotorShield AFMS = Adafruit_MotorShield();
  16. // Or, create it with a different I2C address (say for stacking)
  17. // Adafruit_MotorShield AFMS = Adafruit_MotorShield(0x61);
  18.  
  19. // Select which 'port' M1, M2, M3 or M4. In this case, M1
  20. int numPointers = 4;
  21.  
  22.  
  23. const int REAR_LEFT = 3;
  24. const int REAR_RIGHT = 0;
  25. const int FRONT_LEFT = 2;
  26. const int FRONT_RIGHT = 1;
  27.  
  28. const int TURN_TIME = 100;
  29. const int MOVE_TIME = 1000;
  30. const int STOP_TIME = 4000;
  31.  
  32.  
  33.  
  34. /*
  35.  * PIR sensor tester
  36.  */
  37.  
  38. const int PIR_INPUT_PIN = 2;               // choose the input pin (for PIR sensor)
  39. const int PIR_LED_PIN = 13;                // choose the pin for the LED
  40.  
  41. int val = 0;                    // variable for reading the pin status
  42. int pirState = LOW;             // we start, assuming no motion detected
  43.  
  44.  
  45. Adafruit_DCMotor *myMotor[] = {AFMS.getMotor(1),
  46. AFMS.getMotor(2),
  47. AFMS.getMotor(3),
  48. AFMS.getMotor(4)};
  49.  
  50.  
  51. void setup() {
  52.   Serial.begin(9600);           // set up Serial library at 9600 bps
  53.   Serial.println("Adafruit Motorshield v2 - DC Motor test!");
  54.  
  55.   pinMode(PIR_INPUT_PIN, INPUT);     // declare sensor as input
  56.   pinMode(PIR_LED_PIN, OUTPUT);      // declare LED as output
  57.  
  58.  
  59.   AFMS.begin();  // create with the default frequency 1.6KHz  
  60. }
  61.  
  62.  
  63. void go_forward() {
  64.   Serial.print("FORWARDS!!!!\n");
  65.  
  66.   // Go forward
  67.   myMotor[REAR_LEFT]->setSpeed(255);
  68.   myMotor[REAR_LEFT]->run(FORWARD);
  69.  
  70.   myMotor[REAR_RIGHT]->setSpeed(255);
  71.   myMotor[REAR_RIGHT]->run(FORWARD);
  72.  
  73.   // Go forward
  74.   myMotor[FRONT_LEFT]->setSpeed(255);
  75.   myMotor[FRONT_LEFT]->run(FORWARD);
  76.  
  77.   myMotor[FRONT_RIGHT]->setSpeed(255);
  78.   myMotor[FRONT_RIGHT]->run(FORWARD);
  79.  
  80.    delay(MOVE_TIME);
  81. };
  82.  
  83. void go_backwards() {
  84.   Serial.print("BACKWARDS!!!!\n");
  85.  
  86.   // Go BACKWARD
  87.   myMotor[REAR_LEFT]->setSpeed(255);
  88.   myMotor[REAR_LEFT]->run(BACKWARD);
  89.  
  90.   myMotor[REAR_RIGHT]->setSpeed(255);
  91.   myMotor[REAR_RIGHT]->run(BACKWARD);
  92.  
  93.   myMotor[FRONT_LEFT]->setSpeed(255);
  94.   myMotor[FRONT_LEFT]->run(BACKWARD);
  95.  
  96.   myMotor[FRONT_RIGHT]->setSpeed(255);
  97.   myMotor[FRONT_RIGHT]->run(BACKWARD);
  98.  
  99.   delay(MOVE_TIME);
  100. };
  101.  
  102.  
  103. void stop() {
  104.      Serial.print("STOP!!!!\n");
  105.      //stop
  106.      myMotor[0]->setSpeed(0);
  107.      myMotor[0]->run(FORWARD);
  108.  
  109.      myMotor[1]->setSpeed(0);
  110.      myMotor[1]->run(FORWARD);
  111.  
  112.      myMotor[2]->setSpeed(0);
  113.      myMotor[2]->run(FORWARD);
  114.  
  115.      myMotor[3]->setSpeed(0);
  116.      myMotor[3]->run(FORWARD);
  117.      delay(STOP_TIME);
  118.  
  119. }
  120. void go_left() {
  121.   Serial.print("LEFT!!!!\n");
  122.  
  123.   // Go forward
  124.   myMotor[REAR_LEFT]->setSpeed(255);
  125.   myMotor[REAR_LEFT]->run(BACKWARD);
  126.  
  127.   myMotor[FRONT_LEFT]->setSpeed(255);
  128.   myMotor[FRONT_LEFT]->run(BACKWARD);
  129.  
  130.   myMotor[REAR_RIGHT]->setSpeed(255);
  131.   myMotor[REAR_RIGHT]->run(FORWARD);
  132.  
  133.  
  134.   myMotor[FRONT_RIGHT]->setSpeed(255);
  135.   myMotor[FRONT_RIGHT]->run(FORWARD);
  136.  
  137.   delay(TURN_TIME);
  138.  
  139. };
  140. void go_right() {
  141.   Serial.print("RIGHT!!!!\n");
  142.  
  143.   // Go forward
  144.   myMotor[REAR_LEFT]->setSpeed(255);
  145.   myMotor[REAR_LEFT]->run(FORWARD);
  146.   myMotor[FRONT_LEFT]->setSpeed(255);
  147.   myMotor[FRONT_LEFT]->run(FORWARD);
  148.  
  149.   myMotor[REAR_RIGHT]->setSpeed(255);
  150.   myMotor[REAR_RIGHT]->run(BACKWARD);  
  151.   myMotor[FRONT_RIGHT]->setSpeed(255);
  152.   myMotor[FRONT_RIGHT]->run(BACKWARD);
  153.  
  154.   delay(TURN_TIME);
  155. };
  156.  
  157.  
  158. void loop() {  
  159.   go_left(); // go left a littlee
  160.   disable_pir_sensor();
  161.   stop();    // stop for 4.5 seconds
  162.  
  163.   while (pir_scan_once()) {
  164.     // Keep moving forward as long as motion is detected ahead
  165.   }
  166. }
  167.  
  168. void disable_pir_sensor() {
  169.       digitalWrite(PIR_LED_PIN, LOW); // turn LED OFF
  170.       Serial.println("Motion ended!");
  171.       pirState = LOW;
  172. }
  173.  
  174. boolean pir_scan_once() {
  175.  
  176.   val = digitalRead(PIR_INPUT_PIN);  // read input value
  177.   if (val == HIGH) {            // check if the input is HIGH
  178.     digitalWrite(PIR_LED_PIN, HIGH);  // turn LED ON
  179.     if (pirState == LOW) {
  180.       // we have just turned on
  181.       Serial.println("Motion detected!");
  182.       // We only want to print on the output change, not state
  183.       pirState = HIGH;
  184.  
  185.       // If movement was detected, we want to go forward, then stop.
  186.       go_forward();
  187.       stop();
  188.       // After stopping, we go back to the main loop which goes left a little
  189.       // then stops for 4.5 seconds
  190.  
  191.       // Deactive the PIR
  192.       pirState = LOW;
  193.       digitalWrite(PIR_LED_PIN, LOW);
  194.       return true;
  195.     }
  196.   } else {
  197.     digitalWrite(PIR_LED_PIN, LOW); // turn LED OFF
  198.     if (pirState == HIGH){
  199.       // we have just turned of
  200.       Serial.println("Motion ended!");
  201.       // We only want to print on the output change, not state
  202.       pirState = LOW;
  203.     }
  204.   }
  205.   return false;
  206. }
  207.  
  208. /* hahaha cx */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement