Advertisement
Guest User

Untitled

a guest
Nov 21st, 2018
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.06 KB | None | 0 0
  1. #define BRAKE 0
  2. #define CW 1
  3. #define CCW 2
  4. #define CS_THRESHOLD 15 // Definition of safety current (Check: "1.3 Monster Shield Example").
  5.  
  6. //MOTOR 1
  7. #define MOTOR_A1_PIN 7
  8. #define MOTOR_B1_PIN 8
  9.  
  10. //MOTOR 2
  11. #define MOTOR_A2_PIN 4
  12. #define MOTOR_B2_PIN 9
  13.  
  14. #define PWM_MOTOR_1 5
  15. #define PWM_MOTOR_2 6
  16.  
  17. #define CURRENT_SEN_1 A2
  18. #define CURRENT_SEN_2 A3
  19.  
  20. #define EN_PIN_1 A0
  21. #define EN_PIN_2 A1
  22.  
  23. #define MOTOR_1 0
  24. #define MOTOR_2 1
  25.  
  26. short usSpeed = 150; //default motor speed
  27. unsigned short usMotor_Status = BRAKE;
  28.  
  29. void setup()
  30. {
  31. pinMode(MOTOR_A1_PIN, OUTPUT);
  32. pinMode(MOTOR_B1_PIN, OUTPUT);
  33.  
  34. pinMode(MOTOR_A2_PIN, OUTPUT);
  35. pinMode(MOTOR_B2_PIN, OUTPUT);
  36.  
  37. pinMode(PWM_MOTOR_1, OUTPUT);
  38. pinMode(PWM_MOTOR_2, OUTPUT);
  39.  
  40. pinMode(CURRENT_SEN_1, OUTPUT);
  41. pinMode(CURRENT_SEN_2, OUTPUT);
  42.  
  43. pinMode(EN_PIN_1, OUTPUT);
  44. pinMode(EN_PIN_2, OUTPUT);
  45.  
  46. Serial.begin(9600); // Initiates the serial to do the monitoring
  47. Serial.println("Begin motor control");
  48. Serial.println(); //Print function list for user selection
  49. Serial.println("Enter number for control option:");
  50. Serial.println("1. STOP");
  51. Serial.println("2. FORWARD");
  52. Serial.println("3. REVERSE");
  53. Serial.println("4. READ CURRENT");
  54. Serial.println("+. INCREASE SPEED");
  55. Serial.println("-. DECREASE SPEED");
  56. Serial.println();
  57.  
  58. }
  59.  
  60. void loop()
  61. {
  62. char user_input;
  63.  
  64.  
  65.  
  66. while(Serial.available())
  67. {
  68. user_input = Serial.read(); //Read user input and trigger appropriate function
  69. digitalWrite(EN_PIN_1, HIGH);
  70. digitalWrite(EN_PIN_2, HIGH);
  71.  
  72. if (user_input =='1')
  73. {
  74. Stop();
  75. }
  76. else if(user_input =='2')
  77. {
  78. Forward();
  79. }
  80. else if(user_input =='3')
  81. {
  82. Reverse();
  83. }
  84. else if(user_input =='+')
  85. {
  86. IncreaseSpeed();
  87. }
  88. else if(user_input =='-')
  89. {
  90. DecreaseSpeed();
  91. }
  92. else
  93. {
  94. Serial.println("Invalid option entered.");
  95. }
  96.  
  97. }
  98. }
  99.  
  100. void Stop()
  101. {
  102. Serial.println("Stop");
  103. usMotor_Status = BRAKE;
  104. motorGo(MOTOR_1, usMotor_Status, 0);
  105. motorGo(MOTOR_2, usMotor_Status, 0);
  106. }
  107.  
  108. void Forward()
  109. {
  110. Serial.println("Forward");
  111. usMotor_Status = CW;
  112. motorGo(MOTOR_1, usMotor_Status, usSpeed);
  113. motorGo(MOTOR_2, usMotor_Status, usSpeed);
  114. }
  115.  
  116. void Reverse()
  117. {
  118. Serial.println("Reverse");
  119. usMotor_Status = CCW;
  120. motorGo(MOTOR_1, usMotor_Status, usSpeed);
  121. motorGo(MOTOR_2, usMotor_Status, usSpeed);
  122. }
  123.  
  124. void IncreaseSpeed()
  125. {
  126. usSpeed = usSpeed + 10;
  127. if(usSpeed > 255)
  128. {
  129. usSpeed = 255;
  130. }
  131.  
  132. Serial.print("Speed +: ");
  133. Serial.println(usSpeed);
  134.  
  135. motorGo(MOTOR_1, usMotor_Status, usSpeed);
  136. motorGo(MOTOR_2, usMotor_Status, usSpeed);
  137. }
  138.  
  139. void DecreaseSpeed()
  140. {
  141. usSpeed = usSpeed - 10;
  142. if(usSpeed < 0)
  143. {
  144. usSpeed = 0;
  145. }
  146.  
  147. Serial.print("Speed -: ");
  148. Serial.println(usSpeed);
  149.  
  150. motorGo(MOTOR_1, usMotor_Status, usSpeed);
  151. motorGo(MOTOR_2, usMotor_Status, usSpeed);
  152. }
  153.  
  154. void motorGo(uint8_t motor, uint8_t direct, uint8_t pwm) //Function that controls the variables: motor(0 ou 1), direction (cw ou ccw) e pwm (entra 0 e 255);
  155. {
  156. if(motor == MOTOR_1)
  157. {
  158. if(direct == CW)
  159. {
  160. digitalWrite(MOTOR_A1_PIN, LOW);
  161. digitalWrite(MOTOR_B1_PIN, HIGH);
  162. }
  163. else if(direct == CCW)
  164. {
  165. digitalWrite(MOTOR_A1_PIN, HIGH);
  166. digitalWrite(MOTOR_B1_PIN, LOW);
  167. }
  168. else
  169. {
  170. digitalWrite(MOTOR_A1_PIN, LOW);
  171. digitalWrite(MOTOR_B1_PIN, LOW);
  172. }
  173.  
  174. analogWrite(PWM_MOTOR_1, pwm);
  175. }
  176. else if(motor == MOTOR_2)
  177. {
  178. if(direct == CW)
  179. {
  180. digitalWrite(MOTOR_A2_PIN, LOW);
  181. digitalWrite(MOTOR_B2_PIN, HIGH);
  182. }
  183. else if(direct == CCW)
  184. {
  185. digitalWrite(MOTOR_A2_PIN, HIGH);
  186. digitalWrite(MOTOR_B2_PIN, LOW);
  187. }
  188. else
  189. {
  190. digitalWrite(MOTOR_A2_PIN, LOW);
  191. digitalWrite(MOTOR_B2_PIN, LOW);
  192. }
  193.  
  194. analogWrite(PWM_MOTOR_2, pwm);
  195. }
  196. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement