Works perfectly untill i introduce sleep
-
Hi all
thanks for all previous help with this node, im only 1 step away now from getting it to work
the sketch works perfectly untill i introduce the sleep till interupt.....as soon i uncomment the line for sleep the node starts up then goes to sleep and never wakes up again... and advice is appreciated
LOG
__ __ ____ | \/ |_ _/ ___| ___ _ __ ___ ___ _ __ ___ | |\/| | | | \___ \ / _ \ `_ \/ __|/ _ \| `__/ __| | | | | |_| |___| | __/ | | \__ \ _ | | \__ \ |_| |_|\__, |____/ \___|_| |_|___/\___/|_| |___/ |___/ 2.3.1 16 MCO:BGN:INIT NODE,CP=RNNNA---,REL=255,VER=2.3.1 28 TSM:INIT 28 TSF:WUR:MS=0 34 TSM:INIT:TSP OK 36 TSF:SID:OK,ID=4 38 TSM:FPAR 75 TSF:MSG:SEND,4-4-255-255,s=255,c=3,t=7,pt=0,l=0,sg=0,ft=0,st=OK: 110 TSF:MSG:READ,1-1-4,s=255,c=3,t=8,pt=1,l=1,sg=0:1 116 TSF:MSG:FPAR OK,ID=1,D=2 2084 TSM:FPAR:OK 2084 TSM:ID 2086 TSM:ID:OK 2088 TSM:UPL 2093 TSF:MSG:SEND,4-4-1-0,s=255,c=3,t=24,pt=1,l=1,sg=0,ft=0,st=OK:1 2127 TSF:MSG:READ,0-1-4,s=255,c=3,t=25,pt=1,l=1,sg=0:2 2134 TSF:MSG:PONG RECV,HP=2 2138 TSM:UPL:OK 2138 TSM:READY:ID=4,PAR=1,DIS=2 2144 TSF:MSG:SEND,4-4-1-0,s=255,c=3,t=15,pt=6,l=2,sg=0,ft=0,st=OK:0100 2170 TSF:MSG:READ,0-1-4,s=255,c=3,t=15,pt=6,l=2,sg=0:0100 2201 TSF:MSG:SEND,4-4-1-0,s=255,c=0,t=17,pt=0,l=5,sg=0,ft=0,st=OK:2.3.1 2226 TSF:MSG:SEND,4-4-1-0,s=255,c=3,t=6,pt=1,l=1,sg=0,ft=0,st=OK:1 2260 TSF:MSG:READ,0-1-4,s=255,c=3,t=6,pt=0,l=1,sg=0:M 2299 TSF:MSG:SEND,4-4-1-0,s=255,c=3,t=11,pt=0,l=12,sg=0,ft=0,st=OK:Lounge Multi 2326 TSF:MSG:SEND,4-4-1-0,s=255,c=3,t=12,pt=0,l=3,sg=0,ft=0,st=OK:3.0 2435 TSF:MSG:SEND,4-4-1-0,s=1,c=0,t=3,pt=0,l=0,sg=0,ft=0,st=OK: 2443 MCO:REG:REQ 2449 TSF:MSG:SEND,4-4-1-0,s=255,c=3,t=26,pt=1,l=1,sg=0,ft=0,st=OK:2 2478 TSF:MSG:READ,0-1-4,s=255,c=3,t=27,pt=1,l=1,sg=0:1 2484 MCO:PIM:NODE REG=1 2486 MCO:BGN:STP 2488 MCO:BGN:INIT OK,TSP=1 2496 TSF:MSG:SEND,4-4-1-0,s=1,c=1,t=2,pt=2,l=2,sg=0,ft=0,st=OK:0 2504 MCO:SLP:MS=0,SMS=0,I1=3,M1=1,I2=255,M2=255 2508 TSF:TDI:TSL
Code:
// Enable debug prints to serial monitor #define MY_DEBUG // Enable and select radio type attached #define MY_RADIO_RF24 //#define MY_RADIO_RFM69 #include <MySensors.h> #include <Bounce2.h> #define CHILD_ID 1 #define BUTTON_PIN 3 // Arduino Digital I/O pin for button/reed switch Bounce debouncer = Bounce(); int oldValue=-1; // Change to V_LIGHT if you use S_LIGHT in presentation below MyMessage msg(CHILD_ID,V_LIGHT); //========================= // BATTERY VOLTAGE DIVIDER SETUP // 1M, 470K divider across battery and using internal ADC ref of 1.1V // Sense point is bypassed with 0.1 uF cap to reduce noise at that point // ((1e6+470e3)/470e3)*1.1 = Vmax = 3.44 Volts // 3.44/1023 = Volts per bit = 0.003363075 #define VBAT_PER_BITS 0.003363075 #define VMIN 1.9 // Vmin (radio Min Volt)=1.9V (564v) #define VMAX 3.0 // Vmax = (2xAA bat)=3.0V (892v) int batteryPcnt = 0; // Calc value for battery % int batLoop = 0; // Loop to help calc average int batArray[3]; // Array to store value for average calc. int BATTERY_SENSE_PIN = A0; // select the input pin for the battery sense point //========================= void setup() { // Setup the button pinMode(BUTTON_PIN,INPUT); // Activate internal pull-up digitalWrite(BUTTON_PIN,HIGH); // After setting up the button, setup debouncer debouncer.attach(BUTTON_PIN); debouncer.interval(5); } void presentation() { // 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. sendSketchInfo("Lounge Multi", "3.0"); wait(100); present(CHILD_ID, S_LIGHT); } // 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 send(msg.set(value==HIGH ? 1 : 0)); oldValue = value; } // batM(); sleep(BUTTON_PIN, CHANGE, 0); } void batM() //The battery calculations { delay(500); // Battery monitoring reading int sensorValue = analogRead(BATTERY_SENSE_PIN); delay(500); // Calculate the battery in % float Vbat = sensorValue * VBAT_PER_BITS; int batteryPcnt = static_cast<int>(((Vbat-VMIN)/(VMAX-VMIN))*100.); Serial.print("Battery percent: "); Serial.print(batteryPcnt); Serial.println(" %"); // Add it to array so we get an average of 3 (3x20min) batArray[batLoop] = batteryPcnt; if (batLoop > 2) { batteryPcnt = (batArray[0] + batArray[1] + batArray[2] + batArray[3]); batteryPcnt = batteryPcnt / 3; if (batteryPcnt > 100) { batteryPcnt=100; } Serial.print("Battery Average (Send): "); Serial.print(batteryPcnt); Serial.println(" %"); sendBatteryLevel(batteryPcnt); batLoop = 0; } else { batLoop++; } }
-
additional note my button is connected between GND and D3 with 470k resistor in the middle
-
Hi markjgabb,
#define BUTTON_PIN 3 ... sleep(BUTTON_PIN, CHANGE, 0);
Assuming you use an Arduino, you can use two external interrupts (INT0 and INT1) on pins D2 and D3. But the sleep function requires the external interrupt vector, not the Arduino pin number. When you give it
BUTTON_PIN
(3), nothing will happen, since only INT0 (0) and INT1 (1) are valid external interrupts on ATmega328P-based Arduinos.Instead, you could write
sleep(1, CHANGE, 0)
, or preferablysleep(digitalPinToInterrupt(BUTTON_PIN), CHANGE, 0)
, which maps the pin number to the interrupt vector (orNOT_AN_INTERRUPT
(-1) if invalid), if you want to wake up from D3 (INT1). 0 would wake up from D2 (INT0).For more information, there's a good tutorial on interrupts by Nick Gammon. Also, chapters 12 "Interrupts" and 13 "External Interrupts" in the ATmega328 datasheet might be helpful.
-
@markjgabb said in Works perfectly untill i introduce sleep:
additional note my button is connected between GND and D3 with 470k resistor in the middle
This will be an unstable way to detect a change as the input pin is 'floating' unless the button is connected when it then gets grounded via the resistor.
What would be better is connect the resistor between the positive power of the arduino and the input pin (this is known as a pull-up resistor) and then add the switch from the input pin to ground.
In this configuration the input pin is at all times in a known state, either it is 'high' when the button is not pressed or 'low' when the button is pressed. This will give a clear signal to the arduino.