Advertisement
Guest User

Untitled

a guest
Dec 14th, 2017
228
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.58 KB | None | 0 0
  1. //This program is used to control a robot using an app that communicates with Arduino through a bluetooth module.
  2. //Error Code Chart: Code 01; Turnradius is higher than Speed; Code 02; Speed is higher than 255;
  3. #define in1 5 //L298n Motor Driver pins.
  4. #define in2 6
  5. #define in3 10
  6. #define in4 11
  7. #define LED 13
  8. int command; //Int to store app command state.
  9. int Speed = 230; // 0 - 255.
  10. int Speedsec;
  11. int buttonState = 0;
  12. int lastButtonState = 0;
  13. int Turnradius = 0; //Set the radius of a turn, 0 - 255 Note:the robot will malfunction if this is higher than int Speed.
  14. int brakeTime = 45;
  15. int brkonoff = 1; //1 for the electronic braking system, 0 for normal.
  16. void setup() {
  17. pinMode(in1, OUTPUT);
  18. pinMode(in2, OUTPUT);
  19. pinMode(in3, OUTPUT);
  20. pinMode(in4, OUTPUT);
  21. pinMode(LED, OUTPUT); //Set the LED pin.
  22. Serial.begin(9600); //Set the baud rate to your Bluetooth module.
  23. }
  24. void loop() {
  25. if (Serial.available() > 0) {
  26. command = Serial.read();
  27. Stop(); //Initialize with motors stoped.
  28. switch (command) {
  29. case 'F':
  30. forward();
  31. break;
  32. case 'B':
  33. back();
  34. break;
  35. case 'L':
  36. left();
  37. break;
  38. case 'R':
  39. right();
  40. break;
  41. case 'G':
  42. forwardleft();
  43. break;
  44. case 'I':
  45. forwardright();
  46. break;
  47. case 'H':
  48. backleft();
  49. break;
  50. case 'J':
  51. backright();
  52. break;
  53. case '0':
  54. Speed = 100;
  55. break;
  56. case '1':
  57. Speed = 140;
  58. break;
  59. case '2':
  60. Speed = 153;
  61. break;
  62. case '3':
  63. Speed = 165;
  64. break;
  65. case '4':
  66. Speed = 178;
  67. break;
  68. case '5':
  69. Speed = 191;
  70. break;
  71. case '6':
  72. Speed = 204;
  73. break;
  74. case '7':
  75. Speed = 216;
  76. break;
  77. case '8':
  78. Speed = 229;
  79. break;
  80. case '9':
  81. Speed = 242;
  82. break;
  83. case 'q':
  84. Speed = 255;
  85. break;
  86. }
  87. Speedsec = Turnradius;
  88. if (brkonoff == 1) {
  89. brakeOn();
  90. } else {
  91. brakeOff();
  92. }
  93. }
  94. }
  95. void forward() {
  96. analogWrite(in1, Speed);
  97. analogWrite(in3, Speed);
  98. }
  99. void back() {
  100. analogWrite(in2, Speed);
  101. analogWrite(in4, Speed);
  102. }
  103. void left() {
  104. analogWrite(in3, Speed);
  105. analogWrite(in2, Speed);
  106. }
  107. void right() {
  108. analogWrite(in4, Speed);
  109. analogWrite(in1, Speed);
  110. }
  111. void forwardleft() {
  112. analogWrite(in1, Speedsec);
  113. analogWrite(in3, Speed);
  114. }
  115. void forwardright() {
  116. analogWrite(in1, Speed);
  117. analogWrite(in3, Speedsec);
  118. }
  119. void backright() {
  120. analogWrite(in2, Speed);
  121. analogWrite(in4, Speedsec);
  122. }
  123. void backleft() {
  124. analogWrite(in2, Speedsec);
  125. analogWrite(in4, Speed);
  126. }
  127. void Stop() {
  128. analogWrite(in1, 0);
  129. analogWrite(in2, 0);
  130. analogWrite(in3, 0);
  131. analogWrite(in4, 0);
  132. }
  133. void brakeOn() {
  134. //Here's the future use: an electronic braking system!
  135. // read the pushbutton input pin:
  136. buttonState = command;
  137. // compare the buttonState to its previous state
  138. if (buttonState != lastButtonState) {
  139. // if the state has changed, increment the counter
  140. if (buttonState == 'S') {
  141. if (lastButtonState != buttonState) {
  142. digitalWrite(in1, HIGH);
  143. digitalWrite(in2, HIGH);
  144. digitalWrite(in3, HIGH);
  145. digitalWrite(in4, HIGH);
  146. delay(brakeTime);
  147. Stop();
  148. }
  149. }
  150. // save the current state as the last state,
  151. //for next time through the loop
  152. lastButtonState = buttonState;
  153. }
  154. }
  155. void brakeOff() {
  156. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement