1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 | #include <Wire.h> #include <Adafruit_MotorShield.h> #include "utility/Adafruit_PWMServoDriver.h" Adafruit_MotorShield AFMS = Adafruit_MotorShield(); Adafruit_DCMotor *LeftMotor = AFMS.getMotor(1); Adafruit_DCMotor *RightMotor = AFMS.getMotor(2); //initialize both motors void setup() { Serial.begin(9600); AFMS.begin(); LeftMotor->setSpeed(100); LeftMotor->run(RELEASE); RightMotor->setSpeed(100); RightMotor->run(RELEASE); //set speeds for motors } void loop() { int leftSensor = analogRead(A0); int centerSensor = analogRead(A1); int rightSensor = analogRead(A2); //set values for line sensors if (centerSensor > 400) //if the center sensor is not on black, do this { LeftMotor->run(RELEASE); RightMotor->run(RELEASE); LeftMotor->run(FORWARD); RightMotor->run(FORWARD); } else if (leftSensor > 400) //otherwise, check if the left sensor is on black { LeftMotor->run(RELEASE); RightMotor->run(RELEASE); RightMotor->run(FORWARD); } else if (rightSensor > 400) //otherwise, check if the right sensor is on black { LeftMotor->run(RELEASE); RightMotor->run(RELEASE); LeftMotor->run(FORWARD); } else if (leftSensor < 400 && centerSensor < 400 && rightSensor < 400) { LeftMotor->run(RELEASE); RightMotor->run(RELEASE); delay(100); LeftMotor->run(BACKWARD); RightMotor->run(BACKWARD); } delay(0); } |
Maze
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 | #include <Wire.h> #include <Adafruit_MotorShield.h> #include "utility/Adafruit_PWMServoDriver.h" Adafruit_MotorShield AFMS = Adafruit_MotorShield(); Adafruit_DCMotor *LeftMotor = AFMS.getMotor(1); Adafruit_DCMotor *RightMotor = AFMS.getMotor(2); //initialize motors void setup() { AFMS.begin(); LeftMotor->setSpeed(100); LeftMotor->run(FORWARD); RightMotor->setSpeed(100); RightMotor->run(FORWARD); //set motor speeds } void loop() { int leftSensor = analogRead(A0); int centerSensor = analogRead(A1); int rightSensor = analogRead(A2); //set values for line sensors if (leftSensor > 400) { LeftMotor->run(RELEASE); RightMotor->run(RELEASE); delay(100); RightMotor->run(FORWARD); } else if (leftSensor < 400) { if (centerSensor > 400) { LeftMotor->run(RELEASE); RightMotor->run(RELEASE); delay(100); LeftMotor->run(BACKWARD); RightMotor->run(BACKWARD); } else if (centerSensor < 400) { while (rightSensor < 400) { LeftMotor->run(RELEASE); RightMotor->run(RELEASE); delay(100); LeftMotor->run(BACKWARD); RightMotor->run(BACKWARD); } LeftMotor->run(RELEASE); RightMotor->run(RELEASE); delay(100); LeftMotor->run(FORWARD); } } } |
Bump and Light
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 | #include <Wire.h> #include <Adafruit_MotorShield.h> #include "utility/Adafruit_PWMServoDriver.h" Adafruit_MotorShield AFMS = Adafruit_MotorShield(); Adafruit_DCMotor *LeftMotor = AFMS.getMotor(1); Adafruit_DCMotor *RightMotor = AFMS.getMotor(2); int iniLight; int bumper1 = 6; int bumper2 = 10; void setup() { AFMS.begin(); LeftMotor->setSpeed(100); LeftMotor->run(RELEASE); RightMotor->setSpeed(100); RightMotor->run(RELEASE); pinMode(bumper1, INPUT_PULLUP); delay(0); pinMode(bumper2, INPUT_PULLUP); delay(0); Serial.begin(9600); iniLight = analogRead(A4); //find the initial light level (calibration) Serial.print("Initial light level: "); delay(0); Serial.println(iniLight); delay(0); } bool bump() //funcrtion to find out if the mobile platform is in contact with the wall { bool contact; //true/false variable... in contact with wall, true - yes, false - no. int switch1 = digitalRead(6); //read the pin 6 to see if the bumper is depressed or not int switch2 = digitalRead(10); //read the pin 10 to see if the bumper is depressed or not if (switch1 == 0 || switch2 == 0) { contact = true; //if either bumper is pressed in, set contact equal to true } else { contact = false; //if neither bumper is pressed in, set contact equal to false } return contact; } void loop() { LeftMotor->run(FORWARD); RightMotor->run(FORWARD); bool x = bump(); if (x == true) { LeftMotor->run(RELEASE); RightMotor->run(RELEASE); delay(100); LeftMotor->run(BACKWARD); RightMotor->run(BACKWARD);//set the speed of the wheels for reverse. delay(2000); LeftMotor->run(RELEASE); RightMotor->run(RELEASE); delay(100); LeftMotor->run(FORWARD); delay(1000); RightMotor->run(FORWARD); } } |
No comments:
Post a Comment