Error: 'tft' was not declared in this scope

Hello,

I'm having trouble trying to order my code from spaghetti to nice .cpp and .h files as i learn.

I thought that puting :


#include <TFT_eSPI.h>

TFT_eSPI tft = TFT_eSPI();

At the start before the setup made so tft was global and accessible from anywhere.

I have a Functions.cpp file with :

#include <TFT_eSPI.h>
#include "Functions.h"

void ActionMessage::UpdateMessage(int BoxNum){
  tft.fillRect(100, 75, 100, 20, TFT_CYAN);
  tft.setCursor(100,75);
  tft.print(BoxNum);
};

I got this error : \Functions.cpp:8:3: error: 'tft' was not declared in this scope

I don't know what to do.

It looks like you use the global variable tft in Functions.cpp, but this variable is defined in the main sketch.

If this is the case, try adding the following line to Functions.cpp:

extern TFT_eSPI tft;

Getting rid of the global variable would probably be best.

Thx for the answer, i tried putting

extern TFT_eSPI tft;

in Functions.h but that gave me a kind a message i hadn't seen before.

C:\Users\antoi\AppData\Local\Temp\arduino-sketch-019FABCAC6A30242536B26CB9506D08B\sketch\Sprite_loading_3.ino.cpp.o:(.bss.button_1+0x0): multiple definition of `button_1'
C:\Users\antoi\AppData\Local\Temp\arduino-sketch-019FABCAC6A30242536B26CB9506D08B\sketch\Functions.cpp.o:(.bss.button_1+0x0): first defined here
C:\Users\antoi\AppData\Local\Temp\arduino-sketch-019FABCAC6A30242536B26CB9506D08B\sketch\Sprite_loading_3.ino.cpp.o:(.bss.button_2+0x0): multiple definition of `button_2'
C:\Users\antoi\AppData\Local\Temp\arduino-sketch-019FABCAC6A30242536B26CB9506D08B\sketch\Functions.cpp.o:(.bss.button_2+0x0): first defined here
C:\Users\antoi\AppData\Local\Temp\arduino-sketch-019FABCAC6A30242536B26CB9506D08B\sketch\Sprite_loading_3.ino.cpp.o:(.bss.button_3+0x0): multiple definition of `button_3'
C:\Users\antoi\AppData\Local\Temp\arduino-sketch-019FABCAC6A30242536B26CB9506D08B\sketch\Functions.cpp.o:(.bss.button_3+0x0): first defined here
C:\Users\antoi\AppData\Local\Temp\arduino-sketch-019FABCAC6A30242536B26CB9506D08B\sketch\Sprite_loading_3.ino.cpp.o:(.bss.button_4+0x0): multiple definition of `button_4'
C:\Users\antoi\AppData\Local\Temp\arduino-sketch-019FABCAC6A30242536B26CB9506D08B\sketch\Functions.cpp.o:(.bss.button_4+0x0): first defined here
C:\Users\antoi\AppData\Local\Temp\arduino-sketch-019FABCAC6A30242536B26CB9506D08B\sketch\Sprite_loading_3.ino.cpp.o: In function `handler(Button2&)':
Sprite_loading_3.ino.cpp:(.text._Z7handlerR7Button2+0x0): multiple definition of `handler(Button2&)'
C:\Users\antoi\AppData\Local\Temp\arduino-sketch-019FABCAC6A30242536B26CB9506D08B\sketch\Functions.cpp.o:Functions.cpp:(.text._Z7handlerR7Button2+0x0): first defined here
C:\Users\antoi\AppData\Local\Temp\arduino-sketch-019FABCAC6A30242536B26CB9506D08B\sketch\Sprite_loading_3.ino.cpp.o:(.bss.Box1+0x0): multiple definition of `Box1'
C:\Users\antoi\AppData\Local\Temp\arduino-sketch-019FABCAC6A30242536B26CB9506D08B\sketch\Functions.cpp.o:(.bss.Box1+0x0): first defined here
C:\Users\antoi\AppData\Local\Temp\arduino-sketch-019FABCAC6A30242536B26CB9506D08B\sketch\Sprite_loading_3.ino.cpp.o:(.bss.Act+0x0): multiple definition of `Act'
C:\Users\antoi\AppData\Local\Temp\arduino-sketch-019FABCAC6A30242536B26CB9506D08B\sketch\Functions.cpp.o:(.bss.Act+0x0): first defined here
C:\Users\antoi\AppData\Local\Temp\arduino-sketch-019FABCAC6A30242536B26CB9506D08B\sketch\Sprite_loading_3.ino.cpp.o:(.bss.Box2+0x0): multiple definition of `Box2'
C:\Users\antoi\AppData\Local\Temp\arduino-sketch-019FABCAC6A30242536B26CB9506D08B\sketch\Functions.cpp.o:(.bss.Box2+0x0): first defined here
C:\Users\antoi\AppData\Local\Temp\arduino-sketch-019FABCAC6A30242536B26CB9506D08B\sketch\Sprite_loading_3.ino.cpp.o:(.bss.Box3+0x0): multiple definition of `Box3'
C:\Users\antoi\AppData\Local\Temp\arduino-sketch-019FABCAC6A30242536B26CB9506D08B\sketch\Functions.cpp.o:(.bss.Box3+0x0): first defined here
C:\Users\antoi\AppData\Local\Temp\arduino-sketch-019FABCAC6A30242536B26CB9506D08B\sketch\Sprite_loading_3.ino.cpp.o:(.bss.Box4+0x0): multiple definition of `Box4'
C:\Users\antoi\AppData\Local\Temp\arduino-sketch-019FABCAC6A30242536B26CB9506D08B\sketch\Functions.cpp.o:(.bss.Box4+0x0): first defined here
collect2.exe: error: ld returned 1 exit status

It looks like the compilation step is working, but you now have an error in the linking step. Can it be that you include Functions.h multiple times without using an include guard?

If so, you can try adding the following line to the start of Functions.h:

#pragma once

Alternatively, you can use the old style include guard:

#ifndef FUNCTIONS_H_
#define FUNCTIONS_H_

// Your code here.

#endif

Hello 2ant, Good day.
what I understood:

  1. You have a header file called Functions.h and in this header file the "tft" variable is defind
  2. You have a C code file called Functions.cpp and inside this you include the Functions.h header file.
    Is my understanding so ok?
    Arduino IDE has also a header called functions.h. Yours is Functions.h. Can you have a singular name to the header say myfunctions.h. And it seems that a multiple including to your other sources is happening with leading to:
multiple definition of `button_1'

I now have some error linked to a class i tried to make.

I will post eveything i got since i can't untangle my files.

In file included from C:\Users\antoi\Documents\Arduino\Sprite_loading_3\myFunctions.cpp:3:0:
C:\Users\antoi\Documents\Arduino\Sprite_loading_3\myFunctions.h:45:1: error: 'Box1' does not name a type
 Box1.BoxNum = 1;
 ^
C:\Users\antoi\Documents\Arduino\Sprite_loading_3\myFunctions.h:46:1: error: 'Box2' does not name a type
 Box2.BoxNum = 2;
 ^
C:\Users\antoi\Documents\Arduino\Sprite_loading_3\myFunctions.h:47:1: error: 'Box3' does not name a type
 Box3.BoxNum = 3;
 ^
C:\Users\antoi\Documents\Arduino\Sprite_loading_3\myFunctions.h:48:1: error: 'Box4' does not name a type
 Box4.BoxNum = 4;
 ^
In file included from C:\Users\antoi\Documents\Arduino\Sprite_loading_3\Sprite_loading_3.ino:8:0:
C:\Users\antoi\Documents\Arduino\Sprite_loading_3\myFunctions.h:45:1: error: 'Box1' does not name a type
 Box1.BoxNum = 1;
 ^
C:\Users\antoi\Documents\Arduino\Sprite_loading_3\myFunctions.h:46:1: error: 'Box2' does not name a type
 Box2.BoxNum = 2;
 ^
C:\Users\antoi\Documents\Arduino\Sprite_loading_3\myFunctions.h:47:1: error: 'Box3' does not name a type
 Box3.BoxNum = 3;
 ^
C:\Users\antoi\Documents\Arduino\Sprite_loading_3\myFunctions.h:48:1: error: 'Box4' does not name a type
 Box4.BoxNum = 4;
 ^

Compilation error: exit status 1

.ino :

#include <stdlib.h>
#include <TFT_eSPI.h>

//extern TFT_eSPI tft = TFT_eSPI(320,240);

#include "images.h"

#include "myFunctions.h"


TFT_eSprite spr2 = TFT_eSprite(&tft);
TFT_eSprite spr3 = TFT_eSprite(&tft);

int center = ( 5 + 16 );

uint16_t tab_color[] = {
  TFT_BLACK,    //  0  ^
  TFT_BROWN,    //  1  |
  TFT_RED,      //  2  |
  TFT_ORANGE,   //  3  |
  TFT_YELLOW,   //  4  Colours 0-9 follow the resistor colour code!
  TFT_GREEN,    //  5  |
  TFT_BLUE,     //  6  |
  TFT_PURPLE,   //  7  |
  TFT_DARKGREY, //  8  |
  TFT_WHITE,    //  9  v
  TFT_CYAN,     // 10  Blue+green mix
  TFT_MAGENTA,  // 11  Blue+red mix
  TFT_MAROON,   // 12  Darker red colour
  TFT_DARKGREEN,// 13  Darker green colour
  TFT_NAVY,     // 14  Darker blue colour
  TFT_PINK      // 15
};



