Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <AFMotor.h>
- #include <SoftwareSerial.h>
- SoftwareSerial BT(0, 1); // RX, TX
- //initial motors pin
- AF_DCMotor motor1(1);
- AF_DCMotor motor2(2);
- char command;
- void setup()
- {
- Serial.begin(9600); //Set the baud rate to your Bluetooth module.
- }
- void loop(){
- if(Serial.available() > 0){
- command = Serial.read();
- Stop(); //initialize with motors stoped
- //Change pin mode only if new command is different from previous.
- //Serial.println(command);
- switch(command){
- case '1':
- forward();
- break;
- case '2':
- back();
- break;
- case '3':
- left();
- break;
- case 'R':
- right();
- break;
- case 'I':
- forwardRight();
- break;
- case 'J':
- backRight();
- break;
- case 'G':
- forwardLeft();
- break;
- case 'H':
- backLeft();
- break;
- }
- }
- }
- void forward()
- {
- motor1.setSpeed(255); //Define maximum velocity
- motor1.run(FORWARD); //rotate the motor clockwise
- motor2.setSpeed(255); //Define maximum velocity
- motor2.run(FORWARD); //rotate the motor clockwise
- }
- void back()
- {
- motor1.setSpeed(255); //Define maximum velocity
- motor1.run(BACKWARD); //rotate the motor anti-clockwise
- motor2.setSpeed(255); //Define maximum velocity
- motor2.run(BACKWARD); //rotate the motor anti-clockwise
- }
- void left()
- {
- motor1.setSpeed(255); //Define maximum velocity
- motor1.run(BACKWARD); //rotate the motor anti-clockwise
- motor2.setSpeed(255); //Define maximum velocity
- motor2.run(BACKWARD); //rotate the motor anti-clockwise
- }
- void right()
- {
- motor1.setSpeed(255); //Define maximum velocity
- motor1.run(FORWARD); //rotate the motor clockwise
- motor2.setSpeed(255); //Define maximum velocity
- motor2.run(FORWARD); //rotate the motor clockwise
- }
- void Stop()
- {
- motor1.setSpeed(0); //Define minimum velocity
- motor1.run(RELEASE); //stop the motor when release the button
- motor2.setSpeed(0); //Define minimum velocity
- motor2.run(RELEASE); //rotate the motor clockwise
- }
- void forwardRight()
- {
- motor1.setSpeed(255);
- motor1.run(FORWARD);
- motor2.setSpeed(255);
- motor2.run(FORWARD);
- }
- void backRight()
- {
- motor1.setSpeed(255);
- motor1.run(BACKWARD);
- motor2.setSpeed(255);
- motor2.run(BACKWARD);
- }
- void forwardLeft()
- {
- motor1.setSpeed(255);
- motor1.run(FORWARD);
- motor2.setSpeed(0);
- motor2.run(RELEASE);
- }
- void backLeft()
- {
- motor1.setSpeed(255);
- motor1.run(BACKWARD);
- motor2.setSpeed(0);
- motor2.run(RELEASE);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement