0

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;
}
Slart42
  • 101
  • 9
  • 1
    I'm not seeing a global variable called `_pin`. `Button` has a member named `_pin` but you can't access that outside `Button` – NathanOliver Oct 30 '18 at 16:41

1 Answers1

2

_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!

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
  • Solved! I'm starting to learn c++, looks like i'll have to do some research about the difference between global and member variables. Thanks a lot. – Slart42 Oct 30 '18 at 17:17
  • @LewisMojica Which book are you using? – Lightness Races in Orbit Oct 30 '18 at 17:17
  • None really. I found a really good tutorial on YouTube https://www.youtube.com/watch?v=18c3MTX0PK0&list=PLlrATfBNZ98dudnM48yfGUldqGD0S4FFb when once i know the basics will continue learning with books – Slart42 Oct 30 '18 at 17:19
  • @LewisMojica Start with the books, not the Internet. Unless you already have a good understanding of what is and isn't good C++, how will you be able to tell if the tutorial you are following was written well and worth following? Without knowing the correct terminology to look up, how will you be able to search for what you need to know? [A good book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) solves both problems. – user4581301 Oct 30 '18 at 17:30
  • @user4581301 Can you recommend me a book? That would be helpful – Slart42 Oct 30 '18 at 17:36
  • [The Definitive C++ Book Guide and List](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) – user4581301 Oct 30 '18 at 17:38
  • Found the problem ;) – Lightness Races in Orbit Oct 30 '18 at 17:51
  • Thanks! I'll follow you recommendation – Slart42 Oct 31 '18 at 02:43