void setup() {

  Serial.begin(115200);

  b_init();

  tft.init();
  tft.setRotation(1);
  tft.fillScreen(TFT_CYAN);

  spr3.setColorDepth(16);
  spr3.createSprite(32,32);
  spr3.setSwapBytes(true);

  tft.drawRoundRect(4, 4, 66, 66, 11, 0);
  tft.drawRoundRect(5, 5, 64, 64, 10, 0);

  tft.drawRoundRect(4, 99, 34, 34, 6, 0);
  tft.drawRoundRect(5, 100, 32, 32, 5, 0);

  tft.drawRoundRect(44, 99, 34, 34, 6, 0);
  tft.drawRoundRect(45, 100, 32, 32, 5, 0);

  tft.drawRoundRect(84, 99, 34, 34, 6, 0);
  tft.drawRoundRect(85, 100, 32, 32, 5, 0);

  tft.drawRoundRect(124, 99, 34, 34, 6, 0);
  tft.drawRoundRect(125, 100, 32, 32, 5, 0);
/*
  tft.drawRoundRect(6, 101, 30, 30, 4, TFT_RED);
  tft.drawRoundRect(6, 100, 30, 30, 4, TFT_RED);
  tft.drawRoundRect(7, 102, 28, 28, 3, TFT_PINK);
*/
  tft.setTextColor(TFT_RED);
  tft.setTextSize(2); 

  tft.setCursor(75,6);
  tft.print("Name: ""Egg");
  tft.setCursor(75,30);
  tft.print("Evolution: ""Egg");
  tft.setCursor(75,54);
  tft.print("Status: ""Uncoocked");

  tft.setCursor(5,75);
  tft.print("Action :");

  
  //Act.UpdateMessage (Box1.BoxNum);

  for (int i = 0; i < 8; i++) 
  {
    tft.fillRect((0+(i*32)),176,32,32,tab_color[i]);
  }
  for (int i = 0; i < 8; i++) 
  {
    tft.fillRect((0+((i)*32)),208,32,32,tab_color[i+7]);
  }

}
void loop(void) {

  button_1.loop();
  button_2.loop();
  button_3.loop();
  button_4.loop();
/*
  spr3.fillSprite(TFT_CYAN);
  spr3.pushSprite(center,32, TFT_MAGENTA);
  spr3.pushImage(0, 0, 128, 32, (uint16_t *)egg_1);
  spr3.pushSprite(center,32, TFT_MAGENTA);
  delay(500);

  spr3.fillSprite(TFT_CYAN);
  spr3.pushSprite(center,32, TFT_MAGENTA);
  spr3.pushImage(-32, 0, 128, 32, (uint16_t *)egg_1);
  spr3.pushSprite(center,32, TFT_MAGENTA);
  delay(500);

  spr3.fillSprite(TFT_CYAN);
  spr3.pushSprite(center,32, TFT_MAGENTA);
  spr3.pushImage(-64, 0, 128, 32, (uint16_t *)egg_1);
  spr3.pushSprite(center,32, TFT_MAGENTA);
  delay(500);

  spr3.fillSprite(TFT_CYAN);
  spr3.pushSprite(center,32, TFT_MAGENTA);
  spr3.pushImage(-96, 0, 128, 32, (uint16_t *)egg_1);
  spr3.pushSprite(center,32, TFT_MAGENTA);
  delay(500);
/*
  spr3.fillSprite(TFT_CYAN);
  spr3.pushSprite(center,32, TFT_MAGENTA);
*/
  

 }

images.h (not important )


#include <TFT_eSPI.h>                 

extern const uint16_t stars[98304] PROGMEM ;
extern const uint16_t stars2[4096] PROGMEM ;
extern const uint16_t stars3[4096] PROGMEM ;
extern const uint16_t egg_1[4096] PROGMEM ;

images.cpp is ust a few images

Button2.h (from a lib )

/////////////////////////////////////////////////////////////////
/*
  Button2.h - Arduino Library to simplify working with buttons.
  Created by Lennart Hennigs.
*/
/////////////////////////////////////////////////////////////////
#pragma once

#ifndef Button2_h
#define Button2_h

/////////////////////////////////////////////////////////////////
#if defined(ARDUINO_ARCH_ESP32) || defined(ESP8266)
  #include <functional>
#endif
#include "Arduino.h"

/////////////////////////////////////////////////////////////////

#define DEBOUNCE_MS 50
#define LONGCLICK_MS 200
#define DOUBLECLICK_MS 300
#define CAPACITIVE_TOUCH_THRESHOLD 35

#define SINGLE_CLICK 1
#define DOUBLE_CLICK 2
#define TRIPLE_CLICK 3
#define LONG_CLICK 4

#define UNDEFINED_PIN 255

/////////////////////////////////////////////////////////////////

class Button2 {
protected:
  byte pin;
  byte state;
  byte prev_state;
  byte click_count = 0;
  byte last_click_type = 0;
  byte _pressedState;
  bool is_capacitive = false;
  unsigned long click_ms;
  unsigned long down_ms;

  bool longclick_detected_retriggerable;
  uint16_t longclick_detected_counter;
  bool longclick_detected = false;
  bool longclick_detected_reported = false;
  
  unsigned int debounce_time_ms;
  unsigned int longclick_time_ms;
  unsigned int doubleclick_time_ms;
  
  unsigned int down_time_ms = 0;
  bool pressed_triggered = false;

#if defined(ARDUINO_ARCH_ESP32) || defined(ESP8266)
  typedef std::function<void(Button2 &btn)> CallbackFunction;
#else
  typedef void (*CallbackFunction)(Button2 &);
#endif

  CallbackFunction pressed_cb = NULL;
  CallbackFunction released_cb = NULL;
  CallbackFunction change_cb = NULL;
  CallbackFunction tap_cb = NULL;
  CallbackFunction click_cb = NULL;
  CallbackFunction long_cb = NULL;
  CallbackFunction double_cb = NULL;
  CallbackFunction triple_cb = NULL;
  CallbackFunction longclick_detected_cb = NULL;

public:
  Button2();
  Button2(byte attachTo, byte buttonMode = INPUT_PULLUP, boolean isCapacitive = false, boolean activeLow = true);

  void begin(byte attachTo, byte buttonMode = INPUT_PULLUP, boolean isCapacitive = false , boolean activeLow  = true);

  void setDebounceTime(unsigned int ms);
  void setLongClickTime(unsigned int ms);
  void setDoubleClickTime(unsigned int ms);
  
  unsigned int getDebounceTime() const;
  unsigned int getLongClickTime() const;
  unsigned int getDoubleClickTime() const;
  byte getAttachPin() const;

  void setLongClickDetectedRetriggerable(bool retriggerable);

  void reset();

  void setChangedHandler(CallbackFunction f);
  void setPressedHandler(CallbackFunction f);
  void setReleasedHandler(CallbackFunction f);

  void setTapHandler(CallbackFunction f);
  void setClickHandler(CallbackFunction f);
  void setDoubleClickHandler(CallbackFunction f);
  void setTripleClickHandler(CallbackFunction f);

  void setLongClickHandler(CallbackFunction f);
  void setLongClickDetectedHandler(CallbackFunction f);

  unsigned int wasPressedFor() const;
  boolean isPressed() const;
  boolean isPressedRaw() const;

  byte getNumberOfClicks() const;
  byte getClickType() const;

  bool operator == (Button2 &rhs);

  void loop();

private: 
  byte _getState();

};
/////////////////////////////////////////////////////////////////
#endif
/////////////////////////////////////////////////////////////////

Button2.cpp (from a lib )

/////////////////////////////////////////////////////////////////
/*
  Button2.cpp - Arduino Library to simplify working with buttons.
  Created by Lennart Hennigs.
*/
/////////////////////////////////////////////////////////////////

#include "Button2.h"

/////////////////////////////////////////////////////////////////

Button2::Button2() {  
  pin = UNDEFINED_PIN;
}

/////////////////////////////////////////////////////////////////

Button2::Button2(byte attachTo, byte buttonMode /* = INPUT_PULLUP */, boolean isCapacitive /* = false */, boolean activeLow /* = true */) {
  begin(attachTo, buttonMode, isCapacitive, activeLow);
}

/////////////////////////////////////////////////////////////////

void Button2::begin(byte attachTo, byte buttonMode /* = INPUT_PULLUP */, boolean isCapacitive /* = false */, boolean activeLow /* = true */) {  
  pin = attachTo;
  longclick_detected_retriggerable = false;
  _pressedState = activeLow ? LOW : HIGH;
  setDebounceTime(DEBOUNCE_MS);
  setLongClickTime(LONGCLICK_MS);
  setDoubleClickTime(DOUBLECLICK_MS);
  if (!isCapacitive) {
    pinMode(attachTo, buttonMode);
  } else {
    is_capacitive = true;
  }	
  //  state = activeLow ? HIGH : LOW;
  state = _getState();
  prev_state = state ;
}

/////////////////////////////////////////////////////////////////

void Button2::setDebounceTime(unsigned int ms) {
  debounce_time_ms = ms;
}
    
/////////////////////////////////////////////////////////////////

void Button2::setLongClickTime(unsigned int ms) {
  longclick_time_ms = ms;
}

/////////////////////////////////////////////////////////////////

void Button2::setDoubleClickTime(unsigned int ms) {
  doubleclick_time_ms = ms;
}

/////////////////////////////////////////////////////////////////

unsigned int Button2::getDebounceTime() const {
  return debounce_time_ms;
}

/////////////////////////////////////////////////////////////////

unsigned int Button2::getLongClickTime() const {
  return longclick_time_ms;
}

/////////////////////////////////////////////////////////////////

unsigned int Button2::getDoubleClickTime() const {
  return doubleclick_time_ms;
}

/////////////////////////////////////////////////////////////////

byte Button2::getAttachPin() const {
  return pin;
}

/////////////////////////////////////////////////////////////////

bool Button2::operator == (Button2 &rhs) {
  return (this==&rhs);    
}
      
/////////////////////////////////////////////////////////////////

void Button2::setLongClickDetectedRetriggerable(bool retriggerable) {
  longclick_detected_retriggerable = retriggerable;
}
/////////////////////////////////////////////////////////////////

void Button2::setChangedHandler(CallbackFunction f) {
  change_cb = f;
}
    
/////////////////////////////////////////////////////////////////

void Button2::setPressedHandler(CallbackFunction f) {
  pressed_cb = f; 
}

/////////////////////////////////////////////////////////////////

void Button2::setReleasedHandler(CallbackFunction f) {
  released_cb = f; 
}
        
/////////////////////////////////////////////////////////////////

void Button2::setClickHandler(CallbackFunction f) {
  click_cb = f;
}

/////////////////////////////////////////////////////////////////

void Button2::setTapHandler(CallbackFunction f) {
  tap_cb = f;
}

/////////////////////////////////////////////////////////////////

void Button2::setLongClickHandler(CallbackFunction f) {
  long_cb = f;
}

/////////////////////////////////////////////////////////////////

void Button2::setDoubleClickHandler(CallbackFunction f) {
  double_cb = f;
}

/////////////////////////////////////////////////////////////////

void Button2::setTripleClickHandler(CallbackFunction f) {
  triple_cb = f;
}

/////////////////////////////////////////////////////////////////

void Button2::setLongClickDetectedHandler(CallbackFunction f) {
  longclick_detected_cb = f;
}

/////////////////////////////////////////////////////////////////

unsigned int Button2::wasPressedFor() const {
  return down_time_ms;
}

/////////////////////////////////////////////////////////////////

boolean Button2::isPressed() const {
  return (state == _pressedState);
}

/////////////////////////////////////////////////////////////////

boolean Button2::isPressedRaw() const {
  return (digitalRead(pin) == _pressedState);
}

/////////////////////////////////////////////////////////////////

byte Button2::getNumberOfClicks() const {
    return click_count;
}

/////////////////////////////////////////////////////////////////

byte Button2::getClickType() const {
    return last_click_type;
}

/////////////////////////////////////////////////////////////////

byte Button2::_getState() {
    byte state = 0;
    if (!is_capacitive) {
      state = digitalRead(pin);
    } else {
      #if defined(ARDUINO_ARCH_ESP32)
        int capa = touchRead(pin);
        state = capa < CAPACITIVE_TOUCH_THRESHOLD ? LOW : HIGH;
      #endif
    }
    return state;
}

/////////////////////////////////////////////////////////////////

void Button2::loop() {
  if (pin != UNDEFINED_PIN) {
    prev_state = state;
    unsigned long now = millis();
    state = _getState();
    // is button pressed?
    if (state == _pressedState && prev_state != _pressedState) {
      down_ms = now;
      pressed_triggered = false;
      click_ms = down_ms;

    // trigger pressed event (after debounce has passed)
    } else if (state == _pressedState && !pressed_triggered && (now - down_ms >= debounce_time_ms)) {
      pressed_triggered = true;
      click_count++;
      if (change_cb != NULL) change_cb (*this);      
      if (pressed_cb != NULL) pressed_cb (*this);

    // is the button released?
    } else if (state != _pressedState && prev_state == _pressedState) {
      down_time_ms = now - down_ms;
      // is it beyond debounce time?
      if (down_time_ms >= debounce_time_ms) {
        // trigger release        
        if (change_cb != NULL) change_cb (*this);
        if (released_cb != NULL) released_cb (*this);
        // trigger tap
        if (tap_cb != NULL) tap_cb (*this);        
        // was it a longclick? (preceeds single / double / triple clicks)
        if (down_time_ms >= longclick_time_ms) {
          longclick_detected = true;
        }
      }

    // is the button released and the time has passed for multiple clicks?
    } else if (state != _pressedState && now - click_ms > doubleclick_time_ms) {
      // was there a longclick?
      if (longclick_detected) {
        // was it part of a combination?
        if (click_count == 1) {
          last_click_type = LONG_CLICK;
          if (long_cb != NULL) long_cb (*this);
        }
        longclick_detected = false;
        longclick_detected_reported = false;
        longclick_detected_counter = 0;
        // determine the number of single clicks
      } else if (click_count > 0) {
        switch (click_count) {
          case 1: 
            last_click_type = SINGLE_CLICK;
            if (click_cb != NULL) click_cb (*this);
            break;
          case 2: 
            last_click_type = DOUBLE_CLICK;
            if (double_cb != NULL) double_cb (*this);
            break;
          case 3: 
            last_click_type = TRIPLE_CLICK;
            if (triple_cb != NULL) triple_cb (*this);
            break;
        }
      }
      click_count = 0;
      click_ms = 0;
    }

    bool longclick_period_detected = now - down_ms >= (longclick_time_ms * (longclick_detected_counter + 1));

    // check to see that the longclick_ms period has been exceeded and call the appropriate callback
    if (state == _pressedState && longclick_period_detected && !longclick_detected_reported) {
      longclick_detected_reported = true;
      longclick_detected = true;
      if (longclick_detected_retriggerable) {
        // if it's retriggerable then we bump the counter and reset the "reported" flag (as the counter will stop the false trigger)
        longclick_detected_counter++;
        longclick_detected_reported = false;
      }
//      longpress_detected_ms = now;
      if (longclick_detected_cb != NULL)
        longclick_detected_cb(*this);
    }
  }
}

