Need Help understanding class implementation

Alright, Second post today. Obviously its just one of those days I guess. Anyway... Here's my predicament.

I'm trying to use the 'class' function as a debouncer for many buttons attached to my sketch. The goal is to have the debouncer interpret if the pin is HIGH or LOW. I just started working with classes recently so my understanding of them isn't the best yet.

The error i'm getting is:

exit status 1
request for member 'update' in '1', which is of non-class type 'int'

and My Test code is here:

int TestButton = 10;
int x;
class Debounce
{
  int currentTime;
  int previousTime = 0;
  int sysVar;
  int waitVar;
  int pinVar;
  
  public:
  Debounce(int main, long wait, int pin)
  {
    waitVar = wait;
    pinVar = digitalRead(pin);
    sysVar = main;
  }
  void Update(){
    unsigned long currentTime = millis();
    if ((pinVar==HIGH) && (currentTime-previousTime >= waitVar)){
      sysVar = HIGH;
      previousTime=currentTime;
    }
    else {
      sysVar = LOW;
    }
  }
};
Debounce B1(x, 200, 10);
void setup() {
  pinMode(TestButton, INPUT);
  Serial.begin(9600);
  Serial.println("Testing...");
}
void loop() {
  B1.update();
  if (x==HIGH){
    serial.println("Test");
  }
}

Yes I know there are already debouncing libraries out there but I prefer to keep my sketch simple as it will have more of other things going on.

The message looks wrong to me, but the real problem for you is that you have defined Update() (upper case U) but used update() (lower case u).

Don't use B1 as an object name, it is predefined in Arduino as something like

#define B1 1

Whandall has made a good catch, one that makes the message correct. I wonder what other names should be avoided?

..... wow....

I've got the biggest headache from writing that over and over and thats the issue? Thank you for pointing that out Wandall and Thank you for that catch as well vaj4088.

I spoke too soon I'm afraid. I am not getting the results that I was expecting. The class isn't sending the value back to the main loop. Do you know how I can correct this?

How could it?

Perhaps you want to pass a reference to an int to your class?

class Debounce
{
  ...
  int& sysVar;
 
  Debounce(int& main, long wait, int pin) : sysVar(main)
  {
    ...
  }
}

Whandall:
How could it?

Perhaps you want to pass a reference to an int to your class?

class Debounce

{
  ...
  int& sysVar;

Debounce(int& main, long wait, int pin) : sysVar(main)
  {
    ...
  }
}

Do you know of a more in-dept example/tutorial? I'm not really understanding the example above. Nor do I want to waste your time with little questions. :frowning:

EDIT:
My goal (for better understanding if it helps), Is to basically condense this (see below), into a class with the exception of Serial.println. That is just being used as an example but I'm wanting press a button, it call the class to trigger the button while also debouncing the button for a variable period of time. Then relay the "HIGH" trigger to the main loop.

int TestButton = 10;
int x = 0;

unsigned long previousTime = 0;
long waitTime = 1300;

void setup() {
  pinMode(TestButton, INPUT);
  Serial.begin(9600);
  Serial.println("Testing...");
}
void loop() {
  unsigned long currentTime = millis();
  x = digitalRead(TestButton);
  if((x==HIGH) && (currentTime - previousTime >= waitTime)){
    Serial.println("Test Ran");
    previousTime = currentTime;
  }
}

http://www.learncpp.com/cpp-tutorial/8-5a-constructor-member-initializer-lists/

Whandall:
C++ References
C++ Reference Variables - Cprogramming.com

http://www.learncpp.com/cpp-tutorial/8-5a-constructor-member-initializer-lists/

Thank you! :slight_smile: