Hi to all.
I'm "almost" new to this forum (I signed up years ago but never used it, before).
I hope that someboidy could give me a hint.
I wrote a big sketch for Arduino UNO a couple of months ago, but very soon I found UNO's limits in terms of memory and avaliable pins. I wrote alot of skecthes for UNO before this moment, and never found that limit until a couple of weeks ago.
So I decided to use a Mega2560 for this specific device, instead.
Installed MEGA on the same box of UNO and connected all the pins.
My sketch was perfectly running on UNO before, and it still works for the new Mega, right now, excepted for its sleep-interrupt-wake-up function.
I got almost crazy, so I wrote a very simple sketch in order to test all the single steps.
I've to put Arduino in sleep mode and then let it wake-up every minute, after a certain amount of time or when a button is pressed (it depends upon certain external events).
All the interrupts seem to work well, when used as simple interrupts, but when the sketch puts Mega in sleep mode... it stops to work forever.
Mega does not wake up anymore.
This sketch works normally if I delete the "sleep mode" related lines, the interrupts are correctly received and the LEDs change their state, but does not, when Arduino is put in sleep mode.
It "dies" literally, in this case, and there is not any chance to wake it up.
I never use the 2560's internal resistors with the "input_pullup" option, as I connect a 47K resistor for each button vs. ground and the button work flawlessly (as always).
All the buttons are connected togheter via diodes in order to obtain an unique HIGH state to be connected to pin 3, and -of course- this solution works perfectly as always with UNO and 2560.
#include <DS3232RTC.h>
#include <Streaming.h>
#include <Wire.h>
#include <avr/interrupt.h>
#include <avr/sleep.h>
const byte ledPin = 9;
const byte ledRed = 10;
const byte interruptPin = 3;
const byte interruptRTC = 2;
volatile byte state = LOW;
volatile byte state1 = HIGH;
bool RTCInterruptDetected=false;
int cycleNum=0;
void setup() {
Serial.begin(9600);
Serial.println("Serial port OK");
pinMode(ledPin, OUTPUT);
pinMode(ledRed, OUTPUT);
pinMode(interruptPin, INPUT);
pinMode(interruptRTC, INPUT);
Serial.println(" starting to set RTC params ");
RTC.alarm(ALARM_1);
RTC.alarm(ALARM_2);
RTC.squareWave(SQWAVE_NONE);
RTC.alarmInterrupt(ALARM_1, false);
RTC.alarmInterrupt(ALARM_2, true); .
RTC.setAlarm(ALM2_EVERY_MINUTE, 0, 0, 0, 0);
Serial.println(" RTC params set ");
attachInterrupt(digitalPinToInterrupt(interruptPin), blink, CHANGE);
attachInterrupt(digitalPinToInterrupt(interruptRTC), blink1, FALLING);
}
void loop() {
digitalWrite(ledPin, state);
digitalWrite(ledRed, state1);
if (RTCInterruptDetected) {
RTCInterruptDetected = false;
cycleNum++;
Serial.print(" Entered in IF, cycle #: ");
Serial.println(cycleNum);
Serial.println(" Entered in IF, deleting ALARM1 e ALARM2 in RTC");
RTC.alarm(ALARM_1);
RTC.alarm(ALARM_2);
Serial.println(" Entered in IF resetting RTC parameters");
RTC.squareWave(SQWAVE_NONE); /
RTC.alarmInterrupt(ALARM_1, false);
RTC.alarmInterrupt(ALARM_2, true);
RTC.setAlarm(ALM2_EVERY_MINUTE, 0, 0, 0, 0);
Serial.println(" Entered in IF resetting interrupts paramneters");
attachInterrupt(digitalPinToInterrupt(interruptPin), blink, CHANGE);
attachInterrupt(digitalPinToInterrupt(interruptRTC), blink1, FALLING);
}
Serial.println("setting Arduino's params for the sleep mode");
set_sleep_mode(SLEEP_MODE_PWR_DOWN);
sleep_enable();
delay(100);
Serial.println("next line will put Arduino in sleep mode");
delay(100);
sleep_mode();
Serial.println("Woke Up");
sleep_disable();
Serial.println("sleep bit disabled");
}
void blink() {
state = !state;
Serial.println(" Button Interrupt detected");
}
void blink1() {
detachInterrupt(digitalPinToInterrupt(interruptRTC));
RTCInterruptDetected = true;
state1 = !state1;
}