/////////////////////////////////////////////////////////////////

void Button2::reset() {
  pin = UNDEFINED_PIN;
  click_count = 0;
  last_click_type = 0;
  down_time_ms = 0;
  pressed_triggered = false;
  longclick_detected = false;
  longclick_detected_reported = false;
	
  pressed_cb = NULL;
  released_cb = NULL;
  change_cb = NULL;
  tap_cb = NULL;
  click_cb = NULL;
  long_cb = NULL;
  double_cb = NULL;
  triple_cb = NULL;
  longclick_detected_cb = NULL;
}

myFunctions.h

#pragma once

#ifndef myFUNCTIONS_H 

#define myFUNCTIONS_H

//#ifndef DEF_ActionMessage

//#define DEF_ActionMessage

#include "Button2.h"

extern TFT_eSPI tft;

Button2 button_1, button_2, button_3, button_4;

void b_init();

class ActionBox;

class ActionBox {

  public:

    int BoxNum;

};

class ActionMessage {

  public:

  //ActionMessage();

  //~ActionMessage();

  //int BoxNum;

  void UpdateMessage(int BoxNum);

};

ActionBox Box1;

ActionBox Box2;

ActionBox Box3;

ActionBox Box4;

ActionMessage Act;

Box1.BoxNum = 1;

Box2.BoxNum = 2;

Box3.BoxNum = 3;

Box4.BoxNum = 4;

void handler(Button2& btn) {

    switch (btn.getClickType()) {

        case SINGLE_CLICK:

            break;

        case DOUBLE_CLICK:

            Serial.print("double ");

            break;

    }

    Serial.print("click ");

    Serial.print("on button #");

    Serial.print((btn == button_1) ? "1" : "2");

    Serial.println();

    if (btn == button_1) {

      Serial.println("A clicked");

      

    } else if (btn == button_2) {

      Serial.println("B clicked");

      //tft.drawRoundRect(46, 101, 30, 30, 4, TFT_RED);

    } else if (btn == button_3) {

      Serial.println("C clicked");

      //sendCommand(CMD_PAUSE,0x01,2) ;

    } else if (btn == button_4) {

      Serial.println("D clicked");

      //sendCommand(CMD_PAUSE,0x01,2) ;

    }

}

/////////////////////////////////////////////////////////////////

/*

class ActionMessage {

  ActionMessage();

  ~ActionMessage();

  public:

    int BoxNum;

    void ActionMessage::UpdateMessage(int BoxNum);

    

};

class ActionBox {

  public:

    int BoxNum;

};

*/

#endif

myFunctions.cpp

#include <TFT_eSPI.h>
#include "myFunctions.h"



void ActionMessage::UpdateMessage(int BoxNum){
  tft.fillRect(100, 75, 100, 20, TFT_CYAN);
  tft.setCursor(100,75);
  tft.print(BoxNum);
};

#define BUTTON_PIN_1  4
#define BUTTON_PIN_2  16
#define BUTTON_PIN_3  17
#define BUTTON_PIN_4  5

//Preferences preferences;


unsigned long now = 0;
byte counter = 0;

void longpress(Button2& btn) {
    unsigned int time = btn.wasPressedFor();
    Serial.print("You clicked ");
    if (time > 1500) {
        Serial.print("a really really long time.");
    } else if (time > 1000) {
        Serial.print("a really long time.");
    } else if (time > 500) {
        Serial.print("a long time.");        
    } else {
        Serial.print("long.");        
    }
    Serial.print(" (");        
    Serial.print(time);        
    Serial.println(" ms)");
}
/////////////////////////////////////////////////////////////////

/////////////////////////////////////////////////////////////////

