Thank you both. Everything is working as designed now. For now, I am just going to stick with the code below. I will put the interrupts in on the next version.
/**
* The MySensors Arduino library handles the wireless radio link and protocol
* between your home built sensors/actuators and HA controller of choice.
* The sensors forms a self healing radio network with optional repeaters. Each
* repeater and gateway builds a routing tables in EEPROM which keeps track of the
* network topology allowing messages to be routed to nodes.
*
* Created by Henrik Ekblad <henrik.ekblad@mysensors.org>
* Copyright (C) 2013-2015 Sensnology AB
* Full contributor list: https://github.com/mysensors/Arduino/graphs/contributors
*
* Documentation: http://www.mysensors.org
* Support Forum: http://forum.mysensors.org
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* version 2 as published by the Free Software Foundation.
*
*******************************
*
* REVISION HISTORY
* Combo sketch built from original motion sensor sketch combined with code from the Relay actuator sketch
* 2x Motion Sensors to be used as garage doors status usualy with reed switches
* 2x Relays to control garage doors
* NOTE! this sketch automaticaly turns the relays back off after a door control has been sent to it
* How long the relays are on is defined vith the variable "RELAY_ONTIME" the default is 300 milliseconds
* http://www.mysensors.org/build/relay
*/
/*
arduino Pro mini 5V 16MHz ????
*/
#define MY_RADIO_NRF24
#define MY_DEBUG // Enables debug messages in the serial log
// Set MY_NODE_ID to something unique in your sensor network (1-254)
// or set to AUTO if you want gw to assign a MY_NODE_ID for you.
#define MY_NODE_ID AUTO
#define SN "DualGarageDoor"
#define SV "1.5"
//#define MY_RF24_CE_PIN 9 // Radio specific settings for RF24
//#define MY_RF24_CS_PIN 10 // Radio specific settings for RF24 (you'll find similar config for RFM69)
//#define MY_RF24_PA_LEVEL RF24_PA_MAX
#include <MyConfig.h>
#include <MySensors.h>
#include <SPI.h>
// Enable repeater functionality for this node
#define MY_REPEATER_FEATURE
#define DIGITAL_INPUT_SENSOR 3 // The digital input for PIR.
#define DIGITAL_INPUT_SENSORA 4 // The digital input for Door 1
#define DIGITAL_INPUT_SENSORB 5 // The digital input for Door 2
#define LIGHT_SENSOR_ANALOG_PIN 0 //light sensor
//#define INTERRUPT DIGITAL_INPUT_SENSORA-2 // Usually the interrupt = pin -2 (on uno/nano anyway)
#define CHILD_ID_PIR 40 // Id of the sensor child
#define CHILD_ID_RELAY 30 //Id of relay
#define CHILD_ID_DOORA 21 // Id of the sensor child
#define CHILD_ID_DOORB 20 // Id of the sensor child
#define CHILD_ID_LIGHT 10 //light sensor
#define RELAY_PIN 6 // Arduino Digital I/O pin number for first relay
#define NUMBER_OF_RELAYS 1 // Total number of attached relays
//define what is on and off for the relay
#define RELAY_ON 1
#define RELAY_OFF 0
//define what is on and off for the reed switches
#define DOOR_CLOSED 0
#define DOOR_OPEN 1
int RELAY_ONTIME = 1000; //delay before turning relay back off
int lastLightLevel; //light sensor
boolean oldValueA = 0;
boolean oldValueB = 0;
boolean trippedA = 0;
boolean trippedB = 0;
uint32_t SLEEP_TIME = 10000; // Sleep time between reads (in milliseconds)
MyMessage msg1(CHILD_ID_DOORA, V_LOCK_STATUS);
MyMessage msg2(CHILD_ID_DOORB, V_LOCK_STATUS);
MyMessage msg4(CHILD_ID_LIGHT, V_LIGHT_LEVEL); //light sensor
MyMessage msg3(CHILD_ID_PIR, V_TRIPPED); //PIR
void before()
{
for (int sensor=1, pin=RELAY_PIN; sensor<=NUMBER_OF_RELAYS; sensor++, pin++) {
// Then set relay pins in output mode
pinMode(pin, OUTPUT);
// Set relay to last known state (using eeprom storage)
digitalWrite(pin, loadState(sensor)?RELAY_ON:RELAY_OFF);
}
}
void setup()
{
pinMode(DIGITAL_INPUT_SENSORA, INPUT); // sets the motion sensor digital pin as input
pinMode(DIGITAL_INPUT_SENSORB, INPUT); // sets the motion sensor digital pin as input
// Activate internal pull-up
digitalWrite(DIGITAL_INPUT_SENSORA, HIGH);
digitalWrite(DIGITAL_INPUT_SENSORB, HIGH);
digitalWrite(RELAY_PIN, RELAY_OFF);
pinMode(RELAY_PIN, OUTPUT);
pinMode(DIGITAL_INPUT_SENSOR, INPUT); // sets the motion sensor digital pin as input
}
void presentation()
{
// Send the Sketch Version Information to the Gateway
Serial.println("Presentation");
sendSketchInfo(SN, SV);
for (int sensor=1, pin=RELAY_PIN; sensor<=NUMBER_OF_RELAYS; sensor++, pin++)
{
present(sensor, S_BINARY);
}
{
trippedA = digitalRead(DIGITAL_INPUT_SENSORA);
trippedB = digitalRead(DIGITAL_INPUT_SENSORB);
//Serial.print("Door A status: ");
//Serial.println(trippedA);
//Serial.print("Door B status: ");
//Serial.println(trippedB);
send(msg1.set(trippedA ? DOOR_CLOSED : DOOR_OPEN));
send(msg2.set(trippedB ? DOOR_CLOSED : DOOR_OPEN));
oldValueA = trippedA;
oldValueB = trippedB;
}
{
// Register all sensors to gw (they will be created as child devices)
present(CHILD_ID_DOORA, S_LOCK);
present(CHILD_ID_DOORB, S_LOCK);
present(CHILD_ID_LIGHT, S_LIGHT_LEVEL);
present(CHILD_ID_PIR, S_MOTION);
}
}
void loop()
{
{
// Read digital reed switch value
trippedA = digitalRead(DIGITAL_INPUT_SENSORA);
trippedB = digitalRead(DIGITAL_INPUT_SENSORB);
// Send value only if changed
if(trippedA != oldValueA)
{
//Serial.print("Door A status: ");
//Serial.println(trippedA);
send(msg1.set(trippedA ? DOOR_CLOSED : DOOR_OPEN));
//gw.sendVariable(CHILD_ID_DOORA, V_LOCK_STATUS, trippedA?DOOR_CLOSED:DOOR_OPEN); // Send value change to gw
delay(1000);
oldValueA = trippedA;
}
// Send value only if changed
if(trippedB != oldValueB)
{
//Serial.print("Door B status: ");
//Serial.println(trippedB);
send(msg2.set(trippedB ? DOOR_CLOSED : DOOR_OPEN));
//gw.sendVariable(CHILD_ID_DOORB, V_LOCK_STATUS, trippedB?DOOR_CLOSED:DOOR_OPEN); // Send value change to gw
delay(1000);
oldValueB = trippedB;
}
}
{
int16_t lightLevel = (1023-analogRead(LIGHT_SENSOR_ANALOG_PIN))/10.23;
//Serial.println(lightLevel);
if (abs(lightLevel - lastLightLevel)>20) {
send(msg4.set(lightLevel));
lastLightLevel = lightLevel;
}
}
{
// Read digital motion value
bool tripped = digitalRead(DIGITAL_INPUT_SENSOR) == HIGH;
Serial.println(tripped);
send(msg3.set(tripped?"1":"0")); // Send tripped value to gw
// Sleep until interrupt comes in on motion sensor. Send update every two minute.
wait(digitalPinToInterrupt(DIGITAL_INPUT_SENSOR), CHANGE, SLEEP_TIME);
}
}
void receive(const MyMessage &message)
{
// We only expect one type of message from controller. But we better check anyway.
if (message.type==V_STATUS) {
// Change relay state
digitalWrite(RELAY_PIN, RELAY_ON);
wait(RELAY_ONTIME);
digitalWrite(RELAY_PIN, RELAY_OFF);
wait(2000);
}
}