Advertisement
Guest User

Untitled

a guest
Nov 8th, 2019
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.55 KB | None | 0 0
  1. /*************************************************************
  2. Motor Shield Stepper Demo
  3. by Randy Sarafan
  4.  
  5. For more information see:
  6. https://www.instructables.com/id/Arduino-Motor-Shield-Tutorial/
  7.  
  8. *************************************************************/
  9.  
  10. int delaylegnth = 10;
  11. int current_pos = 0;
  12.  
  13. void setup() {
  14.  
  15. //establish motor direction toggle pins
  16. pinMode(12, OUTPUT); //CH A -- HIGH = forwards and LOW = backwards???
  17. pinMode(13, OUTPUT); //CH B -- HIGH = forwards and LOW = backwards???
  18.  
  19. //establish motor brake pins
  20. pinMode(9, OUTPUT); //brake (disable) CH A
  21. pinMode(8, OUTPUT); //brake (disable) CH B
  22. Serial.begin(9600);
  23.  
  24. }
  25. int parseIntFast(int numberOfDigits){
  26. /*
  27. This function returns the converted integral number as an int value.
  28. If no valid conversion could be performed, it returns zero.*/
  29. char theNumberString[numberOfDigits + 1];
  30. int theNumber;
  31. for (int i = 0; i < numberOfDigits; theNumberString[i++] = Serial.read()){
  32. delay(5);
  33. };
  34. theNumberString[numberOfDigits] = 0x00;
  35. theNumber = atoi(theNumberString);
  36. return theNumber;
  37. }
  38. void ReadSerialCommands(){ // Plot PID Values using Serial Plotter
  39. if (Serial.available()){
  40. char cmdByte = Serial.read();
  41. int setting;
  42. switch (cmdByte) {
  43. case 'M': // Change Proportianal Gain
  44. setting = parseIntFast(4);
  45. takeNSteps(setting);
  46. Serial.print("Set Throttle to ");
  47. Serial.println(setting);
  48. break;
  49. case 'Z': // Change Integral Gain
  50. current_pos = 0;
  51. Serial.print("Set Steering to 0");
  52. break;
  53. }
  54. Serial.flush();
  55. }
  56. }
  57.  
  58. void takeNSteps(int a){
  59. bool go_right = a>0;
  60. a = abs(a);
  61. if (go_right) {
  62. while (a-- > 0){
  63. digitalWrite(9, LOW); //ENABLE CH A
  64. digitalWrite(8, HIGH); //DISABLE CH B
  65.  
  66. digitalWrite(12, HIGH); //Sets direction of CH A
  67. analogWrite(3, 255); //Moves CH A
  68.  
  69. delay(delaylegnth);
  70.  
  71. digitalWrite(9, HIGH); //DISABLE CH A
  72. digitalWrite(8, LOW); //ENABLE CH B
  73.  
  74. digitalWrite(13, LOW); //Sets direction of CH B
  75. analogWrite(11, 255); //Moves CH B
  76.  
  77. delay(delaylegnth);
  78.  
  79. digitalWrite(9, LOW); //ENABLE CH A
  80. digitalWrite(8, HIGH); //DISABLE CH B
  81.  
  82. digitalWrite(12, LOW); //Sets direction of CH A
  83. analogWrite(3, 255); //Moves CH A
  84.  
  85. delay(delaylegnth);
  86.  
  87. digitalWrite(9, HIGH); //DISABLE CH A
  88. digitalWrite(8, LOW); //ENABLE CH B
  89.  
  90. digitalWrite(13, HIGH); //Sets direction of CH B
  91. analogWrite(11, 255); //Moves CH B
  92.  
  93. delay(delaylegnth);
  94. }
  95. } else {
  96. while(a-->0){
  97. digitalWrite(9, LOW); //ENABLE CH A
  98. digitalWrite(8, HIGH); //DISABLE CH B
  99.  
  100. digitalWrite(12, HIGH); //Sets direction of CH A
  101. analogWrite(3, 255); //Moves CH A
  102.  
  103. delay(delaylegnth);
  104.  
  105. digitalWrite(9, HIGH); //DISABLE CH A
  106. digitalWrite(8, LOW); //ENABLE CH B
  107.  
  108. digitalWrite(13, HIGH); //Sets direction of CH B
  109. analogWrite(11, 255); //Moves CH B
  110.  
  111. delay(delaylegnth);
  112.  
  113. digitalWrite(9, LOW); //ENABLE CH A
  114. digitalWrite(8, HIGH); //DISABLE CH B
  115.  
  116. digitalWrite(12, LOW); //Sets direction of CH A
  117. analogWrite(3, 255); //Moves CH A
  118.  
  119. delay(delaylegnth);
  120.  
  121. digitalWrite(9, HIGH); //DISABLE CH A
  122. digitalWrite(8, LOW); //ENABLE CH B
  123.  
  124. digitalWrite(13, LOW); //Sets direction of CH B
  125. analogWrite(11, 255); //Moves CH B
  126.  
  127. delay(delaylegnth);}}}
  128.  
  129. void loop(){
  130. ReadSerialCommands();
  131.  
  132. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement