Advertisement
Guest User

Untitled

a guest
Mar 21st, 2019
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.41 KB | None | 0 0
  1. #include <Arduino.h>
  2. #include <Servo.h>
  3.  
  4. int array_size;
  5. bool receive_done;
  6. int *input_array;
  7. int servo_array[6];
  8. bool receive_start;
  9.  
  10. bool DEBUG = true;
  11.  
  12. void setup()
  13. {
  14. Serial.begin(9600);
  15. for (int i = 0; i < 6; i++)
  16. pinMode(i + 2, OUTPUT);
  17. }
  18.  
  19. void reset_servos()
  20. {
  21. for (int i = 0; i < 6; i++) {
  22. digitalWrite(i + 2, LOW);
  23. servo_array[i] = 0;
  24. }
  25. delay(100); /* artificial "transition" delay */
  26. }
  27.  
  28. void process_servo_action()
  29. {
  30. static int cur_pos = 0;
  31. static bool arrays_set = false;
  32. static bool debounce_hold_right = false;
  33. static bool debounce_hold_left = false;
  34.  
  35. if (!arrays_set) {
  36. reset_servos();
  37.  
  38. if (input_array[cur_pos] & 0b000001)
  39. servo_array[0] = 1;
  40. if (input_array[cur_pos] & 0b000010)
  41. servo_array[1] = 1;
  42. if (input_array[cur_pos] & 0b000100)
  43. servo_array[2] = 1;
  44. if (input_array[cur_pos] & 0b001000)
  45. servo_array[3] = 1;
  46. if (input_array[cur_pos] & 0b010000)
  47. servo_array[4] = 1;
  48. if (input_array[cur_pos] & 0b100000)
  49. servo_array[5] = 1;
  50.  
  51. if (DEBUG) {
  52. Serial.println("Showing value: ");
  53. Serial.println(input_array[cur_pos]);
  54. Serial.println("At pos: ");
  55. Serial.println(cur_pos);
  56. }
  57.  
  58. for (int i = 0; i < 6; i++) {
  59. if (servo_array[i] == 1)
  60. digitalWrite(i + 2, HIGH);
  61. }
  62. arrays_set = true;
  63. }
  64.  
  65. if (Serial.available()) {
  66. int serial_data = Serial.read();
  67.  
  68. if (serial_data == 0xF1) {
  69. cur_pos--;
  70. } else if (serial_data == 0xF2) {
  71. cur_pos++;
  72. } else if (serial_data == 0xF0) { /* new string input */
  73. receive_done = false;
  74. receive_start = true;
  75. cur_pos = 0;
  76. if (DEBUG) Serial.println("Received new string");
  77. delete[] input_array;
  78. }
  79. arrays_set = false;
  80. }
  81. }
  82.  
  83. void process_serial_data()
  84. {
  85. static bool array_created = false;
  86. static int data_collect = 0;
  87.  
  88. if (Serial.available()) {
  89. int incoming_data = Serial.read();
  90.  
  91. Serial.println(incoming_data, HEX);
  92.  
  93. if (array_created && receive_start) {
  94. if (data_collect < array_size) {
  95. input_array[data_collect] = incoming_data;
  96. data_collect++;
  97. if (data_collect == array_size) {
  98. receive_done = true;
  99. receive_start = false;
  100. array_created = false;
  101. return;
  102. }
  103. }
  104. }
  105.  
  106. if (receive_start && !array_created) {
  107. /* heap fragmentation issues with dynamic mem allocation? */
  108. input_array = new int[incoming_data];
  109. if (input_array == NULL)
  110. Serial.println("Failed to alloc mem for input_array");
  111.  
  112. array_size = incoming_data;
  113. data_collect = 0;
  114. array_created = true;
  115. if (DEBUG) {
  116. Serial.print("Created array of size: ");
  117. Serial.print(incoming_data);
  118. Serial.println("");
  119. }
  120. }
  121.  
  122. if (incoming_data == 0xF0) {
  123. if (DEBUG)
  124. Serial.println("Received start send signal");
  125. receive_start = true;
  126. }
  127. }
  128. }
  129.  
  130. void loop()
  131. {
  132. if (!receive_done)
  133. process_serial_data();
  134. else if (receive_done)
  135. process_servo_action();
  136. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement