Guest User

Untitled

a guest
Jan 24th, 2020
74
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2. FSM assignment
  3. */
  4.  
  5. #include <Arduino.h>
  6. #include <Wire.h>
  7.  
  8. enum State {
  9. I000, // 0
  10. I001, // 1
  11. I010, // 2
  12. I011, // 3
  13. I100, // 4
  14. I101, // 5
  15. I110, // 6
  16. I111, // 7
  17. D000, // 8
  18. D001, // 9
  19. D010, // 10
  20. D011, // 11
  21. D100, // 12
  22. D101, // 13
  23. D110, // 14
  24. D111 // 15
  25. };
  26.  
  27. State state = I000;
  28.  
  29.  
  30. void setup() {
  31. Serial.begin(9600);
  32. }
  33.  
  34. void loop() {
  35. state = nextState(state);
  36. delay(1000);
  37. }
  38.  
  39. State nextState (State state) {
  40. char input = Serial.read();
  41. switch (state) {
  42. case I000:
  43. Serial.println(" 0 : 000 ");
  44. if (checkReverse()) {
  45. state = I001;
  46. }
  47. else {
  48. state = D111;
  49. }
  50. break;
  51. case I001:
  52. Serial.println(" 1 : 001 ");
  53. if (checkReverse()) {
  54. state = I010;
  55. }
  56. else {
  57. state = D000;
  58. }
  59. break;
  60. case I010:
  61. Serial.println(" 2 : 010 ");
  62. if (checkReverse()) {
  63. state = I011;
  64. }
  65. else {
  66. state = D001;
  67. }
  68. break;
  69. case I011:
  70. Serial.println(" 3 : 011 ");
  71. if (checkReverse()) {
  72. state = I100;
  73. }
  74. else {
  75. state = D010;
  76. }
  77. break;
  78. case I100:
  79. Serial.println(" 4 : 100 ");
  80. if (checkReverse()) {
  81. state = I101;
  82. }
  83. else {
  84. state = D011;
  85. }
  86. break;
  87. case I101:
  88. Serial.println(" 5 : 101 ");
  89. if (checkReverse()) {
  90. state = I110;
  91. }
  92. else {
  93. state = D100;
  94. }
  95. break;
  96. case I110:
  97. Serial.println(" 6 : 110 ");
  98. if (checkReverse()) {
  99. state = I111;
  100. }
  101. else {
  102. state = D101;
  103. }
  104. break;
  105. case I111:
  106. Serial.println(" 7 : 111 ");
  107. if (checkReverse()) {
  108. state = I000;
  109. }
  110. else {
  111. state = D110;
  112. }
  113. break;
  114. case D000:
  115. Serial.println(" 0 : 000 ");
  116. if (checkReverse()) {
  117. state = D111;
  118. }
  119. else {
  120. state = I001;
  121. }
  122. break;
  123. case D111:
  124. Serial.println(" 7 : 111 ");
  125. if (checkReverse()) {
  126. state = D110;
  127. }
  128. else {
  129. state = I000;
  130. }
  131. break;
  132. case D110:
  133. Serial.println(" 6 : 110 ");
  134. if (checkReverse()) {
  135. state = D101;
  136. }
  137. else {
  138. state = I111;
  139. }
  140. break;
  141. case D101:
  142. Serial.println(" 5 : 101 ");
  143. if (checkReverse()) {
  144. state = D100;
  145. }
  146. else {
  147. state = I110;
  148. }
  149. break;
  150. case D100:
  151. Serial.println(" 4 : 100 ");
  152. if (checkReverse()) {
  153. state = D011;
  154. }
  155. else {
  156. state = I101;
  157. }
  158. break;
  159. case D011:
  160. Serial.println(" 3 : 011 ");
  161. if (checkReverse()) {
  162. state = D010;
  163. }
  164. else {
  165. state = I100;
  166. }
  167. break;
  168. case D010:
  169. Serial.println(" 2 : 010 ");
  170. if (checkReverse()) {
  171. state = D001;
  172. }
  173. else {
  174. state = I011;
  175. }
  176. break;
  177. case D001:
  178. Serial.println(" 1 : 001 ");
  179. if (checkReverse()) {
  180. state = D000;
  181. }
  182. else {
  183. state = I010;
  184. }
  185. break;
  186. }
  187. return state;
  188. }
  189.  
  190. bool checkReverse(){ // or no inputs??
  191. int input = Serial.read();
  192. if(input != -1){
  193. Serial.println(" --Reverse-- ");
  194. return true;
  195. }
  196. return false;
  197. }
  198.  
  199. //to do: don't use checkReverse in nextState and make checkReverse a method and use it in the loop
RAW Paste Data