Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <SoftwareSerial.h>
- SoftwareSerial outStream(10, 11); // RX, TX
- /* Connection states enumerator
- * Used to keep track of the connection state
- */
- typedef enum Connection {
- INIT = 0,
- Disconnected = 1,
- Connected = 2,
- Waiting = 3
- };
- /* Diodes state enumerator
- * Used to keep track of the current state of the lights
- */
- typedef enum lightState {
- Green = 1,
- Red = 0,
- TurnedOff = 2,
- Init = 3
- };
- /* Enumerator with the message types
- * Used to denote messages that the protocol uses
- */
- typedef enum messages {
- RequestRed = 'r',
- RequestGreen = 'g',
- ApproveRed = 'd',
- ApproveGreen = 'n',
- Connection = 'c',
- Confirm = 'f',
- ACK = 'k'
- };
- /*
- * currentState - the state of the lights at the moment
- * connectionState - the state of the connection at the moment
- * timeout - the timeout for messages
- */
- int currentState;
- int connectionState;
- int timeout;
- /*
- * redLedPin - holds the value of the pin which is responsible for the red light
- * greenLedPin - holds the value of the pin which is responsible for the green light
- */
- int redLedPin = 7;
- int greenLedPin = 8;
- /*
- * void setup() - funtion which is ran at the beginning of the program
- */
- void setup() {
- /*
- * set the states to the initial state
- * configure the LED pins to act as output
- */
- connectionState = INIT;
- currentState = Init;
- pinMode(redLedPin, OUTPUT);
- pinMode(greenLedPin, OUTPUT);
- }
- /*
- * void loop() - function which loops at all time and holds all the processes
- */
- void loop() {
- /*
- * Switch statement for handling the lights' states
- * the switch cases are the possible light states
- */
- switch(currentState) {
- case Init:
- currentState = TurnedOff;
- break;
- case Red:
- Serial.write("Red\n");
- digitalWrite(redLedPin, HIGH);
- digitalWrite(greenLedPin, LOW);
- break;
- case Green:
- Serial.write("Green\n");
- digitalWrite(redLedPin, LOW);
- digitalWrite(greenLedPin, HIGH);
- break;
- case TurnedOff:
- digitalWrite(redLedPin, LOW);
- digitalWrite(greenLedPin, LOW);
- break;
- }
- /*
- * Switch statement for handling the connection's states
- * the switch cases are the possible connection states
- */
- switch(connectionState) {
- case INIT:
- /*
- * Start the serial streams
- */
- Serial.begin(4800);
- outStream.begin(4800);
- Serial.write("I'm up!\n");
- connectionState = Disconnected;
- break;
- case Disconnected:
- /*
- * keep attempting to reconnect until response is received
- */
- Serial.write("Disconnected \n");
- if(outStream.available()) {
- if(outStream.read() == Confirm) {
- connectionState = Connected;
- }
- }
- outStream.write(Confirm);
- break;
- case Connected:
- /*
- * Listen for messages
- * Also transmit messages if any are send in the Serial console
- * timeout becomes the current milliseconds when a message is sent
- */
- Serial.write("Connected \n");
- int msg;
- if(Serial.available()) {
- outStream.write(Serial.read());
- timeout = millis();
- connectionState = Waiting;
- }
- else if(outStream.available()) {
- msg = outStream.read();
- }
- switch(msg) {
- case RequestRed:
- currentState = Green;
- outStream.write(ApproveRed);
- break;
- case RequestGreen:
- currentState = Red;
- outStream.write(ApproveGreen);
- break;
- case Connection:
- outStream.write(Confirm);
- break;
- }
- break;
- case Waiting:
- /*
- * Wait for responses or until the timeout is reached
- * then change the state to disconnected
- * The current timeout tolerance is 3000 mS
- * When an understandable message is receive
- * the state is changed accordingly
- */
- Serial.write("Waiting \n");
- msg = ' ';
- if(outStream.available()) {
- msg = outStream.read();
- }
- else if (millis() - timeout >= 3000){
- connectionState = Connected;
- break;
- }
- switch(msg) {
- case ApproveRed:
- currentState = Red;
- connectionState = Connected;
- break;
- case ApproveGreen:
- currentState = Green;
- connectionState = Connected;
- break;
- case ACK:
- connectionState = Connected;
- break;
- case Confirm:
- connectionState = Connected;
- break;
- }
- break;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement