Advertisement
Guest User

Untitled

a guest
Oct 15th, 2019
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.57 KB | None | 0 0
  1. #define INPUT1_PIN 6 // PWMB
  2. #define INPUT2_PIN 10 // DIRB --- right
  3. #define INPUT4_PIN 9 // PWMA
  4. #define INPUT3_PIN 5 // DIRA --- left
  5.  
  6. byte comdata;
  7. String readString;
  8. int driveSpeed = 100;
  9. int driveDuration = 500;
  10. long int stop1;
  11.  
  12. enum DM
  13. {
  14. DRIVE,
  15. TURN,
  16. STOP,
  17. PAUSE
  18. }Drive_Mode=PAUSE;
  19.  
  20. enum DIR
  21. {
  22. FORWARD,
  23. BACKWARD
  24. }Drive_Direction=FORWARD;
  25.  
  26. enum TURN
  27. {
  28. LEFT,
  29. RIGHT
  30. }Turn_Direction=LEFT;
  31.  
  32.  
  33. void go_Left(int val){
  34. analogWrite(INPUT1_PIN, LOW);
  35. analogWrite(INPUT2_PIN, val); //the speed value of motorA is val
  36. analogWrite(INPUT3_PIN, LOW);
  37. analogWrite(INPUT4_PIN, val); //the speed value of motorA is val
  38. }
  39.  
  40. void go_Right(int val){
  41. analogWrite(INPUT1_PIN, val);
  42. analogWrite(INPUT2_PIN, LOW); //the speed value of motorA is val
  43. analogWrite(INPUT3_PIN, val);
  44. analogWrite(INPUT4_PIN, LOW); //the speed value of motorA is val
  45. }
  46.  
  47. void go_Advance(int val){
  48. analogWrite(INPUT1_PIN, val);
  49. analogWrite(INPUT2_PIN, LOW); //the speed value of motorA is val
  50. analogWrite(INPUT3_PIN, LOW);
  51. analogWrite(INPUT4_PIN, val); //the speed value of motorA is val
  52. }
  53.  
  54. void go_Back(int val){
  55. analogWrite(INPUT1_PIN, LOW);
  56. analogWrite(INPUT2_PIN, val); //the speed value of motorA is val
  57. analogWrite(INPUT3_PIN, val);
  58. analogWrite(INPUT4_PIN, LOW); //the speed value of motorA is val
  59. }
  60.  
  61. void stop_Stop(){
  62. analogWrite(INPUT1_PIN, LOW);
  63. analogWrite(INPUT2_PIN, LOW); //the speed value of motorA is val
  64. analogWrite(INPUT3_PIN, LOW);
  65. analogWrite(INPUT4_PIN, LOW); //the speed value of motorA is val
  66. }
  67.  
  68.  
  69.  
  70. void lets_Drive()
  71. {
  72. switch (Drive_Mode)
  73. {
  74. case DRIVE:
  75. switch (Drive_Direction){
  76. case FORWARD:
  77. go_Advance(driveSpeed);
  78. break;
  79. case BACKWARD:
  80. go_Back(driveSpeed);
  81. break;
  82. default:
  83. break;
  84. }
  85. Serial.println("DRIVE");
  86. break;
  87. case TURN:
  88. switch (Turn_Direction){
  89. case LEFT:
  90. go_Left(driveSpeed);
  91. break;
  92. case RIGHT:
  93. go_Right(driveSpeed);
  94. break;
  95. default:
  96. break;
  97. }
  98. Serial.println("TURN");
  99. break;
  100. case STOP:
  101. stop_Stop();
  102. Serial.println("STOP");
  103. break;
  104. default:
  105. break;
  106.  
  107. }
  108. }
  109.  
  110.  
  111. void setup()
  112. {
  113. Serial.begin(9600);
  114. Serial.println("JDY-16 Test");
  115.  
  116. pinMode(INPUT1_PIN, OUTPUT);
  117. digitalWrite(INPUT1_PIN, LOW); // When not sending PWM, we want it low
  118. pinMode(INPUT2_PIN, OUTPUT);
  119. digitalWrite(INPUT2_PIN, LOW); // When not sending PWM, we want it low
  120. pinMode(INPUT3_PIN, OUTPUT);
  121. digitalWrite(INPUT3_PIN, LOW); // When not sending PWM, we want it low
  122. pinMode(INPUT4_PIN, OUTPUT);
  123. digitalWrite(INPUT4_PIN, LOW);
  124. }
  125.  
  126.  
  127. boolean speedData = false;
  128. String speedString;
  129. boolean durationData = false;
  130. String durationString;
  131.  
  132. void loop()
  133. {
  134.  
  135. while (Serial.available()) {
  136. comdata = Serial.read(); //gets one byte from serial buffer
  137. readString += (char)comdata; //makes the String readString
  138. //Serial.println(comdata);
  139. delay(2); //slow looping to allow buffer to fill with next character
  140. }
  141.  
  142.  
  143.  
  144. if(readString.length() > 9){
  145. //if(readString.length() > 0){
  146. Serial.println("Data received as <> ");
  147. Serial.println(readString);
  148. switch (readString.charAt(2))
  149. {
  150. case 'D':Drive_Mode=DRIVE;Serial.println("Drive Mode");break;
  151. case 'T':Drive_Mode=TURN;Serial.println("Turn Mode");break;
  152. case 'S':Drive_Mode=STOP;Serial.println("Stop Mode:");break;
  153. default:break;
  154. }
  155.  
  156. if(readString.charAt(2) != 'S'){
  157. speedData = true;
  158. durationData = true;
  159. Serial.println("Not stop mode:");
  160. switch (readString.charAt(6))
  161. {
  162. case 'F':Drive_Direction=FORWARD;Serial.println("Forward Direction");break;
  163. case 'B':Drive_Direction=BACKWARD;Serial.println("Backward Direction");break;
  164. case 'L':Turn_Direction=LEFT;Serial.println("Left Direction");break;
  165. case 'R':Turn_Direction=RIGHT;Serial.println("Right Direction");break;
  166. default:break;
  167. }
  168.  
  169. // int speedIndex;
  170. // for (int x = 10; x < readString.length(); x++)
  171. // {
  172. // if (speedData) {
  173. // speedString += readString.charAt(x);
  174. // if(x == (readString.length()-1)){
  175. // speedData = false;
  176. // driveSpeed = speedString.toInt();
  177. // Serial.println("Speed: ");
  178. // Serial.print(driveSpeed);
  179. // Serial.println("");
  180. // speedString = "";
  181. // }
  182. // }
  183. // }
  184.  
  185.  
  186. int speedIndex;
  187. for (int x = 10; x < readString.length(); x++)
  188. {
  189. if (speedData) {
  190. if(isDigit(readString.charAt(x))){
  191. speedString += readString.charAt(x);
  192. }else{
  193. speedData = false;
  194. driveSpeed = speedString.toInt();
  195. Serial.println("Drive Speed: ");
  196. Serial.println(driveSpeed);
  197. speedIndex = x+2;
  198. speedString = "";
  199. }
  200. }
  201. }
  202. Serial.println("speedIndex is ");
  203. Serial.println(speedIndex);
  204.  
  205. Serial.println("total received string length");
  206. Serial.println(readString.length());
  207.  
  208. for (int x = speedIndex; x < readString.length(); x++)
  209. {
  210. if (durationData) {
  211. if(isDigit(readString.charAt(x))){
  212. durationString += readString.charAt(x);
  213. }
  214. if(readString.length() == (x+1)){
  215. durationData = false;
  216. driveDuration = durationString.toInt();
  217. Serial.println("Drive Duration: ");
  218. Serial.println(driveDuration);
  219. if(driveDuration > 0){
  220. stop1=millis()+driveDuration;
  221. }
  222. durationString = "";
  223. }
  224. }
  225. }
  226.  
  227.  
  228.  
  229. }
  230. readString="";
  231. lets_Drive();
  232. }
  233.  
  234. if(driveDuration > 0 && stop1 > 0){
  235. if (millis()>=stop1) {
  236. Serial.println("Timer over, stoping the car!");
  237. stop_Stop();
  238. }
  239. }
  240.  
  241.  
  242. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement