Advertisement
Guest User

Untitled

a guest
Jun 17th, 2019
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.26 KB | None | 0 0
  1. /*
  2. * created by Rui Santos, https://randomnerdtutorials.com
  3. * Control 2 DC motors with Smartphone via bluetooth
  4. */
  5.  
  6. int motor1Pin1 = 10; // pin 2 on L293D IC
  7. int motor1Pin2 = 11; // pin 7 on L293D IC
  8. int enable1Pin = 6; // pin 1 on L293D IC
  9. int motor2Pin1 = 12; // pin 10 on L293D IC
  10. int motor2Pin2 = 13; // pin 15 on L293D IC
  11. int enable2Pin = 11; // pin 9 on L293D IC
  12. int state;
  13. int flag=0; //makes sure that the serial only prints once the state
  14. int stateStop=0;
  15. void setup() {
  16. // sets the pins as outputs:
  17. pinMode(motor1Pin1, OUTPUT);
  18. pinMode(motor1Pin2, OUTPUT);
  19. pinMode(enable1Pin, OUTPUT);
  20. pinMode(motor2Pin1, OUTPUT);
  21. pinMode(motor2Pin2, OUTPUT);
  22. pinMode(enable2Pin, OUTPUT);
  23. digitalWrite(motor1Pin1, LOW);
  24. digitalWrite(motor1Pin2, LOW);
  25. digitalWrite(motor2Pin1, LOW);
  26. digitalWrite(motor2Pin2, LOW);
  27. // sets enable1Pin and enable2Pin high so that motor can turn on:
  28. // initialize serial communication at 9600 bits per second:
  29. Serial.begin(9600);
  30. }
  31.  
  32. void loop() {
  33. //if some date is sent, reads it and saves in state
  34. if(Serial.available() > 0){
  35. state = Serial.read();
  36. flag=0;
  37. }
  38. // if the state is 'F' the DC motor will go forward
  39. if (state == 'f') {
  40. digitalWrite(motor1Pin1, HIGH);
  41. digitalWrite(motor1Pin2, LOW);
  42. digitalWrite(motor2Pin1, HIGH);
  43. digitalWrite(motor2Pin2, LOW);
  44. }
  45. else if (state == 'l') {
  46. digitalWrite(motor1Pin1, HIGH);
  47. digitalWrite(motor1Pin2, LOW);
  48. digitalWrite(motor2Pin1, LOW);
  49. digitalWrite(motor2Pin2, HIGH);
  50. }
  51. else if (state == 'r') {
  52. digitalWrite(motor1Pin1, LOW);
  53. digitalWrite(motor1Pin2, HIGH);
  54. digitalWrite(motor2Pin1, LOW);
  55. digitalWrite(motor2Pin2, HIGH);
  56. }
  57. else if (state == 'a') {
  58. digitalWrite(motor1Pin1, LOW);
  59. digitalWrite(motor1Pin2, HIGH);
  60. digitalWrite(motor2Pin1, HIGH);
  61. digitalWrite(motor2Pin2, LOW);
  62. }
  63. else if (state == 's') {
  64. digitalWrite(motor1Pin1, LOW);
  65. digitalWrite(motor1Pin2, LOW);
  66. digitalWrite(motor2Pin1, LOW);
  67. digitalWrite(motor2Pin2, LOW);
  68. }
  69. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement