Making I2C LCD Work with C++ style code

I have a project where I need to make a coffee machine using C++ style code in Arduino UNO (meaning using only main and functions). What I want to know is how can I Initialize the LCD.

#include <LiquidCrystal_I2C.h>

#include <avr/io.h>

#include <avr/interrupt.h>

#include <MsTimer2.h>

LiquidCrystal_I2C lcd(0x27, 16, 2);

// Globals

uint8_t flag = 1;

static unsigned long last_interrupt_time = 0;

/*

class LCD{

  public:

  LiquidCrystal_I2C lcd = LiquidCrystal_I2C(0x27, 16, 2);

    LCD () {

      lcd.init();

      lcd.backlight();

    }

};

*/

class coff{

public:

  void sui(){

    switch (flag){

    case 1:

   

    flag++;

    break;

   }

  }

};

ISR(INTO_vect){

 

  if (millis() - last_interrupt_time > 200)

  {

  flag++;

}

last_interrupt_time = millis() ;

}

int main (void){

  lcd.init();

  lcd.clear();

  lcd.backlight();

  coff m;

  // Rising edge of INTO generates interrupt

  EICRA |= (1 << ISC01);

  EICRA &= ~(1 << ISC00);

  // Enable interrupts for INT0

  EIMSK |= (1 <<INT0);

  // Enable global interrupts

  sei();

 

  while(true){

  m.sui();

  }

}

I have an interrupt that you can ignore. I have tried to start the LCD in main, I also tried some class stuff which I commented out for now. But I really yeah don't know, and there is not much information that I could find on using the LCD in the way I want it. Let alone using C++ style code in Arduino

lcd lib in wokwi project added?

yeah

Welcome to the forum.

Hold on, what is wrong with the assignment ?
Arduino uses C++ code. When you just use main, then the Arduino software might not work well. Every Arduino function and every library depends on a working Arduino layer.
Does millis() work ? Does the I2C bus work ? I don't know. I have never seen something like this.

What you can do, is use the Uno board with the ATmega328P microcontroller and make a program with normal code. But you can not use any Arduino function and not any library that uses Arduino.
The website avrfreaks.net has nice projects and information how to use a ATmega328P (without Arduino).

You are either 100% in or 100% out the Arduino environment.

Arduino is C++.

You don't see main(), as it is hidden from you. loop() and setup() are ordinary functions called by main().

what does this mean?

"function" is a wayyy to generalised term to define anything.
Each and every Arduino-library uses "functions". A part of the functions defined inside "classes" a part of them without "classes" but all are "functions"

So you have to do the same thing as with any software-project:

writing down very very precise information. Even the most advanced "fuzzy-logic" algorithm is based on very precise basic lines of code.

best regards Stefan

I figured it out
I just had to write
LiquidCrystal_I2C lcd(0x27,20,4);
and
init();
in the main

as a "thank you" post the working code so the community can participate

#include <LiquidCrystal_I2C.h>
#include <avr/io.h>
#include <avr/interrupt.h>



LiquidCrystal_I2C lcd(0x27,20,4);


int main (void){
  init();
  lcd.init();
  lcd.clear();
  lcd.backlight();
  lcd.print("hi")
  
  while(true){

  }
}

This "may" be a trick assignment. It surely can be done with caveats:
https://www.youtube.com/watch?v=-XPrSScamXc

The display class/function will provide an init{} function/member.

Select your display. Find a library. Pull necessary functionality out into direct functions if you are not permitted to use a class lib.

Example, Nokia LCD


void LcdInitialise(void)
{
  pinMode(PIN_SCE, OUTPUT);
  pinMode(PIN_RESET, OUTPUT);
  pinMode(PIN_DC, OUTPUT);
  pinMode(PIN_SDIN, OUTPUT);
  pinMode(PIN_SCLK, OUTPUT);
  digitalWrite(PIN_RESET, LOW);
  digitalWrite(PIN_RESET, HIGH);
  LcdWrite(LCD_C, 0x21 );  // LCD Extended Commands.
  LcdWrite(LCD_C, 0xB1 );  // Set LCD Vop (Contrast). 
  LcdWrite(LCD_C, 0x04 );  // Set Temp coefficent. //0x04
  LcdWrite(LCD_C, 0x14 );  // LCD bias mode 1:48. //0x13
  LcdWrite(LCD_C, 0x0C );  // LCD in normal mode.
  LcdWrite(LCD_C, 0x20 );
  LcdWrite(LCD_C, 0x0C );
}

1 Like

Sounds like it. For me, it would imply no use of cores and no use of libraries; so main() and after that you have to re-invent the wheel.

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