How to access a class from another class with Arduino

0

I am trying to separate the display code i2c from another file as there are many lines in main.cpp

I'm using PlatformIO , the class is inside a folder that you create called Class

This is my main

#include <Arduino.h>
#include "Class/Display.h"

Display lcd;
void setup() {
    lcd.bienvenida();
}

void loop() {
  // 
}

And this is my Display class

#ifndef Display_h
#define Display_h


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

class Display;
class Display {

    private:
        LiquidCrystal_I2C lcd = LiquidCrystal_I2C(0x27,16,2);
    public:
        Display();
        void bienvenida();
};

Display::Display() {
    lcd.init();  
    lcd.backlight();
};

void Display::bienvenida() {
    lcd.clear();
    lcd.setCursor(4,0);
    lcd.print("HOLA");
    lcd.setCursor(3,1);
    lcd.print("MUNDO");
};
#endif

But it does not work, if I put all the code of the class in the main, then if it works fine but not from class.

I know very little about c ++ and I'm starting to see Arduino

Any solution?

    
asked by Chiiviito 31.10.2018 в 05:28
source

1 answer

0

Solve the problem with the code, create a pointer to the type of the class and then in the setup instantiate the class. I think those would be the technical words

#include <Arduino.h>
#include <Class/Display.h>

Display * lcd;
void setup() {
    lcd = new Display();
    lcd->bienvenida();
}

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

Source: link

    
answered by 31.10.2018 / 17:34
source