arduino nano timer interrupt

Hi all,

I want to try fill my skills about programming and I crash on „Interrupts“.
I have some kind of code, I was tried to use library TimerOne.h and I was failed every times.
Please, can somebody explain me how to use this library or some kind of interrupt in this kind of code?

Many thanks.

#include <DS3231.h>
int Relay = 4;
int wait = 150;
int Pin1 = 9;
DS3231  rtc(SDA, SCL);
Time t;
const int OnHour = 12;
const int OnMin = 24;
const int OffHour = 12;
const int OffMin = 25;
void setup() {
  Serial.begin(9600);
  rtc.begin();
  pinMode(Relay, OUTPUT);
  digitalWrite(Relay, LOW);
}
void loop() {
  t = rtc.getTime();
  Serial.print(t.hour);
  Serial.print(" hour(s), ");
  Serial.print(t.min);
  Serial.print(" minute(s)");
  Serial.println(" ");
  delay (1000);
  if(t.hour == OnHour && t.min == OnMin){
    digitalWrite(Relay,HIGH);
    Serial.println("ON");
    rise();
    }
    
    else if(t.hour == OffHour && t.min == OffMin){
      digitalWrite(Relay,LOW);
      Serial.println("OFF");
    }
}
void rise() {

for (i=0; i<=255; i++) {
            analogWrite(Pin1, i);
            delay(wait);
}
}

If you want to use the TimeOne library why do I see all kind of stuff like DS3231?

And "I was failed" isn't really explaining what happens...

I see no reference to TimerOne or any use of interrupts. Where you are 'crashing' is not clear.

this code what I write above, is only shortcut of my code. I reflect time on display (nokia 5110),

if

const int OnHour = 12;
const int OnMin = 24;

than void rise() is running, but time on display is stopped.

Now I am in work, if I will be at home I will upload whole code.

aughostino:
is only shortcut of my code.

That's exacly what I asked you to do not :wink: See Snippets R Us

okay, sorry for this I just acquaint with this.

here is whole code and fail is, that time on display is stopped during void rise(). How to correct use intettupts?

#include <TimerOne.h>
#include <Nokia5110.h>
#include <DS3231.h>


#define RST 8
#define CE 7
#define DC 6
#define DIN 5
#define CLK 4

#define BETWEEN 2579
#define DURATION 43
#define TIMES 7
#define REDPIN 9
#define GREENPIN 10
#define BLUEPIN 11
#define FADESPEED 5

LCDnokia5110 lcd(RST, CE, DC, DIN, CLK);

int Relay = 2;
int Relay2 = 3;
int temp;
unsigned long lastTime = 0;
int waitTime = 0;
int r, g, b;
int wait = 150;

DS3231  rtc(SDA, SCL);
Time t;

const int OnHour = 17;
const int OnMin = 48;
const int OffHour = 17;
const int OffMin = 49;

const int Relay2OnHour = 17;
const int Relay2OnMin = 50;
const int Relay2OffHour = 17;
const int Relay2OffMin = 51;

void setup() {
  Serial.begin(9600);
  rtc.begin();
  pinMode(Relay, OUTPUT);
  pinMode(Relay2, OUTPUT);
  pinMode(REDPIN, OUTPUT);
  pinMode(GREENPIN, OUTPUT);
  pinMode(BLUEPIN, OUTPUT);
  digitalWrite(Relay, HIGH);
  digitalWrite(Relay2, HIGH);
  Timer1.initialize(1000000);
  Timer1.attachInterrupt(rise);
  Timer1.attachInterrupt(fall);
  
  lcd.LcdInitialise();
  lcd.LcdClear();
  lcd.CharSpace = 1; // nastavení velikosti mezery mezi písmeny,lze nastavit na 0, 1 či 2 body
  lcd.GotoXY(70, 0);
  lcd.LcdString("'C");
  lcd.GotoXY(0, 1);
  lcd.LcdString("Relay 1:");
  lcd.GotoXY(55, 1);
  lcd.LcdString("OFF");
  lcd.GotoXY(0, 2);
  lcd.LcdString("Relay 2:");
  lcd.GotoXY(55, 2);
  lcd.LcdString("OFF");
  lcd.CharSpace = 0;
  lcd.GotoXY(0, 3);
  lcd.LcdString("Teplota:");
  lcd.GotoXY(55, 3);
  lcd.CharSpace = 1;
  lcd.LcdString("60'C");
  lcd.GotoXY(0, 44);
  lcd.LcdString("PH:");
}

