Problem with Interrupt

Hello dear community. I am beginner with Arduino and I am trying to make simple interrupt where in loop you got IF, IF int 1 then turn on code else do nothing but when I connect my button and write an interrupt with int++ then nothing. The int is not getting new value.

  1. Part of my code
#include <Wire.h>
#include <LiquidCrystal_I2C.h>

LiquidCrystal_I2C lcd(0x27,16,2);  // nastavenie portov pre LCD

#define echoPin 11
#define trigPin 12
int LED = 13;
long duration, distance;
const int interruptPin = 2;
volatile int cislo = 0;

Setup

void setup()
{
  pinMode(interruptPin,INPUT_PULLUP);  // nastaveníe pinu tlačítka ako vstup
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  pinMode(LED, OUTPUT);
  lcd.init();
  lcd.backlight();
  attachInterrupt(digitalPinToInterrupt(interruptPin), prerusenie, CHANGE);
}

Loop

void loop(){

if ( cislo == 1 ){

  digitalWrite (trigPin,LOW);
  delayMicroseconds (2);

  digitalWrite (trigPin, HIGH);
  delayMicroseconds (10);
  digitalWrite (trigPin, LOW);

  duration = pulseIn (echoPin, HIGH);
  distance = duration/58   ;
  
  lcd.setCursor(0,0); 
  lcd.print("VZDIALENOST: "); 
  lcd.print(distance); 
  lcd.print("  cm  ");

  if (distance < 5 ){
  digitalWrite(LED, HIGH);
}

else if (distance > 5) {
  digitalWrite(LED, LOW);
}
}
else {
  lcd.print(cislo);
  lcd.setCursor(0,0); 
}
}

Interrupt

void prerusenie(){
  cislo++;
}

Interrupts are an advanced topic. I suggest you re-write your code without interrupts.

MarvelSK: I am trying to make simple interrupt where in loop you got IF, IF int 1 then turn on code else do nothing

I wouldn't say "else do nothing". Your code prints the value of cislo to the LCD in the else clause.

MarvelSK: The int is not getting new value.

"The int" is a very vague statement. If you want to have success in programming and electronics you will need to train yourself to be very precise. You have multiple "int"s in your code. We give variables names so that they are easy to refer to. It's the same idea as giving people and towns names. You wouldn't say "I talked to person" you would say "I talked to Susan". Why wouldn't you use the name of the variable when talking about it?

Are you saying that the LCD always shows 0?

With the addition of:

.
.
.
int last_cislo;

void setup()
{
    Serial.begin(9600);
    
    pinMode(interruptPin,INPUT_PULLUP);  // nastaveníe pinu tlačítka ako vstup
    pinMode(trigPin, OUTPUT);
    pinMode(echoPin, INPUT);
    pinMode(LED, OUTPUT);
    lcd.init();
    lcd.backlight();
    attachInterrupt(digitalPinToInterrupt(interruptPin), prerusenie, CHANGE);
    last_cislo = 0;
    
}

void loop()
{
    if( cislo != last_cislo )
    {
        Serial.println( cislo );
        last_cislo = cislo;
        
    }//if
.
.
.

On my 2560, when I repeatedly connect pin 2 to GND the value of cislo increments rapidly on the serial monitor with each touch.

I'd say your interrupt code is okay. Are you sure about your interrupt source, power and connections?

pert: I wouldn't say "else do nothing". Your code prints the value of cislo to the LCD in the else clause. "The int" is a very vague statement. If you want to have success in programming and electronics you will need to train yourself to be very precise. You have multiple "int"s in your code. We give variables names so that they are easy to refer to. It's the same idea as giving people and towns names. You wouldn't say "I talked to person" you would say "I talked to Susan". Why wouldn't you use the name of the variable when talking about it?

Are you saying that the LCD always shows 0?

Yes LCD always shows 0