void pressed(Button2& btn) {
  counter++;
  if (counter == 2) {
    now = millis();
    Serial.println("now!");
  }
  if (btn == button_1) {
      Serial.println("A pressed");
      tft.drawRoundRect(6, 101, 30, 30, 4, TFT_RED);
      tft.drawRoundRect(6, 100, 30, 30, 4, TFT_RED);
      tft.drawRoundRect(7, 102, 28, 28, 3, TFT_PINK);
      Act.UpdateMessage (Box1.BoxNum);
    } else if (btn == button_2) {
      Serial.println("B pressed");
      tft.drawRoundRect(46, 101, 30, 30, 4, TFT_RED);
      tft.drawRoundRect(46, 100, 30, 30, 4, TFT_RED);
      tft.drawRoundRect(47, 102, 28, 28, 3, TFT_PINK);
      Act.UpdateMessage (Box2.BoxNum);
    } else if (btn == button_3) {
      tft.drawRoundRect(86, 101, 30, 30, 4, TFT_RED);
      tft.drawRoundRect(86, 100, 30, 30, 4, TFT_RED);
      tft.drawRoundRect(87, 102, 28, 28, 3, TFT_PINK);
      Act.UpdateMessage (Box3.BoxNum);
    } else if (btn == button_4) {
      tft.drawRoundRect(126, 101, 30, 30, 4, TFT_RED);
      tft.drawRoundRect(126, 100, 30, 30, 4, TFT_RED);
      tft.drawRoundRect(127, 102, 28, 28, 3, TFT_PINK);
      Act.UpdateMessage (Box4.BoxNum);
    }
  
}

/////////////////////////////////////////////////////////////////

void released(Button2& btn) {
  counter--;
  if (counter == 0) {
    if (now != 0) {
      Serial.print("Pressed for: ");
      Serial.print(millis() - now);
      Serial.println("ms");
    }
    now = 0;
  }
  if (btn == button_1) {
      tft.drawRoundRect(6, 101, 30, 30, 4, TFT_CYAN);
      tft.drawRoundRect(6, 100, 30, 30, 4, TFT_CYAN);
      tft.drawRoundRect(7, 102, 28, 28, 3, TFT_CYAN);
      tft.drawRoundRect(5, 100, 32, 32, 5, 0);
    } else if (btn == button_2) {
      Serial.println("B pressed");
      tft.drawRoundRect(46, 101, 30, 30, 4, TFT_CYAN);
      tft.drawRoundRect(46, 100, 30, 30, 4, TFT_CYAN);
      tft.drawRoundRect(47, 102, 28, 28, 3, TFT_CYAN);
      tft.drawRoundRect(45, 100, 32, 32, 5, 0);
    } else if (btn == button_3) {
      tft.drawRoundRect(86, 101, 30, 30, 4, TFT_CYAN);
      tft.drawRoundRect(86, 100, 30, 30, 4, TFT_CYAN);
      tft.drawRoundRect(87, 102, 28, 28, 3, TFT_CYAN);
      tft.drawRoundRect(85, 100, 32, 32, 5, 0);
    } else if (btn == button_4) {
      tft.drawRoundRect(126, 101, 30, 30, 4, TFT_CYAN);
      tft.drawRoundRect(126, 100, 30, 30, 4, TFT_CYAN);
      tft.drawRoundRect(127, 102, 28, 28, 3, TFT_CYAN);
      tft.drawRoundRect(125, 100, 32, 32, 5, 0);
    }
}

/////////////////////////////////////////////////////////////////


void b_init() {
  
  Serial.println();

  //preferences.begin("my-app", false);

  delay(10);

  button_1.begin(BUTTON_PIN_1);
  button_1.setClickHandler(handler);
  button_1.setDoubleClickHandler(handler);
  button_1.setLongClickHandler(longpress);
  button_1.setPressedHandler(pressed);
  button_1.setReleasedHandler(released);

  button_2.begin(BUTTON_PIN_2);
  button_2.setClickHandler(handler);
  button_2.setDoubleClickHandler(handler);
  button_2.setLongClickHandler(longpress);
  button_2.setPressedHandler(pressed);
  button_2.setReleasedHandler(released);

  button_3.begin(BUTTON_PIN_3);
  button_3.setClickHandler(handler);
  button_3.setDoubleClickHandler(handler);
  button_3.setLongClickHandler(longpress);
  button_3.setPressedHandler(pressed);
  button_3.setReleasedHandler(released);

  button_4.begin(BUTTON_PIN_4);
  button_4.setClickHandler(handler);
  button_4.setDoubleClickHandler(handler);
  button_4.setLongClickHandler(longpress);
  button_4.setPressedHandler(pressed);
  button_4.setReleasedHandler(released);

}

/////////////////////////////////////////////////////////////////

void b_loop() {
  button_1.loop();
  button_2.loop();
}

These lines are not allowed outside of a function:

You could add a constructor that does the initialisation instead, e.g.:

class ActionBox {
  public:
    ActionBox(int num) {
      BoxNum = num;
    }
    int BoxNum;
};

You can use this as follows:

ActionBox Box1(1);

Thanks for your answers, i tried what @jfjlaros suggested and now i'm back to this :