void loop() {
  temp = rtc.getTemp();
  t = rtc.getTime();

  char buffer[5];


  lcd.GotoXY(0, 0);
  sprintf(buffer, "%2d", t.hour);
  lcd.LcdString(buffer);
  lcd.GotoXY(12, 0);
  lcd.LcdString(":");
  //==========MINUTY
  lcd.GotoXY(17, 0);
  sprintf(buffer, "%2d", t.min);
  lcd.LcdString(buffer);
  lcd.GotoXY(29, 0);
  lcd.LcdString(":");
  //==========SEKUNDY
  lcd.GotoXY(34, 0);
  sprintf(buffer, "%2d", t.sec);
  lcd.LcdString(buffer);

  //=========TEPLOTA elektroniky
  lcd.GotoXY(57, 0);
  sprintf(buffer, "%2d", temp);
  lcd.LcdString(buffer);

  //=========TEPLOTA elektroniky
  lcd.GotoXY(57, 0);
  sprintf(buffer, "%2d", temp);
  lcd.LcdString(buffer);

  delay(1000);


  //===========================Rele LED pásik
  if (t.hour == OnHour && t.min == OnMin) {
    digitalWrite(Relay, LOW);
    lcd.GotoXY(55, 1);
    lcd.LcdString("ON ");
    rise();
  }

  else if (t.hour == OffHour && t.min == OffMin) {
    digitalWrite(Relay, HIGH);
    lcd.GotoXY(55, 1);
    lcd.LcdString("OFF");
  }


  //========================Rele Svetlo
  if (t.hour == Relay2OnHour && t.min == Relay2OnMin) {
    digitalWrite(Relay2, LOW);
    lcd.GotoXY(55, 2);
    lcd.LcdString("ON ");
    fall();
  }

  else if (t.hour == Relay2OffHour && t.min == Relay2OffMin) {
    digitalWrite(Relay2, HIGH);
    lcd.GotoXY(55, 2);
    lcd.LcdString("OFF");
  }
}



void rise() {
  g = 0;
  b = 0;
  for (r = 0; r <= 255; r++)
  {
    g = r;
    if (g >= 50) {
      b = b + 1;  
    }
    analogWrite(REDPIN, r);
    analogWrite(GREENPIN, g);
    analogWrite(BLUEPIN, b);
    Serial.print("R=");
    Serial.print(r);
    Serial.print("G=");
    Serial.print(g);
    Serial.print("B=");
    Serial.print(b);
    Serial.print(" | ");
    delay(wait);
    if(r==253) {
      b=253;
    }
    if(r==255) {
      exit(r);
    }
  }
}

void fall() {
  g = 255;
  b = 255;
  for (r = 255; r >= 0; r--)
  {
    g = r;
    if (g <= 206) {
      b = b - 1;  
    }
    analogWrite(REDPIN, r);
    analogWrite(GREENPIN, g);
    analogWrite(BLUEPIN, b);
    Serial.print("R=");
    Serial.print(r);
    Serial.print("G=");
    Serial.print(g);
    Serial.print("B=");
    Serial.print(b);
    Serial.print(" | ");
    delay(wait);
    if(r==2) {
      b=2;
    }
    if(r==0) {
      exit(r);
    }
  }
}


void burka() {
  if (millis() - waitTime > lastTime)  // time for a new flash
  {
    // adjust timing params
    lastTime += waitTime;
    waitTime = random(BETWEEN);
    analogWrite(BLUEPIN, 220);
    for (int i = 0; i < random(TIMES); i++)
    {
      Serial.println(millis());
      analogWrite(REDPIN, 255);
      analogWrite(GREENPIN, 255);
      analogWrite(BLUEPIN, 255),
                  delay(20 + random(DURATION));
      analogWrite(REDPIN, 0);
      analogWrite(GREENPIN, 0);
      analogWrite(BLUEPIN, 0);
      delay(10);
    }
  }
  analogWrite(BLUEPIN, 5);
  if (millis() - waitTime > lastTime)  // time for a new flash
  {
    // adjust timing params
    lastTime += waitTime;
    waitTime = random(BETWEEN);

    for (int i = 0; i < random(TIMES); i++)
    {
      analogWrite(BLUEPIN, 255);
      delay(10 + random(DURATION));
      analogWrite(BLUEPIN, 0);
      delay(10);
    }
  }
}

I can't try to compile as I don't have your special libraries so I can't tell what errors are being reported or where. Your code looks okay but the jury is definitely still out on the use of the exit() function which you have in both rise() and fall(). If I'm not wrong, everything stops.

23.10.4.11 void exit ( int __status )
The exit() function terminates the application. Since there is no environment to return to, status is ignored, and
code execution will eventually reach an infinite loop, thereby effectively halting all code processing. Before entering the infinite loop, interrupts are globally disabled.

That's straight out of the AVR library manual.