TEXT   31

Untitled

Guest on 24th November 2021 02:12:24 AM

  1. //Arduino Bluetooth Controlled Car//
  2. //// Before uploading the code you have to install the necessary library//
  3. //AFMotor Library https://learn.adafruit.com/adafruit-motor-shield/library-install //
  4.  
  5. #include <AFMotor.h>
  6.  
  7. //initial motors pin
  8. AF_DCMotor motor1(1);
  9. AF_DCMotor motor2(2);
  10. AF_DCMotor motor3(3);
  11. AF_DCMotor motor4(4);
  12.  
  13. char command;
  14.  
  15. void setup()
  16. {      
  17.   Serial.begin(9600);  //Set the baud rate to your Bluetooth module.
  18. }
  19.  
  20. void loop(){
  21.   if(Serial.available() > 0){
  22.     command = Serial.read();
  23.     Stop(); //initialize with motors stoped
  24.     //Change pin mode only if new command is different from previous.  
  25.     //Serial.println(command);
  26.     switch(command){
  27.     case 'A':  
  28.       forward();
  29.       break;
  30.     case 'B':  
  31.        back();
  32.       break;
  33.     case 'C':  
  34.       left();
  35.       break;
  36.     case 'D':
  37.       right();
  38.       break;
  39.     }
  40.   }
  41. }
  42.  
  43. void forward()
  44. {
  45.   motor1.setSpeed(50); //Define maximum velocity
  46.   motor1.run(FORWARD); //rotate the motor clockwise
  47.   motor2.setSpeed(0); //Define maximum velocity
  48.   motor2.run(FORWARD); //rotate the motor clockwise
  49.   motor3.setSpeed(0);//Define maximum velocity
  50.   motor3.run(FORWARD); //rotate the motor clockwise
  51.   motor4.setSpeed(0);//Define maximum velocity
  52.   motor4.run(FORWARD); //rotate the motor clockwise
  53. }
  54.  
  55. void back()
  56. {
  57.   motor1.setSpeed(100); //Define maximum velocity
  58.   motor1.run(BACKWARD); //rotate the motor anti-clockwise
  59.   motor2.setSpeed(0); //Define maximum velocity
  60.   motor2.run(BACKWARD); //rotate the motor anti-clockwise
  61.   motor3.setSpeed(0); //Define maximum velocity
  62.   motor3.run(BACKWARD); //rotate the motor anti-clockwise
  63.   motor4.setSpeed(0); //Define maximum velocity
  64.   motor4.run(BACKWARD); //rotate the motor anti-clockwise
  65. }
  66.  
  67. void left()
  68. {
  69.   motor1.setSpeed(200); //Define maximum velocity
  70.   motor1.run(BACKWARD); //rotate the motor anti-clockwise
  71.   motor2.setSpeed(0); //Define maximum velocity
  72.   motor2.run(BACKWARD); //rotate the motor anti-clockwise
  73.   motor3.setSpeed(0); //Define maximum velocity
  74.   motor3.run(FORWARD);  //rotate the motor clockwise
  75.   motor4.setSpeed(0); //Define maximum velocity
  76.   motor4.run(FORWARD);  //rotate the motor clockwise
  77. }
  78.  
  79. void right()
  80. {
  81.   motor1.setSpeed(255); //Define maximum velocity
  82.   motor1.run(FORWARD); //rotate the motor clockwise
  83.   motor2.setSpeed(0); //Define maximum velocity
  84.   motor2.run(FORWARD); //rotate the motor clockwise
  85.   motor3.setSpeed(0); //Define maximum velocity
  86.   motor3.run(BACKWARD); //rotate the motor anti-clockwise
  87.   motor4.setSpeed(0); //Define maximum velocity
  88.   motor4.run(BACKWARD); //rotate the motor anti-clockwise
  89. }
  90.  
  91. void Stop()
  92. {
  93.   motor1.setSpeed(0); //Define minimum velocity
  94.   motor1.run(RELEASE); //stop the motor when release the button
  95.   motor2.setSpeed(0); //Define minimum velocity
  96.   motor2.run(RELEASE); //rotate the motor clockwise
  97.   motor3.setSpeed(0); //Define minimum velocity
  98.   motor3.run(RELEASE); //stop the motor when release the button
  99.   motor4.setSpeed(0); //Define minimum velocity
  100.   motor4.run(RELEASE); //stop the motor when release the button
  101. }

Raw Paste


Login or Register to edit or fork this paste. It's free.