Search code examples
c++arduinovisual-studio-codeplatformio

_pin is a global variable but i still get the error : '_pin' was not declared in this scope


Programming language: c++

When i compile my program i get this error:
/Button.cpp:50:18: error: '_pin' was not declared in this scope

It is telling me that the variable _pin wasn't declared in the scope of the function
bool longPush(unsigned log interval), but it was declared, i even used _pin in other function with no problem.

This are the files of the library Button that are causing me trouble:

Button.h:

#ifndef Button_h
#define Button_h
#include <Arduino.h>

class Button
{
private:
  byte _pin;
  byte _anti_bounce;
  bool _est_ant;
  bool _push;

public:
  enum PullRes : bool {FLOATING = false, PULLUP = true,};

  Button(byte pin, byte anti_bounce, PullRes = FLOATING);

  bool falling();
  bool rissing();
  bool check();
  bool longPush(unsigned long interval);    
};
#endif

Button.cpp:

#include <Arduino.h>
#include <Button.h>
#include <Timer.h>

Button::Button(byte pin, byte anti_bounce, PullRes mode = FLOATING) {
  _pin = pin;
  _anti_bounce = anti_bounce;
  if(mode == PULLUP) pinMode(pin,INPUT_PULLUP);
  else pinMode(pin,INPUT);

}

  bool Button::rissing() {                                          //Funcion que retorna un 1 cuando se detecta un rissing en el pin <_pin>
  bool puls;
  if ((digitalRead(_pin) == 1) && (_est_ant == 0)) {
    puls = 1;
    _est_ant = 1;
  }
  else {
    puls = 0;
    _est_ant = digitalRead(_pin);
  }
  delay(_anti_bounce);
  return puls;
}

bool Button::falling() {                                          //Funcion que retorna un 1 cuando se detecta un falling en el pin <_pin>
  bool puls;
  if ((digitalRead(_pin) == 0) && (_est_ant == 1)) {
    puls = 1;
    _est_ant = 0;
  }
  else {
    puls = 0;
    _est_ant = digitalRead(_pin);
  }
  delay(_anti_bounce);
  return puls;
}

bool Button::check(){                                             //Funcion que retorna el estado actual del pin <_pin>
  return digitalRead(_pin);
}

bool longPush(unsigned long interval){
  static Timer timer(interval);
  timer.setInterval(interval);
  static bool released = true;

  if(digitalRead(_pin)){ 
    timer.end();
    released = true;
    return false;
  } else if (!timer.isRunning() && released){
    released = false;
    timer.init();
    return false;
  } else if (timer.read()){
    timer.end();
    released = false;

    return true;
  }
  return false;
}

The error in in the .cpp file:

bool longPush(unsigned long interval){
  static Timer timer(interval);
  timer.setInterval(interval);
  static bool released = true;
////////////////////////////////////////
  if(digitalRead(_pin)){// <--- right here
    timer.end();
    released = true;
    return false;
  } else if (!timer.isRunning() && released){
    released = false;
    timer.init();
    return false;
  } else if (timer.read()){
    timer.end();
    released = false;

    return true;
  }
return false;
}

Solution

  • _pin is not a global variable; it is a member variable.

    You're trying to use it as if it were a global variable, because you're defining longPush (a global function) instead of Button::longPush (a member function).

    Here:

       bool Button::longPush(unsigned long interval){
    //      ^^^^^^^^
    

    You're not having this problem with the other functions because their definitions do not have this typo.

    It's an easy mistake to make, though: I've done it at least twice just today!