Advertisement
Guest User

Untitled

a guest
Mar 24th, 2019
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.32 KB | None | 0 0
  1. // Basic Bluetooth sketch HC-05_02
  2. // Connect the HC-05 module and communicate using the serial monitor
  3. //
  4. // The HC-05 defaults to commincation mode when first powered on.
  5. // The default baud rate for communication mode is 9600
  6. //
  7.  
  8. #include <SoftwareSerial.h>
  9. SoftwareSerial BTserial(2, 3); // RX | TX
  10. // Connect the HC-05 TX to Arduino pin 2 RX.
  11. // Connect the HC-05 RX to Arduino pin 3 TX through a voltage divider.
  12. //
  13. class BluetoothDataReader {
  14. public:
  15. String angle;
  16. String type;
  17.  
  18. public:
  19. BluetoothDataReader() {
  20. }
  21.  
  22. void read_bluetooth_data_and_parse_wanted_values(String myString) {
  23. int symbol_index = myString.indexOf("_");
  24. type = myString.substring(0, symbol_index);
  25. angle = myString.substring(symbol_index + 1, myString.length());
  26. }
  27.  
  28. String getAngle() {
  29. return this->angle;
  30. }
  31. String getType() {
  32. return this->type;
  33. }
  34. };
  35.  
  36. char c = ' ';
  37. String wholeMessage = "";
  38. BluetoothDataReader* bdr;
  39.  
  40. //------------------
  41. #define STEPPER_PIN_1 8
  42. #define STEPPER_PIN_2 9
  43. #define STEPPER_PIN_3 10
  44. #define STEPPER_PIN_4 11
  45. int step_number = 0;
  46.  
  47.  
  48. //Rotation motor constant variables
  49. #define MAX_ANGLE 180
  50. #define MIN_ANGLE 0
  51. #define BLIND_LENGTH 150
  52. #define ANGLE_PER_ROTATION_PERCENTAGE 22.5
  53. #define STEPS_PER_FULL_ROTATION 2100
  54.  
  55. int current_angle = 0;
  56. int target_angle = 0;
  57. //------------------
  58.  
  59. void setup()
  60. {
  61. Serial.begin(9600);
  62. BTserial.begin(9600);
  63. Serial.println("Arduino is ready");
  64. bdr = new BluetoothDataReader();
  65.  
  66. //rotation setup
  67. pinMode(STEPPER_PIN_1, OUTPUT);
  68. pinMode(STEPPER_PIN_2, OUTPUT);
  69. pinMode(STEPPER_PIN_3, OUTPUT);
  70. pinMode(STEPPER_PIN_4, OUTPUT);
  71. }
  72. void loop()
  73. {
  74. // if (BTserial.available()) {
  75. // String msg_bt = parse_msg_from_bluetooth();
  76. String msg_bt = "r_45";
  77. bdr->read_bluetooth_data_and_parse_wanted_values(msg_bt);
  78. wholeMessage = "";
  79. Serial.print("angle:");
  80. Serial.println(bdr->getAngle());
  81. Serial.print("type:");
  82. Serial.println(bdr->getType());
  83.  
  84. if (bdr->getType().equals("r")) {
  85. target_angle = bdr->getAngle().toInt();
  86. Serial.print("target:");
  87. Serial.println(target_angle);
  88.  
  89.  
  90. if (target_angle < MIN_ANGLE) {
  91. target_angle = MIN_ANGLE;
  92. }
  93. if (target_angle > MAX_ANGLE) {
  94. target_angle = MAX_ANGLE;
  95. }
  96. if (target_angle != 0) {
  97. int rotations = calculateRotations();
  98. bool direc = calculateRotationDirection();
  99. Serial.print("rotations:");
  100. Serial.println(rotations);
  101. Serial.print("direc:");
  102. Serial.println(direc);
  103. for (int i = 0; i < rotations; i++) {
  104.  
  105. for (int j = 0; j < STEPS_PER_FULL_ROTATION; j++) {
  106. OneStep(direc);
  107. delay(2);
  108. }
  109. }
  110. current_angle = target_angle;
  111. target_angle = 0;
  112. delay(10000);
  113. }
  114. }
  115. if (bdr->getType().equals("m")) {
  116. }
  117. // }
  118. delay(100);
  119. }
  120.  
  121. String parse_msg_from_bluetooth() {
  122. while (BTserial.available()) {
  123. c = BTserial.read();
  124. if (c != '{' && c != '}') {
  125. wholeMessage += c;
  126. }
  127. if (c == '}') {
  128. c = '\0';
  129. return wholeMessage;
  130. }
  131. }
  132. }
  133.  
  134. int calculateRotations() {
  135. int rotations_to_do = current_angle - target_angle;
  136. return abs(rotations_to_do) / ANGLE_PER_ROTATION_PERCENTAGE;
  137. }
  138.  
  139. bool calculateRotationDirection() {
  140. return current_angle - target_angle < 0 ? true : false;
  141. }
  142.  
  143. void OneStep(bool dir) {
  144. if (dir) {
  145. switch (step_number) {
  146. case 0:
  147. digitalWrite(STEPPER_PIN_1, HIGH);
  148. digitalWrite(STEPPER_PIN_2, LOW);
  149. digitalWrite(STEPPER_PIN_3, LOW);
  150. digitalWrite(STEPPER_PIN_4, LOW);
  151. break;
  152. case 1:
  153. digitalWrite(STEPPER_PIN_1, LOW);
  154. digitalWrite(STEPPER_PIN_2, HIGH);
  155. digitalWrite(STEPPER_PIN_3, LOW);
  156. digitalWrite(STEPPER_PIN_4, LOW);
  157. break;
  158. case 2:
  159. digitalWrite(STEPPER_PIN_1, LOW);
  160. digitalWrite(STEPPER_PIN_2, LOW);
  161. digitalWrite(STEPPER_PIN_3, HIGH);
  162. digitalWrite(STEPPER_PIN_4, LOW);
  163. break;
  164. case 3:
  165. digitalWrite(STEPPER_PIN_1, LOW);
  166. digitalWrite(STEPPER_PIN_2, LOW);
  167. digitalWrite(STEPPER_PIN_3, LOW);
  168. digitalWrite(STEPPER_PIN_4, HIGH);
  169. break;
  170. }
  171. } else {
  172. switch (step_number) {
  173. case 0:
  174. digitalWrite(STEPPER_PIN_1, LOW);
  175. digitalWrite(STEPPER_PIN_2, LOW);
  176. digitalWrite(STEPPER_PIN_3, LOW);
  177. digitalWrite(STEPPER_PIN_4, HIGH);
  178. break;
  179. case 1:
  180. digitalWrite(STEPPER_PIN_1, LOW);
  181. digitalWrite(STEPPER_PIN_2, LOW);
  182. digitalWrite(STEPPER_PIN_3, HIGH);
  183. digitalWrite(STEPPER_PIN_4, LOW);
  184. break;
  185. case 2:
  186. digitalWrite(STEPPER_PIN_1, LOW);
  187. digitalWrite(STEPPER_PIN_2, HIGH);
  188. digitalWrite(STEPPER_PIN_3, LOW);
  189. digitalWrite(STEPPER_PIN_4, LOW);
  190. break;
  191. case 3:
  192. digitalWrite(STEPPER_PIN_1, HIGH);
  193. digitalWrite(STEPPER_PIN_2, LOW);
  194. digitalWrite(STEPPER_PIN_3, LOW);
  195. digitalWrite(STEPPER_PIN_4, LOW);
  196.  
  197.  
  198. }
  199. }
  200. step_number++;
  201. if (step_number > 3) {
  202. step_number = 0;
  203. }
  204. }
  205. // Keep reading from Arduino Serial Monitor and send to HC-05
  206. // if (Serial.available())
  207. // {
  208. // c = Serial.read();
  209. // BTserial.write(c);
  210. // }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement