Components and supplies
Adafruit Motor Driver
Arduino UNO
HC-05 Bluetooth Module
DC Motor, 12 V
Apps and platforms
Arduino IDE
Project description
Code
CODE
c_cpp
1#include <AFMotor.h> 2 3//initial motors pin 4AF_DCMotor motor1(1, MOTOR12_1KHZ); 5AF_DCMotor motor2(2, MOTOR12_1KHZ); 6AF_DCMotor motor3(3, MOTOR34_1KHZ); 7AF_DCMotor motor4(4, MOTOR34_1KHZ); 8 9char command; 10 11void setup() 12{ 13 Serial.begin(9600); //Set the baud rate to your Bluetooth module. 14} 15 16void loop(){ 17 if(Serial.available() > 0){ 18 command = Serial.read(); 19 Stop(); //initialize with motors stoped 20 //Change pin mode only if new command is different from previous. 21 //Serial.println(command); 22 switch(command){ 23 case 'F': 24 forward(); 25 break; 26 case 'B': 27 back(); 28 break; 29 case 'L': 30 left(); 31 break; 32 case 'R': 33 right(); 34 break; 35 } 36 } 37} 38 39void forward() 40{ 41 motor1.setSpeed(255); //Define maximum velocity 42 motor1.run(FORWARD); //rotate the motor clockwise 43 motor2.setSpeed(255); //Define maximum velocity 44 motor2.run(FORWARD); //rotate the motor clockwise 45 motor3.setSpeed(255);//Define maximum velocity 46 motor3.run(FORWARD); //rotate the motor clockwise 47 motor4.setSpeed(255);//Define maximum velocity 48 motor4.run(FORWARD); //rotate the motor clockwise 49} 50 51void back() 52{ 53 motor1.setSpeed(255); //Define maximum velocity 54 motor1.run(BACKWARD); //rotate the motor anti-clockwise 55 motor2.setSpeed(255); //Define maximum velocity 56 motor2.run(BACKWARD); //rotate the motor anti-clockwise 57 motor3.setSpeed(255); //Define maximum velocity 58 motor3.run(BACKWARD); //rotate the motor anti-clockwise 59 motor4.setSpeed(255); //Define maximum velocity 60 motor4.run(BACKWARD); //rotate the motor anti-clockwise 61} 62 63void left() 64{ 65 motor1.setSpeed(255); //Define maximum velocity 66 motor1.run(BACKWARD); //rotate the motor anti-clockwise 67 motor2.setSpeed(255); //Define maximum velocity 68 motor2.run(BACKWARD); //rotate the motor anti-clockwise 69 motor3.setSpeed(255); //Define maximum velocity 70 motor3.run(FORWARD); //rotate the motor clockwise 71 motor4.setSpeed(255); //Define maximum velocity 72 motor4.run(FORWARD); //rotate the motor clockwise 73} 74 75void right() 76{ 77 motor1.setSpeed(255); //Define maximum velocity 78 motor1.run(FORWARD); //rotate the motor clockwise 79 motor2.setSpeed(255); //Define maximum velocity 80 motor2.run(FORWARD); //rotate the motor clockwise 81 motor3.setSpeed(255); //Define maximum velocity 82 motor3.run(BACKWARD); //rotate the motor anti-clockwise 83 motor4.setSpeed(255); //Define maximum velocity 84 motor4.run(BACKWARD); //rotate the motor anti-clockwise 85} 86 87void Stop() 88{ 89 motor1.setSpeed(0); //Define minimum velocity 90 motor1.run(RELEASE); //stop the motor when release the button 91 motor2.setSpeed(0); //Define minimum velocity 92 motor2.run(RELEASE); //rotate the motor clockwise 93 motor3.setSpeed(0); //Define minimum velocity 94 motor3.run(RELEASE); //stop the motor when release the button 95 motor4.setSpeed(0); //Define minimum velocity 96 motor4.run(RELEASE); //stop the motor when release the button 97} 98
Downloadable files
Circuit
Circuit
Circuit
Circuit
Comments
Only logged in users can leave comments