- Posts: 4
- Thank you received: 2
Please Log in or Create an account to join the conversation.
// Simple binary switch example
// Connect button or door/window reed switch between
// digitial I/O pin 3 (BUTTON_PIN1 below) and GND.
#include <avr/sleep.h> // Sleep Modes
#include <MySensor.h>
#include <SPI.h>
#include <Bounce2.h>
#define CHILD_ID1 3
#define CHILD_ID2 4
#define CHILD_ID3 5
#define BUTTON_PIN 3 // Arduino Digital I/O pin for button/reed switch
MySensor gw;
#define INTERRUPT_SENSOR 1 // Interrupt 1 is on DIGITAL PIN 3!
#define BATTERY_SENSE_PIN
#define SLEEP_IN_MS 86400000 // 1 day
byte val;
int oldBatLevel;
Bounce debouncer = Bounce();
int oldValue=-1;
// Change to V_LIGHT if you use S_LIGHT in presentation below
MyMessage msg(CHILD_ID1,V_TRIPPED);
MyMessage msg2(CHILD_ID2,V_TRIPPED);
MyMessage msg3(CHILD_ID3,V_TRIPPED);
void setup()
{
gw.begin();
// Setup the button
pinMode(BUTTON_PIN,INPUT_PULLUP);
// After setting up the button, setup debouncer
debouncer.attach(BUTTON_PIN);
debouncer.interval(5);
pinMode(4,INPUT_PULLUP);
pinMode(5,INPUT_PULLUP);
pinMode(6,INPUT_PULLUP);
pinMode(7,INPUT_PULLUP);
pinMode(8,INPUT_PULLUP);
// Register binary input sensor to gw (they will be created as child devices)
// You can use S_DOOR, S_MOTION or S_LIGHT here depending on your usage.
// If S_LIGHT is used, remember to update variable type you send in. See "msg" above.
gw.present(CHILD_ID1, S_DOOR);
gw.present(CHILD_ID2, S_DOOR);
gw.present(CHILD_ID3, S_DOOR);
val = digitalRead(BUTTON_PIN);
oldBatLevel = -1;
sendValue();
}
// Check if digital input has changed and send in new value
void loop()
{
debouncer.update();
// Get the update value
int value = debouncer.read();
if (value != oldValue) {
// Send in the new value
gw.send(msg.set(value==HIGH ? 1 : 0));
oldValue = value;
}
}
void sendValue()
{
gw.powerUp();
gw.send(msg.set(val==HIGH ? 1 : 0));
//
// Here is where we want to add the extra sensors and the GW.send address needs to change.
//
gw.powerDown();
int batLevel = getBatteryLevel();
if (oldBatLevel != batLevel)
{
gw.powerUp();
gw.sendBatteryLevel(batLevel);
oldBatLevel = batLevel;
}
}
// Battery measure
int getBatteryLevel ()
{
int results = (readVcc() - 2000) / 10;
if (results > 100)
results = 100;
if (results < 0)
results = 0;
return results;
} // end of getBandgap
// when ADC completed, take an interrupt
EMPTY_INTERRUPT (ADC_vect);
long readVcc() {
long result;
// Read 1.1V reference against AVcc
ADMUX = _BV(REFS0) | _BV(MUX3) | _BV(MUX2) | _BV(MUX1);
delay(2); // Wait for Vref to settle
noInterrupts ();
// start the conversion
ADCSRA |= _BV (ADSC) | _BV (ADIE);
set_sleep_mode (SLEEP_MODE_ADC); // sleep during sample
interrupts ();
sleep_mode ();
// reading should be done, but better make sure
// maybe the timer interrupt fired
while (bit_is_set(ADCSRA,ADSC));
result = ADCL;
result |= ADCH<<8;
result = 1126400L / result; // Back-calculate AVcc in mV
return result;
}
Please Log in or Create an account to join the conversation.
Please Log in or Create an account to join the conversation.