1
#include <Wire.h>
#include <LiquidCrystal.h>
int temp;
const int rs = 13, en = 12, d4 = 11, d5 = 10, d6 = 9, d7 = 8;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
char Nick[15] = {' ', ' ', 'T', 'A', 'R', 'I', 'F', ' ', 'Z', 'I', 'N', 'E', 'D', ' ', ' '};
int butonPin = 2;

void setup() {
  Wire.begin(1);
  Serial.begin(9600);
  pinMode(butonPin,INPUT_PULLUP);
  attachInterrupt(digitalPinToInterrupt(butonPin), Rev, RISING);
  lcd.begin(16, 2);  
}

void loop() {
  Wire.onReceive(data);
  lcd.setCursor(2, 1);
  lcd.print(temp);

  if (temp < 25) {
    lcd.setCursor(5, 1);
    lcd.print("NORMAL");
    lcd.print(" ");
    lcd.setCursor(12, 1);
    lcd.print(" ");
  }
  if (temp >= 25 && temp <= 30) {
    lcd.setCursor(5, 1);
    lcd.print("HOT");
    lcd.print(" ");
    lcd.setCursor(11, 1);
    for (int i = 0 ; i < 3 ; i++) {
      lcd.print(" ");
    }
  }
  if (temp > 30) {
    lcd.setCursor(5, 1);
    lcd.print("VERY HOT");
    lcd.print(" ");
  }
  lcd.setCursor(0, 0);
  for (int j = 15; j > 0 ; j--) {
    lcd.print(Nick[j]);
  }
  lcd.setCursor(0, 0);
  delay(70);
  int swap = Nick[15];
  for (int i = 15; i > 0; i--) {
    Nick[i] = Nick[i - 1];
  }
  Nick[0] = swap;
}
void Rev(){
   Wire.onReceive(data);
  lcd.setCursor(2, 0);
  lcd.print(temp);

  if (temp < 25) {
    lcd.setCursor(5, 0);
    lcd.print("NORMAL");
    lcd.print(" ");
    lcd.setCursor(12, 0);
    lcd.print(" ");
  }
  if (temp >= 25 && temp <= 30) {
    lcd.setCursor(5, 0);
    lcd.print("HOT");
    lcd.print(" ");
    lcd.setCursor(11, 0);
    for (int i = 0 ; i < 3 ; i++) {
      lcd.print(" ");
    }
  }
  if (temp > 30) {
    lcd.setCursor(5, 0);
    lcd.print("VERY HOT");
    lcd.print(" ");
  }
  lcd.setCursor(0, 1);
  for (int j = 15; j > 0 ; j--) {
    lcd.print(Nick[j]);
  }
  lcd.setCursor(0, 1);
  delay(70);
  int swap = Nick[15];
  for (int i = 15; i > 0; i--) {
    Nick[i] = Nick[i - 1];
  }
  Nick[0] = swap;
}


void data(int numBytes) {
  while (Wire.available()) {
    temp = Wire.read();
  }
}

In lcd's first line i have scrolling text. Second line it shows current temperature and data from another master device. My question is i want to change text line when button pressed and text shouldn't go back to old state it need to be stay in changed line and when the button repressed it change again
In my code when the button pressed several times lcd gone weird : desc

I tried swap lines with lcd.setCursor() function : In loop function :

lcd.setCursor(0, 0);
      for (int j = 15; j > 0 ; j--) {
        lcd.print(Nick[j]);
      }

In reverse function :

lcd.setCursor(0, 1);
  for (int j = 15; j > 0 ; j--) {
    lcd.print(Nick[j]);
  }

Thanks for answers guys

4
  • your ISR (interrupt service routine) is too long and complex ... a press of a button should call an ISR that only sets a flag and exits .... the flag should be checked and serviced during the execution of loop() – jsotola Jun 1 '20 at 21:37
  • Catching humans pressing buttons is a poor use of an interrupt. You could do this a lot easier if you just poll the button. Your interrupt function contains way too much code and uses code like delay that just plain won't work in an interrupt context. If you don't KNOW what you're doing with interrupts then it's probably best to leave them alone. You really need a lot of study before you use them. There's obviously a lot you don't know. – Delta_G Jun 1 '20 at 21:37
  • For one, inside an ISR all other interrupts are off. So things like delay and serial comms and maybe even stuff to do with your LCD simply won't work at best or lock up the program at worst. – Delta_G Jun 1 '20 at 21:37
  • I readed all answers and %100 you guys right if i have chance i leave alone this interrupt madness but this project is my final assignment in college and my instructor was made a working one. Actually i am not expert in arduino like you guys said. – Dennis Jun 2 '20 at 0:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.