/* DigitalReadSerial Reads a digital input on pin 2, prints the result to the serial monitor This example code is in the public domain. */ // digital pin 5 has a pushbutton attached to it. Give it a name: int zwaveSwitch = 5; int ledPin = 16; //LED is ON when ARMED AWAY (be careful, this pin is reversed. LOW is ON/HIGH is OFF. int armedPin = 0; //pin "0" (D3) and let it be ARMED AWAY int disarmedPin = 4; //pin "4" (D2) and let it be DISARMED int stateMachine = 0; int buttonState = 0; // the setup routine runs once when you press reset: void setup() { // initialize serial communication at 115200 bits per second: //Serial.begin(115200); // make the pushbutton's pin an input: pinMode(zwaveSwitch, INPUT_PULLUP); pinMode(armedPin, OUTPUT); pinMode(disarmedPin, OUTPUT); pinMode(ledPin, OUTPUT); stateMachine = digitalRead(zwaveSwitch); //Make sure there is no mismatch for some odd reason. } // the loop routine runs over and over again forever: void loop() { // read the input pin: buttonState = digitalRead(zwaveSwitch); if (buttonState == 0 && stateMachine == 0){ // this means that the switch is ON (And the Alarm should be ARMED AWAY) digitalWrite(armedPin, HIGH); delay(2000); digitalWrite(armedPin, LOW); stateMachine = 1; } if (buttonState == 1 && stateMachine == 1){ // this means that the switch is OFF (And the Alarm should be DISARMED) digitalWrite(disarmedPin, HIGH); delay(2000); digitalWrite(disarmedPin, LOW); stateMachine = 0; } if (buttonState == 0 && stateMachine == 1){ // this means that the switch is ON (And the Alarm should be ARMED AWAY) digitalWrite(ledPin, LOW); //we want the LED to light up while the switch is ON and the Alarm is armed. } if (buttonState == 1 && stateMachine == 0){ // this means that the switch is OFF (And the Alarm should be DISARMED) digitalWrite(ledPin, HIGH); //we want the LED to turn off while the switch is OFF and the Alarm is disarmed. } // print out the state of the button: //Serial.print(buttonState); //Serial.println(stateMachine); delay(10); // delay in between reads for stability }