C:\Users\antoi\AppData\Local\Temp\arduino-sketch-019FABCAC6A30242536B26CB9506D08B\sketch\myFunctions.cpp.o:(.bss.button_1+0x0): multiple definition of `button_1'
C:\Users\antoi\AppData\Local\Temp\arduino-sketch-019FABCAC6A30242536B26CB9506D08B\sketch\Sprite_loading_3.ino.cpp.o:(.bss.button_1+0x0): first defined here
C:\Users\antoi\AppData\Local\Temp\arduino-sketch-019FABCAC6A30242536B26CB9506D08B\sketch\myFunctions.cpp.o:(.bss.button_2+0x0): multiple definition of `button_2'
C:\Users\antoi\AppData\Local\Temp\arduino-sketch-019FABCAC6A30242536B26CB9506D08B\sketch\Sprite_loading_3.ino.cpp.o:(.bss.button_2+0x0): first defined here
C:\Users\antoi\AppData\Local\Temp\arduino-sketch-019FABCAC6A30242536B26CB9506D08B\sketch\myFunctions.cpp.o:(.bss.button_3+0x0): multiple definition of `button_3'
C:\Users\antoi\AppData\Local\Temp\arduino-sketch-019FABCAC6A30242536B26CB9506D08B\sketch\Sprite_loading_3.ino.cpp.o:(.bss.button_3+0x0): first defined here
C:\Users\antoi\AppData\Local\Temp\arduino-sketch-019FABCAC6A30242536B26CB9506D08B\sketch\myFunctions.cpp.o:(.bss.button_4+0x0): multiple definition of `button_4'
C:\Users\antoi\AppData\Local\Temp\arduino-sketch-019FABCAC6A30242536B26CB9506D08B\sketch\Sprite_loading_3.ino.cpp.o:(.bss.button_4+0x0): first defined here
C:\Users\antoi\AppData\Local\Temp\arduino-sketch-019FABCAC6A30242536B26CB9506D08B\sketch\myFunctions.cpp.o: In function `handler(Button2&)':
myFunctions.cpp:(.text._Z7handlerR7Button2+0x0): multiple definition of `handler(Button2&)'
C:\Users\antoi\AppData\Local\Temp\arduino-sketch-019FABCAC6A30242536B26CB9506D08B\sketch\Sprite_loading_3.ino.cpp.o:Sprite_loading_3.ino.cpp:(.text._Z7handlerR7Button2+0x0): first defined here
C:\Users\antoi\AppData\Local\Temp\arduino-sketch-019FABCAC6A30242536B26CB9506D08B\sketch\myFunctions.cpp.o:(.bss.Box1+0x0): multiple definition of `Box1'
C:\Users\antoi\AppData\Local\Temp\arduino-sketch-019FABCAC6A30242536B26CB9506D08B\sketch\Sprite_loading_3.ino.cpp.o:(.bss.Box1+0x0): first defined here
C:\Users\antoi\AppData\Local\Temp\arduino-sketch-019FABCAC6A30242536B26CB9506D08B\sketch\myFunctions.cpp.o:(.bss.Box2+0x0): multiple definition of `Box2'
C:\Users\antoi\AppData\Local\Temp\arduino-sketch-019FABCAC6A30242536B26CB9506D08B\sketch\Sprite_loading_3.ino.cpp.o:(.bss.Box2+0x0): first defined here
C:\Users\antoi\AppData\Local\Temp\arduino-sketch-019FABCAC6A30242536B26CB9506D08B\sketch\myFunctions.cpp.o:(.bss.Box3+0x0): multiple definition of `Box3'
C:\Users\antoi\AppData\Local\Temp\arduino-sketch-019FABCAC6A30242536B26CB9506D08B\sketch\Sprite_loading_3.ino.cpp.o:(.bss.Box3+0x0): first defined here
C:\Users\antoi\AppData\Local\Temp\arduino-sketch-019FABCAC6A30242536B26CB9506D08B\sketch\myFunctions.cpp.o:(.bss.Box4+0x0): multiple definition of `Box4'
C:\Users\antoi\AppData\Local\Temp\arduino-sketch-019FABCAC6A30242536B26CB9506D08B\sketch\Sprite_loading_3.ino.cpp.o:(.bss.Box4+0x0): first defined here
C:\Users\antoi\AppData\Local\Temp\arduino-sketch-019FABCAC6A30242536B26CB9506D08B\sketch\myFunctions.cpp.o:(.bss.Act+0x0): multiple definition of `Act'
C:\Users\antoi\AppData\Local\Temp\arduino-sketch-019FABCAC6A30242536B26CB9506D08B\sketch\Sprite_loading_3.ino.cpp.o:(.bss.Act+0x0): first defined here
collect2.exe: error: ld returned 1 exit status
Compilation error: exit status 1}

The problem is that you have code in the header file. All global variables and functions should be moved to the cpp file. In the header you can publish the global variables with:

extern ActionBox Box1;
extern ActionBox Box2;

etc. Make sure to add a prototype for the functions you want to export, e.g.,

void handler(Button2&);

Thank you i'll try that.

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