#include //////////////// // Setup vars // //////////////// SoftwareSerial mySerial(1, 0); // LED pins const int greenPin = 7; const int orangePin = 8; const int redPin = 3; // Button pin const int buttonPin = 2; // Local address byte local_address = 001; //default button state int buttonState = 0; //////////////////// // setup function // //////////////////// void setup() { // initialize the LED pins as outputs pinMode(greenPin, OUTPUT); pinMode(orangePin, OUTPUT); pinMode(redPin, OUTPUT); // initialize the pushbutton pin as an input pinMode(buttonPin, INPUT); // Serial initialization mySerial.begin(9600); //default led state digitalWrite(greenPin, LOW); digitalWrite(orangePin, LOW); digitalWrite(redPin, LOW); } /////////////////// // loop function // /////////////////// void loop() { // read the state of the pushbutton value: buttonState = digitalRead(buttonPin); if (buttonState == HIGH) { digitalWrite(greenPin, HIGH); digitalWrite(orangePin, HIGH); digitalWrite(redPin, HIGH); unsigned int count = 0; while (buttonState == HIGH) { buttonState = digitalRead(buttonPin); count ++; delay(50); } send_Message("002"); digitalWrite(greenPin, LOW); digitalWrite(orangePin, LOW); digitalWrite(redPin, LOW); delay(250); } // Resetting command address byte command_address = 0; // Check if there is stuff available in serial buffer while (mySerial.available() > 0) { // Received raw command var String raw_command = ""; // Extract raw command raw_command = mySerial.readStringUntil(';'); // Read the address received in the command byte command_address = address_LookUp(raw_command); // If the command address is valid and match the local address then execute the next command if (command_address == 255 || command_address == local_address) { raw_command = mySerial.readStringUntil(';'); request_LookUp(raw_command); } else { // Otherwise put the next command in the trash bin mySerial.readStringUntil(';'); } } } /////////////////////////// // send_Message function // /////////////////////////// void send_Message(String address) { mySerial.println("addr:"+ address +";request;"); } ///////////////////////////// // address_LookUp function // ///////////////////////////// byte address_LookUp(String raw) { // Look for valid "addr" command int address_command_pos = raw.lastIndexOf("addr:"); // If there is a valid "address" command then read the value if (address_command_pos != -1 ) { String address_string_value = raw.substring(address_command_pos + 5, raw.length()); // If the readed value is a string of only digits then convert it to int e return it if (isValidInt(address_string_value)) { return address_string_value.toInt(); } } return 0; } ///////////////////////////// // request_LookUp function // ///////////////////////////// void request_LookUp(String raw) { // Look for valid "dim_set" command int dimset_command_pos = raw.lastIndexOf("request"); // If there is a valid "dim_set" command then handoff to dimSetCommand function if (dimset_command_pos != -1 ) { request_Sequence(); } } ///////////////////////////// // request_LookUp function // ///////////////////////////// void request_Sequence() { //GREEN digitalWrite(greenPin, HIGH); digitalWrite(orangePin, LOW); digitalWrite(redPin, LOW); delay(5000); //ORANGE digitalWrite(greenPin, LOW); digitalWrite(orangePin, HIGH); digitalWrite(redPin, LOW); delay(1000); //RED digitalWrite(greenPin, LOW); digitalWrite(orangePin, LOW); digitalWrite(redPin, HIGH); } ///////////////////////// // isValidInt function // ///////////////////////// boolean isValidInt(String str) { boolean isInt = false; for(byte i = 0; i < str.length(); i++) { if(isDigit(str.charAt(i))) { isInt = true; } else { return false; } } return isInt; }