Updated test code

This commit is contained in:
Christoffer Martinsson 2018-02-13 11:30:26 +01:00
parent 0f8613a4da
commit 35828f5242

View File

@ -1,5 +1,7 @@
#include <ZUNO_SERVO.h> #include <ZUNO_SERVO.h>
#define MY_SERIAL Serial
#define SERVO_PIN 12 #define SERVO_PIN 12
#define SERVO_ENABLE_PIN 9 #define SERVO_ENABLE_PIN 9
@ -15,20 +17,35 @@ unsigned long int rfid = 0;
int heartbeat = 0; int heartbeat = 0;
unsigned long current_timestamp = 0; unsigned long current_timestamp = 0;
unsigned long heartbeat_timestamp = 0; unsigned long heartbeat_timestamp = 0;
unsigned long lock_timestamp = 0;
ServoController servo(SERVO_PIN); int lock_status = 0;
byte lastSetDimmer;
ServoController servo(12);
// set up channel // set up channel
ZUNO_SETUP_CHANNELS( //ZUNO_SETUP_CHANNELS(ZUNO_SWITCH_MULTILEVEL(getter, setter));
ZUNO_SENSOR_MULTILEVEL(ZUNO_SENSOR_MULTILEVEL_TYPE_GENERAL_PURPOSE_VALUE, //ZUNO_SETUP_DEBUG_MODE(DEBUG_ON);
SENSOR_MULTILEVEL_SCALE_PERCENTAGE_VALUE,
SENSOR_MULTILEVEL_SIZE_FOUR_BYTES, void lock(int mode){
SENSOR_MULTILEVEL_PRECISION_ZERO_DECIMALS, if (mode == 1){
rfid_getter) servo.setValue(40);
); }else if (mode == 2){
servo.setValue(140);
}
digitalWrite(SERVO_ENABLE_PIN, HIGH);
servo.begin();
lock_timestamp = current_timestamp + 1000;
lock_status = mode;
}
// the setup routine runs once when you press reset: // the setup routine runs once when you press reset:
void setup() { void setup() {
MY_SERIAL.begin(115200);
MY_SERIAL.println("Starting...");
pinMode(LED_UNLOCK_PIN, OUTPUT); // setup pin as output pinMode(LED_UNLOCK_PIN, OUTPUT); // setup pin as output
digitalWrite(LED_UNLOCK_PIN, LOW); digitalWrite(LED_UNLOCK_PIN, LOW);
pinMode(LED_LOCK_PIN, OUTPUT); // setup pin as output pinMode(LED_LOCK_PIN, OUTPUT); // setup pin as output
@ -42,23 +59,54 @@ void setup() {
// the loop routine runs over and over again forever: // the loop routine runs over and over again forever:
void loop() { void loop() {
current_timestamp = millis(); current_timestamp = millis();
// Lock disable timeout
if (lock_status > 0) {
if (current_timestamp >= lock_timestamp) {
servo.end();
digitalWrite(SERVO_ENABLE_PIN, LOW);
lock_status = 0;
}
}
// Heartbeat // Heartbeat
if (current_timestamp >= heartbeat_timestamp) { if (current_timestamp >= heartbeat_timestamp) {
if (heartbeat == 1) { if (heartbeat == 1) {
heartbeat = 0; heartbeat = 0;
servo.setValue(60);
digitalWrite(LED_UNLOCK_PIN, HIGH); digitalWrite(LED_UNLOCK_PIN, HIGH);
digitalWrite(LED_LOCK_PIN, HIGH); digitalWrite(LED_LOCK_PIN, HIGH);
lock(1);
} else { } else {
heartbeat = 1; heartbeat = 1;
servo.setValue(120); //servo.setValue(120);
digitalWrite(LED_UNLOCK_PIN, LOW); digitalWrite(LED_UNLOCK_PIN, LOW);
digitalWrite(LED_LOCK_PIN, LOW); digitalWrite(LED_LOCK_PIN, LOW);
lock(2);
} }
heartbeat_timestamp = current_timestamp + 2000; heartbeat_timestamp = current_timestamp + 5000;
} }
} }
unsigned long int rfid_getter(){ void setter(byte value) {
return rfid; 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;
} }