Variable changed in interrupt won´t change in loop.

For some reason the if sentence in the end of the loop won´t work when I change the variable with the interrupt. The serial monitor shows that the variables "timer" and "minutt" really is changing, but somehow it does not affect the outcome of the if sentence. Any ideas why?

#include <SPI.h>
#include <Adafruit_GFX.h>
#include <Adafruit_PCD8544.h>
#include <Wire.h>
#include "RTClib.h"
RTC_Millis rtc;
Adafruit_PCD8544 display = Adafruit_PCD8544(8, 7, 6, 5, 4);

#define NUMFLAKES 10
#define XPOS 0
#define YPOS 1
#define DELTAY 2

volatile int minutt=3;
volatile int timer=0;
volatile int pinA=2;
volatile int pinB=3;
volatile int forsinkelse;

void setup() {
pinMode(pinA,INPUT);
pinMode(pinB,INPUT);
pinMode(11,OUTPUT);
Serial.begin(115200);
digitalWrite(pinA,HIGH);
digitalWrite(pinB,HIGH);
attachInterrupt(0,knobTurned,RISING);
Serial.begin(115200);

display.clearDisplay();

display.begin();
display.setContrast(39);
display.setTextSize(4);
display.setRotation(1);
display.setTextColor(BLACK);
display.println(minutt);

rtc.adjust(DateTime(DATE, TIME));
}

void loop() {
delay(forsinkelse);
display.clearDisplay();
DateTime now = rtc.now();
display.setTextSize(4);
display.setRotation(1);
display.setTextColor(BLACK);
display.setCursor(0,0);
display.println(now.hour(), DEC);
display.setCursor(0,42);
display.println(now.minute(), DEC);
display.display();
delay(forsinkelse);
forsinkelse=1000;
Serial.print(now.hour(), DEC);
Serial.print(" : ");
Serial.print(now.minute(), DEC);
Serial.print(" : ");
Serial.println(now.second(), DEC);
Serial.print(timer);
Serial.print(" : ");
Serial.println(minutt);

if(minutt<now.minute(),DEC){
analogWrite(11,0);
}else{analogWrite(11,200);}
}

void knobTurned(){
detachInterrupt(0);
forsinkelse=2500;
display.clearDisplay();
if(digitalRead(pinA)&&digitalRead(pinB)==1){
if(minutt==59){
minutt=0;
timer++;
}else
minutt++;
}
if(digitalRead(pinA)==1&&digitalRead(pinB)==0){
if(minutt==0){
minutt=59;
timer--;
}else
minutt--;
}

display.setTextSize(4);
display.setRotation(1);
display.setTextColor(BLACK);
display.setCursor(0,42);
display.println(minutt);
display.setCursor(0,0);
display.println(timer);

display.display();

attachInterrupt(0,knobTurned,RISING);
}

 if(minutt<now.minute(),DEC)

Can you explain what you think that is doing (hint: it isn't)

Please use CODE TAGS when posting code.

void knobTurned(){
  detachInterrupt(0);
 
  attachInterrupt(0,knobTurned,RISING);
}

Why are you detaching and reattaching the interrupt handler? While the interrupt is running, you can diddle with the knob all you want and it won't interrupt the interrupt handler.

AWOL:
Sorry about the code tags..

"minutt" is a variable I am changing with a rotary encoder, and "now.minute(), DEC" returns the current minute from an rtc in decimal. As you can see in the code, I am also sending "now.minute(),DEC" to the serial monitor. In the serial monitor, all the variables are changing as they should, but somehow the logic in the if sentence will not work.

PaulS:
To be honest, I don´t remember. I understood it when i watched this tutorial Rotary Encoder Tutorial with Arduino Code - YouTube (the same guy who made the 8x8x8 led cube) , but that is a long time ago :wink: Anyway, that is not the problem, and as I wrote to AWOL; everything is working fine, except that it actually seems like the variables don´t change within the if sentence.. I know this is not possible, but that is what it seems like..

No, DEC tells the print method that you want to see the result in decimal (which is the default anyway).
It has nothing at all to do with what format the function returns its value in.

(I did hint that it wasn't doing what you thought)

Ok, I thought you weren´t familiar with the rtc library :wink: So what then does minute.now() return? And what do I have to do to compare this value to a variable?

And what do I have to do to compare this value to a variable?

Use a comparison operator, like >, <, >=, <=, == or !=.
But don't use DEC or the comma that preceded it.

I tried with:

if(minutt>now.minute())

But it still wont work..

Nevermind.. The project is for a alarm clock with light, and one of the leads to from the power supply had come loose. It works now. Thanks AWOL!:slight_smile: