Advertisement
Guest User

Untitled

a guest
Dec 11th, 2018
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.64 KB | None | 0 0
  1. #include <SoftwareSerial.h>
  2.  
  3. SoftwareSerial outStream(10, 11); // RX, TX
  4.  
  5. /* Connection states enumerator
  6. * Used to keep track of the connection state
  7. */
  8. typedef enum Connection {
  9. INIT = 0,
  10. Disconnected = 1,
  11. Connected = 2,
  12. Waiting = 3
  13. };
  14.  
  15. /* Diodes state enumerator
  16. * Used to keep track of the current state of the lights
  17. */
  18. typedef enum lightState {
  19. Green = 1,
  20. Red = 0,
  21. TurnedOff = 2,
  22. Init = 3
  23. };
  24.  
  25.  
  26. /* Enumerator with the message types
  27. * Used to denote messages that the protocol uses
  28. */
  29. typedef enum messages {
  30. RequestRed = 'r',
  31. RequestGreen = 'g',
  32. ApproveRed = 'd',
  33. ApproveGreen = 'n',
  34. Connection = 'c',
  35. Confirm = 'f',
  36. ACK = 'k'
  37. };
  38.  
  39. /*
  40. * currentState - the state of the lights at the moment
  41. * connectionState - the state of the connection at the moment
  42. * timeout - the timeout for messages
  43. */
  44. int currentState;
  45. int connectionState;
  46. int timeout;
  47.  
  48. /*
  49. * redLedPin - holds the value of the pin which is responsible for the red light
  50. * greenLedPin - holds the value of the pin which is responsible for the green light
  51. */
  52. int redLedPin = 7;
  53. int greenLedPin = 8;
  54.  
  55.  
  56. /*
  57. * void setup() - funtion which is ran at the beginning of the program
  58. */
  59. void setup() {
  60. /*
  61. * set the states to the initial state
  62. * configure the LED pins to act as output
  63. */
  64. connectionState = INIT;
  65. currentState = Init;
  66. pinMode(redLedPin, OUTPUT);
  67. pinMode(greenLedPin, OUTPUT);
  68. }
  69.  
  70. /*
  71. * void loop() - function which loops at all time and holds all the processes
  72. */
  73. void loop() {
  74. /*
  75. * Switch statement for handling the lights' states
  76. * the switch cases are the possible light states
  77. */
  78. switch(currentState) {
  79. case Init:
  80. currentState = TurnedOff;
  81. break;
  82. case Red:
  83. Serial.write("Red\n");
  84. digitalWrite(redLedPin, HIGH);
  85. digitalWrite(greenLedPin, LOW);
  86. break;
  87. case Green:
  88. Serial.write("Green\n");
  89. digitalWrite(redLedPin, LOW);
  90. digitalWrite(greenLedPin, HIGH);
  91. break;
  92. case TurnedOff:
  93. digitalWrite(redLedPin, LOW);
  94. digitalWrite(greenLedPin, LOW);
  95. break;
  96. }
  97.  
  98. /*
  99. * Switch statement for handling the connection's states
  100. * the switch cases are the possible connection states
  101. */
  102. switch(connectionState) {
  103. case INIT:
  104. /*
  105. * Start the serial streams
  106. */
  107. Serial.begin(4800);
  108. outStream.begin(4800);
  109. Serial.write("I'm up!\n");
  110. connectionState = Disconnected;
  111. break;
  112. case Disconnected:
  113. /*
  114. * keep attempting to reconnect until response is received
  115. */
  116. Serial.write("Disconnected \n");
  117. if(outStream.available()) {
  118. if(outStream.read() == Confirm) {
  119. connectionState = Connected;
  120. }
  121. }
  122.  
  123. outStream.write(Confirm);
  124. break;
  125. case Connected:
  126. /*
  127. * Listen for messages
  128. * Also transmit messages if any are send in the Serial console
  129. * timeout becomes the current milliseconds when a message is sent
  130. */
  131. Serial.write("Connected \n");
  132. int msg;
  133.  
  134. if(Serial.available()) {
  135. outStream.write(Serial.read());
  136. timeout = millis();
  137. connectionState = Waiting;
  138. }
  139. else if(outStream.available()) {
  140. msg = outStream.read();
  141. }
  142.  
  143. switch(msg) {
  144. case RequestRed:
  145. currentState = Green;
  146. outStream.write(ApproveRed);
  147. break;
  148. case RequestGreen:
  149. currentState = Red;
  150. outStream.write(ApproveGreen);
  151. break;
  152. case Connection:
  153. outStream.write(Confirm);
  154. break;
  155. }
  156. break;
  157. case Waiting:
  158. /*
  159. * Wait for responses or until the timeout is reached
  160. * then change the state to disconnected
  161. * The current timeout tolerance is 3000 mS
  162. * When an understandable message is receive
  163. * the state is changed accordingly
  164. */
  165. Serial.write("Waiting \n");
  166. msg = ' ';
  167.  
  168. if(outStream.available()) {
  169. msg = outStream.read();
  170. }
  171. else if (millis() - timeout >= 3000){
  172. connectionState = Connected;
  173. break;
  174. }
  175.  
  176. switch(msg) {
  177. case ApproveRed:
  178. currentState = Red;
  179. connectionState = Connected;
  180. break;
  181. case ApproveGreen:
  182. currentState = Green;
  183. connectionState = Connected;
  184. break;
  185. case ACK:
  186. connectionState = Connected;
  187. break;
  188. case Confirm:
  189. connectionState = Connected;
  190. break;
  191. }
  192. break;
  193. }
  194. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement