Getting Pin Change Interrupts working together with Timer interrupts / sleep(XX ms) on Arduino Pro Mini
-
// Enable debug prints to serial monitor #define MY_DEBUG // Enable and select radio type attached #define MY_RADIO_RF24 #define MY_RADIO_RF24_PA_MAX // #define MY_RF24_CE_PIN 8 // used when Radio is wired non conventional // #define MY_RF24_CS_PIN 7 // #include <MySensors.h> #define MY_NODE_ID 10 // if this works you can create your own NODES #define SKETCH_NAME "Remote Control" #define SKETCH_MAJOR_VER "1" #define SKETCH_MINOR_VER "5" // define your children here #define PRIMARY_CHILD_ID 7 #define SECONDARY_CHILD_ID 8 #define PRIMARY_CHILD2_ID 9 #define SECONDARY_CHILD2_ID 10 // construct your button pins here for the analog inputs you have created const int PRIMARY_BUTTON3_PIN_OUT = 5; const int SECONDARY_BUTTON4_PIN_OUT = 7 ; // These are the primary and secondary main buttons #define PRIMARY_BUTTON_PIN 2 // Arduino Digital I/O pin for button/reed switch #define SECONDARY_BUTTON_PIN 3 // Arduino Digital I/O pin for button/reed switch // these are the 1ast 2 analog pin chosen #define PRIMARY_BUTTON3_PIN 4 // Arduino Digital I/O pin for button/reed switch #define SECONDARY_BUTTON4_PIN 6 // Arduino Digital I/O pin for button/reed switch #if (PRIMARY_BUTTON_PIN < 2 || PRIMARY_BUTTON_PIN >7) #error PRIMARY_BUTTON_PIN must be either 2 or 3 for interrupts to work #endif #if (SECONDARY_BUTTON_PIN < 2 || SECONDARY_BUTTON_PIN > 7) #error SECONDARY_BUTTON_PIN must be either 2 or 3 for interrupts to work #endif #if (PRIMARY_BUTTON_PIN == SECONDARY_BUTTON_PIN) #error PRIMARY_BUTTON_PIN and BUTTON_PIN2 cannot be the same #endif #if (PRIMARY_CHILD_ID == SECONDARY_CHILD_ID) #error PRIMARY_CHILD_ID and SECONDARY_CHILD_ID cannot be the same #endif // create you messages here // Change to V_LIGHT if you use S_LIGHT in presentation below MyMessage msg(PRIMARY_CHILD_ID, V_TRIPPED); MyMessage msg2(SECONDARY_CHILD_ID, V_TRIPPED); MyMessage msg3(PRIMARY_CHILD2_ID, V_TRIPPED); MyMessage msg4(SECONDARY_CHILD2_ID, V_TRIPPED); // ******************************************** ISR (PCINT2_vect) { // handle pin change interrupt for D0 to D7 here if (PIND & bit (4)) // if it was high PORTD |= bit (5); // turn on D5 else PORTD &= ~bit (5); // turn off D5 if (PIND & bit (6)) // if it was high PORTD |= bit (7); // turn on D7 else PORTD &= ~bit (7); // turn off D7 } // end of PCINT2_vect //************************************************* void setup() { // Setup the buttons pinMode(PRIMARY_BUTTON_PIN, INPUT_PULLUP); pinMode(SECONDARY_BUTTON_PIN, INPUT_PULLUP); //********************************************************** // pin change interrupt (D4) PCMSK2 |= bit (PCINT20); // want pin 4 PCIFR |= bit (PCIF2); // clear any outstanding interrupts PCICR |= bit (PCIE2); // enable pin change interrupts for D0 to D7 pinMode(PRIMARY_BUTTON3_PIN, INPUT_PULLUP); pinMode (PRIMARY_BUTTON3_PIN_OUT, OUTPUT); // pin change interrupt (D6) PCMSK2 |= bit (PCINT22); // want pin 6 PCIFR |= bit (PCIF2); // clear any outstanding interrupts PCICR |= bit (PCIE2); // enable pin change interrupts for D0 to D7 pinMode (SECONDARY_BUTTON4_PIN, INPUT_PULLUP); pinMode (SECONDARY_BUTTON4_PIN_OUT, OUTPUT); //********************************************************************** // Send the sketch version information to the gateway and Controller sendSketchInfo(SKETCH_NAME, SKETCH_MAJOR_VER "." SKETCH_MINOR_VER); // Register binary input sensor to sensor_node (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. present(PRIMARY_CHILD_ID, S_DOOR); present(SECONDARY_CHILD_ID, S_DOOR); present(PRIMARY_CHILD2_ID, S_DOOR); present(SECONDARY_CHILD2_ID, S_DOOR); } // Loop will iterate on changes on the BUTTON_PINs void loop() { uint8_t value; static uint8_t sentValue = 2; static uint8_t sentValue2 = 2; static uint8_t sentValue3 = 2; static uint8_t sentValue4 = 2; // short delay to allow buttons to properly settle delay(5); value = digitalRead(PRIMARY_BUTTON_PIN); if (value != sentValue) { // Value has changed from last transmission, send the updated value send(msg.set(value == HIGH)); Serial.print("Value of Primary Button > "); Serial.println(digitalRead(value)); sentValue = value; } value = digitalRead(SECONDARY_BUTTON_PIN); if (value != sentValue2) { // Value has changed from last transmission, send the updated value send(msg2.set(value == HIGH)); Serial.print("Value of Secondary Button > "); Serial.println(digitalRead(value)); sentValue2 = value; } value = digitalRead(PRIMARY_BUTTON3_PIN); if (value != sentValue3) { send(msg3.set(value == HIGH)); Serial.print("Value of Primary Button 3 > "); Serial.println(digitalRead(PRIMARY_BUTTON3_PIN_OUT)); sentValue3 = value; } value = digitalRead(SECONDARY_BUTTON4_PIN); if (value != sentValue4) { send(msg4.set(value == HIGH)); Serial.print("Value of Secondary Button 4 > "); Serial.println(digitalRead(SECONDARY_BUTTON4_PIN_OUT)); sentValue4 = value; } // Sleep until something happens with the sensor sleep(PRIMARY_BUTTON_PIN - 2, CHANGE, 0, SECONDARY_BUTTON_PIN - 2, CHANGE, 0); } -
Thanks, will recap your code for identifying the pressed button, seems pretty concise.
Though this code in general will in my experience not work together with a "timed" sleep() - you have sleep(...,0), and my goal is to get PCInterrupts and Timer interrupts working together.After some ongoing testing, the sketch I posted above works in achieving both.
Now I still have to figure out a way how to sleep() for the remaining sleep interval after the node was awoken with a button press /PCInt (say, I want a sensor reading strictly every 4 hours (sleep(4 * 60 * 60 * 1000), but wake the node after 90minutes with a button press. Now I'd need a way to figure out the remainder of the sleep interval (2.5h)).
Thanks,Joost
-
Thanks, will recap your code for identifying the pressed button, seems pretty concise.
Though this code in general will in my experience not work together with a "timed" sleep() - you have sleep(...,0), and my goal is to get PCInterrupts and Timer interrupts working together.After some ongoing testing, the sketch I posted above works in achieving both.
Now I still have to figure out a way how to sleep() for the remaining sleep interval after the node was awoken with a button press /PCInt (say, I want a sensor reading strictly every 4 hours (sleep(4 * 60 * 60 * 1000), but wake the node after 90minutes with a button press. Now I'd need a way to figure out the remainder of the sleep interval (2.5h)).
Thanks,Joost
@Joost RTC?
-
@Joost RTC?
@zboblamont Ah, ok.
To be honest, I had hoped there'd be a counter/register (for the timer interrupt - how does the Arduino keep track of itself otherwise?) I could read and then sleep() for the remainder...I'll browse around later and see if I can dig up something on the web.
Thanks! -
How does MySensors accomplish long sleep() times? Is it like
sleep() with Watchdog Timer set for 8s -> calculate remainder -> sleep() again?
If so, I could maybe find the var in which MySensors itself keeps track of sleep time.@Joost There has been an attempt to do this a few years ago. I don't know if they ever got it working though: https://forum.mysensors.org/topic/7197/sleep-time-and-external-interrrupts
Besides that, you could request the current time from the controller on interrupts and calculate the remaining sleep duration from there or, as mentioned before, use an external RTC.
-
How does MySensors accomplish long sleep() times? Is it like
sleep() with Watchdog Timer set for 8s -> calculate remainder -> sleep() again?
If so, I could maybe find the var in which MySensors itself keeps track of sleep time. -
@zboblamont Ah, ok.
To be honest, I had hoped there'd be a counter/register (for the timer interrupt - how does the Arduino keep track of itself otherwise?) I could read and then sleep() for the remainder...I'll browse around later and see if I can dig up something on the web.
Thanks!@Joost My only thought on the RTC is separation of the timing aspect allowing the processor to go deep-sleep forever until Interrupt. I have one set up to hourly scan my water tank, but I can press a button in parallel with the interrupt to ground for ad-hoc update.
-
@Joost My only thought on the RTC is separation of the timing aspect allowing the processor to go deep-sleep forever until Interrupt. I have one set up to hourly scan my water tank, but I can press a button in parallel with the interrupt to ground for ad-hoc update.
@zboblamont Thanks, will think about that if a pure software approach fails! Though I'd really like to keep complexity down; I'm planning on fabricating at least 10-20 nodes of that kind (by hand).
-
@Joost I implemented a way to get the remaining sleep time, albeit only roughly on AVR: https://forum.mysensors.org/topic/9595/interrupted-sleep/11
@Yveaux Thanks, very cool and promising, will take a more in depth look tomorrow!
I just threw that in my code quickly tonight, so far I got a 5 digit number back, but that was always the same also with different wake up delays via PCInt after going to sleep(). -
@Yveaux Thanks, very cool and promising, will take a more in depth look tomorrow!
I just threw that in my code quickly tonight, so far I got a 5 digit number back, but that was always the same also with different wake up delays via PCInt after going to sleep().@Joost as documented it can be roughly 8 seconds off on avr. When an avr is set to sleep for 8 seconds (the maximum watchdog sleep time) and it is woken by an interrupt, it will not know how much time is remaining and always return the same time remaining.
Try sleeping for a minute, then interrupt after 1 second and after half a minute. The remaining time should be different. -
@Joost as documented it can be roughly 8 seconds off on avr. When an avr is set to sleep for 8 seconds (the maximum watchdog sleep time) and it is woken by an interrupt, it will not know how much time is remaining and always return the same time remaining.
Try sleeping for a minute, then interrupt after 1 second and after half a minute. The remaining time should be different.@Yveaux said in Getting Pin Change Interrupts working together with Timer interrupts / sleep(XX ms) on Arduino Pro Mini:
@Joost as documented it can be roughly 8 seconds off on avr. When an avr is set to sleep for 8 seconds (the maximum watchdog sleep time) and it is woken by an interrupt, it will not know how much time is remaining and always return the same time remaining.
Try sleeping for a minute, then interrupt after 1 second and after half a minute. The remaining time should be different.@Yveaux Hi, yeah, I was already expecting the 8s inaccuracy (which is no problem at all, I am planning to cover intervals of several hours).
I tried yesterday night with sleep times of 1-5 minutes and could not get it to work at that point (always receiving identical numbers though the time to awakening the node with PCInt differed), but this might be because I just hacked it in an otherwise more complicated sketch.I'll start now with making up a minimal test sketch to get the hang of it!
Thanks, J -
@Joost as documented it can be roughly 8 seconds off on avr. When an avr is set to sleep for 8 seconds (the maximum watchdog sleep time) and it is woken by an interrupt, it will not know how much time is remaining and always return the same time remaining.
Try sleeping for a minute, then interrupt after 1 second and after half a minute. The remaining time should be different.@Yveaux & others:
So this works great! Here is an example sketch to get Pin Change Interrupts working together with timer-based sleep(). There will be identification of the triggered pin as well as discrimination between rising and falling edge. To test, just add a jumper wire to pin A1 or/and A2 to a Arduino Pro Mini and short it to Ground to wake the Arduino out of a timed sleep() (a connected Radio, here a RFM95, is needed to get the sketch started).#include <Arduino.h> #define MY_RADIO_RFM95 #include <MySensors.h> volatile int isrcounter = 0, timercounter=0; volatile char PinRegLast, ChangedPin, PinRegister; volatile boolean isr_interrupted; uint32_t remainingSleepTime = 0; ISR(PCINT1_vect) { PinRegister = PINC; ChangedPin = PinRegister ^ PinRegLast; PinRegLast = PinRegister; _wokeUpByInterrupt = 0xFE; isr_interrupted=true; isrcounter++; } void setup() { noInterrupts(); PCICR |= 0b00000011; // Enables Ports B and C Pin Change Interrupts PCMSK1 |= 0b00000110; //PCINT9 & PCINT10 = A1 & A2 PCIFR = B00000001; // Reset Interruptflag pinMode(A1, INPUT_PULLUP); pinMode(A2, INPUT_PULLUP); PinRegister = PINC; interrupts(); Serial.begin(38400); } void presentation() {} void loop() { noInterrupts(); if ( isr_interrupted ) { isr_interrupted = false; switch (ChangedPin){ case (1 << PINC1): if (~PinRegister & (1 << PINC1)) { Serial.print("Interrupted / Woken up by Pin A1, remaining sleep() time: "); remainingSleepTime = getSleepRemaining(); Serial.println( remainingSleepTime );} else { Serial.println("Falling edge of Pin A1"); } break; case (1 << PINC2): if (~PinRegister & (1 << PINC2)) { Serial.print("Interrupted / Woken up by Pin A2, remaining sleep() time: "); remainingSleepTime = getSleepRemaining(); Serial.println( remainingSleepTime );} else { Serial.println("Falling edge of Pin A2"); } break; default: break; interrupts(); } } else { interrupts(); Serial.println("Timer wake up / Loop"); timercounter++; } Serial.print(" "); Serial.print(" ISR Counter: ");Serial.print(isrcounter); Serial.print(" Timer counter: "); Serial.println(timercounter); sleep((uint32_t) 60*1000); }Thanks a lot! Joost
-
@Joost I like your solution to be able to use other pins as pin change interrupts together with Timer interrupts in the MySensors library.
I did use the default sleep function of MySensors with the foreseen D1 and D2 pins where D1 is already taken by the radio, so only D2 is usable for custom use.
See https://www.mysensors.org/download/sensor_api_20#sleeping -
I think this is the same thing as you were trying to accomplish.
Sleep for 6 minutes or when interrupted. I have a reed switch on pin 2
pinMode(REED_SWITCH, INPUT); // set the reed switch digital pin as input //digitalWrite(REED_SWITCH, HIGH); pinMode(REED_SWITCH, INPUT_PULLUP);sleep(digitalPinToInterrupt(REED_SWITCH), CHANGE, 360000); //3600000 hour