This code worked normally initially but then stopped working, now the only thing that shows me it's not hardware problem is when I comment out the line with the second interrupt declaration. ( attachInterrupt digitalPinToInterrupt(LightOffPin), buttonpressed, FALLING)
.
And no, I tested the button and it is not stuck.
Any ideas?
#include <Wire.h>
#include <TimeLib.h>
#include <DS3232RTC.h>
#define LightLevelPin 9
#define LightOffPin 3
#define AlarmPin 2
#define IndicatorLED 13
double LightLevel = 0;
double Base = 0.0039215;
byte AlarmH = 18;
byte AlarmM = 10; // Set 30 minutes before wake time
volatile boolean WakeBoss = false;
void wakeshining();
void setup() {
pinMode(LightLevelPin, OUTPUT);
pinMode(IndicatorLED, OUTPUT);
pinMode(AlarmPin, INPUT_PULLUP);
pinMode(LightOffPin, INPUT_PULLUP);
// Serial.begin(9600);
// while (!Serial) ; // wait for serial
//delay(200);
RTC.setAlarm(ALM2_MATCH_DATE, 0, 0, 0, 1);
RTC.alarm(ALARM_2);
RTC.alarmInterrupt(ALARM_1, false);
RTC.alarmInterrupt(ALARM_2, false);
RTC.squareWave(SQWAVE_NONE);
RTC.setAlarm(ALM2_MATCH_HOURS, 0, AlarmM, AlarmH, 0);
RTC.alarm(ALARM_2);
RTC.squareWave(SQWAVE_NONE);
RTC.alarmInterrupt(ALARM_2, true);
attachInterrupt(digitalPinToInterrupt(AlarmPin), waketheboss, FALLING);
// attachInterrupt(digitalPinToInterrupt(LightOffPin), buttonpressed, FALLING);
setPwmFrequency(LightLevelPin, 1);
}
void loop() {
if (WakeBoss == true) {
digitalWrite(IndicatorLED, HIGH);
wakeshining();
}
}
void waketheboss() {
WakeBoss = true;
}
void wakeshining() { // I know that this ISR takes half an hour LOL, should be no longer than microseconds.
while (LightLevel < 255) { // 1020 increments up to 255, 1,764 secondsX1020 = 30 mins.
for (float f = 0; f < 83000; f++) {} // About 1 sec of delay per line
for (float f = 0; f < 63470; f++) {} // About 0,764 sec of delay per line
analogWrite(LightLevelPin, LightLevel);
LightLevel = pow(Base, 4) + 1 ; //y=x^4...
// Serial.println(LightLevel);
Base = Base + 0.0039215;
}
WakeBoss = false;
digitalWrite(IndicatorLED, LOW);
}
void buttonpressed() {
digitalWrite(LightLevelPin, LOW);
digitalWrite(IndicatorLED, LOW);
RTC.setAlarm(ALM2_MATCH_DATE, 0, 0, 0, 1);
RTC.alarm(ALARM_1);
RTC.alarm(ALARM_2);
RTC.alarmInterrupt(ALARM_1, false);
RTC.alarmInterrupt(ALARM_2, false);
RTC.setAlarm(ALM2_MATCH_HOURS, 0, AlarmM, AlarmH, 0);
digitalWrite(IndicatorLED, LOW);
}
void setPwmFrequency(int pin, int divisor) {
byte mode;
if (pin == 5 || pin == 6 || pin == 9 || pin == 10) {
switch (divisor) {
case 1: mode = 0x01; break;
case 8: mode = 0x02; break;
case 64: mode = 0x03; break;
case 256: mode = 0x04; break;
case 1024: mode = 0x05; break;
default: return;
}
if (pin == 5 || pin == 6) {
TCCR0B = TCCR0B & 0b11111000 | mode;
} else {
TCCR1B = TCCR1B & 0b11111000 | mode;
}
} else if (pin == 3 || pin == 11) {
switch (divisor) {
case 1: mode = 0x01; break;
case 8: mode = 0x02; break;
case 32: mode = 0x03; break;
case 64: mode = 0x04; break;
case 128: mode = 0x05; break;
case 256: mode = 0x06; break;
case 1024: mode = 0x07; break;
default: return;
}
TCCR2B = TCCR2B & 0b11111000 | mode;
}
}
The problem with the two interrupts declared is that none of them works.