diff --git a/zuno/zuno.ino b/zuno/zuno.ino index b964fbb..06afc03 100644 --- a/zuno/zuno.ino +++ b/zuno/zuno.ino @@ -1,50 +1,189 @@ #include -#define MY_SERIAL Serial +#define RFID Serial #define SERVO_PIN 12 -#define SERVO_ENABLE_PIN 9 +const int SERVO_ENABLE_PIN = 9; -#define BTN_LOCK_PIN 3 -#define BTN_UNLOCK_PIN 4 -#define LED_LOCK_PIN 5 -#define LED_UNLOCK_PIN 6 +const int SENSOR_LOCK_PIN = 0; +const int SENSOR_HANDLE_PIN = 1; +const int SENSOR_DISABLE_PIN = 2; +const int SENSOR_CLOSE_PIN = 10; + +const int BTN_LOCK_PIN = 3; +const int BTN_UNLOCK_PIN = 4; +const int LED_LOCK_PIN = 5; +const int LED_UNLOCK_PIN = 6; + +#define SERVO_DISABLE 0 +#define SERVO_LOCK 1 +#define SERVO_UNLOCK 2 +#define SERVO_CENTER 3 + +#define SOURCE_EXTERNAL 0; +#define SOURCE_RFID 1; +#define SOURCE_ZWAVE 2; +#define SOURCE_BUTTONS 3; // Last saved LED value -byte currentLEDValue; +byte currentLEDValue = 0; unsigned long int rfid = 0; +#define SIGNAL_DEBOUNCE_CONSTANT 30 + +bool sensor_lock = false; +bool sensor_handle = false; +bool sensor_disable = false; +bool sensor_close = false; + +int sensor_lock_prev_state = HIGH; +int sensor_lock_current_state = LOW; +int sensor_lock_debounce = 0; +int sensor_handle_prev_state = HIGH; +int sensor_handle_current_state = LOW; +int sensor_handle_debounce = 0; +int sensor_disable_prev_state = HIGH; +int sensor_disable_current_state = LOW; +int sensor_disable_debounce = 0; +int sensor_close_prev_state = HIGH; +int sensor_close_current_state = LOW; +int sensor_close_debounce = 0; + +bool btn_lock = false; +bool btn_unlock = false; + +int btn_lock_prev_state = HIGH; +int btn_lock_current_state = LOW; +int btn_lock_debounce = 0; +int btn_unlock_prev_state = HIGH; +int btn_unlock_current_state = LOW; +int btn_unlock_debounce = 0; + int heartbeat = 0; +int servo_status = 0; +int lock_status = 0; +int source_status = 0; + unsigned long current_timestamp = 0; unsigned long heartbeat_timestamp = 0; -unsigned long lock_timestamp = 0; - -int lock_status = 0; - -byte lastSetDimmer; +unsigned long servo_timestamp = 0; ServoController servo(12); // set up channel -//ZUNO_SETUP_CHANNELS(ZUNO_SWITCH_MULTILEVEL(getter, setter)); -//ZUNO_SETUP_DEBUG_MODE(DEBUG_ON); +ZUNO_SETUP_CHANNELS(ZUNO_SWITCH_MULTILEVEL(getter, setter)); +ZUNO_SETUP_DEBUG_MODE(DEBUG_ON); -void lock(int mode){ - if (mode == 1){ +void set_servo(int mode){ + if (mode == SERVO_LOCK){ servo.setValue(40); - }else if (mode == 2){ - servo.setValue(140); + lock_status = SERVO_LOCK; + } + else if (mode == SERVO_UNLOCK){ + servo.setValue(140); + lock_status = SERVO_UNLOCK; + } + else if (mode == SERVO_CENTER){ + servo.setValue(90); + } + if (mode == SERVO_DISABLE){ + //servo.end(); + digitalWrite(SERVO_ENABLE_PIN, LOW); + } + else{ + servo.begin(); + digitalWrite(SERVO_ENABLE_PIN, HIGH); + servo_timestamp = current_timestamp + 500; + } + servo_status = mode; + source_status = 0; +} + +void update_buttons(){ + sensor_lock_current_state = digitalRead(SENSOR_LOCK_PIN); + sensor_handle_current_state = digitalRead(SENSOR_HANDLE_PIN); + sensor_disable_current_state = digitalRead(SENSOR_DISABLE_PIN); + sensor_close_current_state = digitalRead(SENSOR_CLOSE_PIN); + + btn_lock_current_state = digitalRead(BTN_LOCK_PIN); + btn_unlock_current_state = digitalRead(BTN_UNLOCK_PIN); + + // Debounce + if (sensor_lock_current_state != sensor_lock_prev_state) { + if (++sensor_lock_debounce == SIGNAL_DEBOUNCE_CONSTANT) { + if (sensor_lock_current_state == LOW) { + sensor_lock = true; + } + sensor_lock_prev_state = sensor_lock_current_state; + sensor_lock_debounce = 0; + } + } else { + sensor_lock_debounce = 0; + } + if (sensor_handle_current_state != sensor_handle_prev_state) { + if (++sensor_handle_debounce == SIGNAL_DEBOUNCE_CONSTANT) { + if (sensor_handle_current_state == LOW) { + sensor_handle = true; + } + sensor_handle_prev_state = sensor_handle_current_state; + sensor_handle_debounce = 0; + } + } else { + sensor_handle_debounce = 0; + } + if (sensor_disable_current_state != sensor_disable_prev_state) { + if (++sensor_disable_debounce == SIGNAL_DEBOUNCE_CONSTANT) { + if (sensor_disable_current_state == LOW) { + sensor_disable = true; + } + sensor_disable_prev_state = sensor_disable_current_state; + sensor_disable_debounce = 0; + } + } else { + sensor_disable_debounce = 0; + } + if (sensor_close_current_state != sensor_close_prev_state) { + if (++sensor_close_debounce == SIGNAL_DEBOUNCE_CONSTANT) { + if (sensor_close_current_state == LOW) { + sensor_close = true; + } + sensor_close_prev_state = sensor_close_current_state; + sensor_close_debounce = 0; + } + } else { + sensor_close_debounce = 0; + } + if (btn_lock_current_state != btn_lock_prev_state) { + if (++btn_lock_debounce == SIGNAL_DEBOUNCE_CONSTANT) { + if (btn_lock_current_state == LOW) { + if (servo_status == SERVO_DISABLE){ + btn_lock = true; + } + } + btn_lock_prev_state = btn_lock_current_state; + btn_lock_debounce = 0; + } + } else { + btn_lock_debounce = 0; + } + if (btn_unlock_current_state != btn_unlock_prev_state) { + if (++btn_unlock_debounce == SIGNAL_DEBOUNCE_CONSTANT) { + if (btn_unlock_current_state == LOW) { + if (servo_status == SERVO_DISABLE){ + btn_unlock = true; + } + } + btn_unlock_prev_state = btn_unlock_current_state; + btn_unlock_debounce = 0; + } + } else { + btn_unlock_debounce = 0; } - digitalWrite(SERVO_ENABLE_PIN, HIGH); - servo.begin(); - lock_timestamp = current_timestamp + 500; - lock_status = mode; } // the setup routine runs once when you press reset: void setup() { - MY_SERIAL.begin(115200); - MY_SERIAL.println("Starting..."); + RFID.begin(9600); pinMode(LED_UNLOCK_PIN, OUTPUT); // setup pin as output digitalWrite(LED_UNLOCK_PIN, LOW); @@ -52,68 +191,55 @@ void setup() { digitalWrite(LED_LOCK_PIN, LOW); pinMode(SERVO_ENABLE_PIN, OUTPUT); // setup pin as output - digitalWrite(SERVO_ENABLE_PIN, HIGH); + digitalWrite(SERVO_ENABLE_PIN, LOW); - servo.setValue(90); - servo.begin(); + set_servo(SERVO_CENTER); } + // the loop routine runs over and over again forever: void loop() { current_timestamp = millis(); - + + update_buttons(); + + if (btn_lock == true && servo_status == SERVO_DISABLE){ + set_servo(SERVO_LOCK); + btn_lock = false; + btn_unlock = false; + } + else if (btn_unlock == true && servo_status == SERVO_DISABLE){ + set_servo(SERVO_UNLOCK); + btn_unlock = false; + } + // Lock disable timeout - if (lock_status > 0) { - if (current_timestamp >= lock_timestamp) { - if (lock_status < 3) { - servo.setValue(90); - lock_timestamp = current_timestamp + 500; - lock_status = 3; + if (servo_status > SERVO_DISABLE) { + if (current_timestamp >= servo_timestamp) { + if (servo_status < SERVO_CENTER) { + set_servo(SERVO_CENTER); }else{ - servo.end(); - digitalWrite(SERVO_ENABLE_PIN, LOW); - lock_status = 0; + set_servo(SERVO_DISABLE); } } } + // Heartbeat if (current_timestamp >= heartbeat_timestamp) { if (heartbeat == 1) { heartbeat = 0; - digitalWrite(LED_UNLOCK_PIN, HIGH); digitalWrite(LED_LOCK_PIN, HIGH); - lock(1); } else { heartbeat = 1; - //servo.setValue(120); - digitalWrite(LED_UNLOCK_PIN, LOW); digitalWrite(LED_LOCK_PIN, LOW); - lock(2); } - heartbeat_timestamp = current_timestamp + 5000; + heartbeat_timestamp = current_timestamp + 500; } } void setter(byte value) { - byte tempVariable; - // value is a variable, holding a "new value" - // which came from the controller or other Z-Wave device - if (value > 99) { - // by Z-Wave specification, this value can't be more then 99 - value = 99; - } - // but the Servo angle scale ranges from 0 to 180 - // we need to prepare our "value" for this new scale - tempVariable = ((word)value)*180/99; - // now we set the Servo position - servo.setValue(tempVariable); - // let's save our value for the situation, when the controller will ask us about it - lastSetDimmer = value; } -// getter function is called once the controller -// or other device in the network asks Z-Uno about -// it's current level byte getter(void) { // return previously saved (in getter()) value - return lastSetDimmer; + return currentLEDValue; }