How can I fix this strange rollback?

I had programming to use LiquidCrystal_I2C, and integrated find I2C address and use LCD. So I use class, but it has something wrong. I made just one object, but the program loop back a specific part

Here is my code

//main.ino
#include "LCD.h"

LCD_CUSTOM LCD;

void setup() {
  
}

void loop() {
  // put your main code here, to run repeatedly:

}
//LCD.h
#ifndef LCDLibrary_H
#define LCDLibrary_H

#include "Arduino.h"
#include <Wire.h>
#include <LiquidCrystal_I2C.h>

class LCD_CUSTOM {
  public: 
    uint8_t address;
    LCD_CUSTOM() {
      Serial.begin(9600);
      address = 0;
      init();
      running();
    };
  private:
    void init();
    void running();

};

#endif
// LCD.cpp
#include "LCD.h"

void LCD_CUSTOM::init() {
  Wire.begin();

  Serial.println("\n--------------------I2C Scanner--------------------");
  Serial.println(LCD_CUSTOM::address);

  byte error = 0;
  int nDevices;

  Serial.println("active");    // loop back point. 
  for(LCD_CUSTOM::address = 1; LCD_CUSTOM::address < 127; LCD_CUSTOM::address++) 
  {
    Wire.beginTransmission(LCD_CUSTOM::address);
    error = Wire.endTransmission();

    if(error == 0)
    {
      Serial.print("I2C device found at :address 0x");
      if(LCD_CUSTOM::address < 16) 
        Serial.println(" !");
      Serial.print(LCD_CUSTOM::address, HEX);
      Serial.println(" !");
      break;
    }

    else if(error==4) {
      Serial.print("Unknow error at address 0x");
      if(LCD_CUSTOM::address<16)
        Serial.print("0");
      Serial.println(LCD_CUSTOM::address, HEX);
    }
  }

  if(LCD_CUSTOM::address == 127)
    Serial.println("No I2C devices found\n");
  else
    Serial.println("done\n");

  Serial.println("\n--------------------I2C Scanner End--------------------");
  Serial.println("Init complete. Running LCD");
}

void LCD_CUSTOM::running() {
  LiquidCrystal_I2C lcd(address, 16, 2);
  lcd.begin();
  lcd.print("Hello, World!");
}

Make the following changes:

void setup() {
  LCD.init();
}

void LCD_CUSTOM::running() {
  LiquidCrystal_I2C lcd(LCD_CUSTOM::address, 16, 2); // Use LCD_CUSTOM::address
  lcd.begin();
  lcd.print("Hello, World!");
}

// LCD.h
#ifndef LCDLibrary_H
#define LCDLibrary_H

#include "Arduino.h"
#include <Wire.h>
#include <LiquidCrystal_I2C.h>

class LCD_CUSTOM {
  public: 
    uint8_t address;
    LCD_CUSTOM();
    void init();
    void running();
};

#endif

// LCD.cpp
#include "LCD.h"

LCD_CUSTOM::LCD_CUSTOM() {
  Serial.begin(9600);
  address = 0;
}

void LCD_CUSTOM::init() {
  Wire.begin();

  Serial.println("\n--------------------I2C Scanner--------------------");
  Serial.println(address);

  byte error = 0;
  int nDevices;

  Serial.println("active");
  for(address = 1; address < 127; address++) {
    Wire.beginTransmission(address);
    error = Wire.endTransmission();

    if (error == 0) {
      Serial.print("I2C device found at address 0x");
      if (address < 16) 
        Serial.print("0");
      Serial.println(address, HEX);
      break;
    } else if (error == 4) {
      Serial.print("Unknown error at address 0x");
      if (address < 16)
        Serial.print("0");
      Serial.println(address, HEX);
    }
  }

  if (address == 127)
    Serial.println("No I2C devices found\n");
  else
    Serial.println("done\n");

  Serial.println("\n--------------------I2C Scanner End--------------------");
  Serial.println("Init complete. Running LCD");
}

void LCD_CUSTOM::running() {
  LiquidCrystal_I2C lcd(address, 16, 2);
  lcd.begin();
  lcd.print("Hello, World!");
}

#include "LCD.h"

LCD_CUSTOM LCD;

void setup() {
  LCD.init();
}

void loop() {
  LCD.running();
}

1 Like

It works! Thank you so much.

But why did this happen?

Your loop was void. And also you did not initialize the LCD in your setup function. These were the main issues.

1 Like

I see. Thank you for reply!

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.