Variable or field 'button_on_press' declared void

Hi,

I am trying to implement the library GFbutton according to the example GFButton-advanced.ino into a big project totaling 700 lines of code (hard to past all that stuff here).

I have at the root scope (verified):

#include "GFButton.h"

#define BUTTON_UP    35

GFButton btn = GFButton(BUTTON_UP);

void button_on_press(GFButton & btn)
{
  // Use of the press count field
  Serial.print(F("Button pressed "));
  Serial.print(btn.getPressCount());
  Serial.println(F(" times."));
}

At the setup() scope:

  // Attach callbacks to the button object
  btn.setPressHandler(button_on_press);
  btn.setHoldHandler(button_on_hold);
  btn.setReleaseHandler(button_on_release);
  btn.setClicksHandler(button_on_click);

At the loop() scope
btn.process();

And upon compiling I get these messages:


b_Functions:14:22: error: variable or field 'button_on_press' declared void
 void button_on_press(GFButton & btn)
                      ^
b_Functions:14:22: error: 'GFButton' was not declared in this scope
b_Functions:14:33: error: 'btn' was not declared in this scope
 void button_on_press(GFButton & btn)
...
exit status 1
variable or field 'button_on_press' declared void

I am clueless, the instance declaration (at root scope) and the function button_on_press() are just consecutive.
How can the compiler issue 'GFButton' was not declared in this scope?

Has anyone an idea what is harming here?

Thank you for your enlightenments.

Just a wild guess - Perhaps because there is a bug in your code?

Sadly, since you only provided a few little useless snippets, nobody here will be able to help you.

Please post a small but complete sketch that illustrates the problem

Thank you,
but I have already posted the complete related code in the given snippets and given them respecting the code sequence:

The first part (from which the error issues) is really at the root scope, I have absolutely no opened curly brace upfront. I have searched for them!

The code is that of the library example.
The rest of my code is vast and is not (should not be) related to the button function.

Of course I have also searched for "button", and "btn" in all flavors throughout my code: no other occurrence found.

I understand that i MUST be something incompatible in my code, but i really can't figure it out.
What could I else search for?

My code (without buttons) is at Github: https://github.com/rin67630/Drok-Juntek-on-steroids

Thank you for your help.

Stitching together the snippets that you have posted, which you could have done yourself, I get

#include "GFButton.h"

#define BUTTON_UP    35

GFButton btn = GFButton(BUTTON_UP);

void button_on_press(GFButton & btn)
{
  // Use of the press count field
  Serial.print(F("Button pressed "));
  Serial.print(btn.getPressCount());
  Serial.println(F(" times."));
}
void setup()
{
  Serial.begin(115200);
  while (!Serial);
  // Attach callbacks to the button object
  btn.setPressHandler(button_on_press);
  //  btn.setHoldHandler(button_on_hold);
  //  btn.setReleaseHandler(button_on_release);
  //  btn.setClicksHandler(button_on_click);
}

void loop()
{
  btn.process();
}

This compiles for me, so the problem is elsewhere in your code

Yes, i know.
Without the above snippets my code compiles flawlessly, the snippets alone compile flawlessly, only together it does not work.

But i really can’t figure it out what may conflict.
Why is the compiler suddenly complaining about a "void", it did not before?

What could I else search for?
Which kind of conflict could one look at?

Summary :
The code that you posted compiles correctly

Conclusion :
The problem is caused by the code that you have not posted

Bite the bullet and post or attach your complete sketch

Hi,
Try this from the examples.

/**
   GeekFactory - "INNOVATING TOGETHER"
   Distribucion de materiales para el desarrollo e innovacion tecnologica
   www.geekfactory.mx

   Advanced example for GFButton library. This example ilustrates the callback programming
   pattern and also there are examples for most methods on the GFButton class.
*/

#include "GFButton.h"

GFButton btn = GFButton(35);

/**
   PROGRAM INITIALIZATION
*/
void setup() {
  // Prepare serial interface
  Serial.begin(9600);

  // Attach callbacks to the button object
  btn.setPressHandler(button_on_press);
  btn.setHoldHandler(button_on_hold);
  btn.setReleaseHandler(button_on_release);
  btn.setClicksHandler(button_on_click);

  // Show dialog to serial monitor
  Serial.println(F("----------------------------------------------------"));
  Serial.println(F("            GFBUTTON LIBRARY TEST PROGRAM           "));
  Serial.println(F("             https://www.geekfactory.mx             "));
  Serial.println(F("----------------------------------------------------"));
}

/**
   MAIN PROGRAM LOOP
*/
void loop() {

  // Process button in main loop
  btn.process();

  // Perform other tasks here if necessary
}

/**
   CALLBACK FOR BUTTON PRESS EVENT
*/
void button_on_press(GFButton & btn)
{
  // Use of the press count field
  Serial.print(F("Button pressed "));
  Serial.print(btn.getPressCount());
  Serial.println(F(" times."));
}

/**
   CALLBACK FOR BUTTON HOLD EVENT
*/
void button_on_hold(GFButton & btn)
{
  // Use only the first gold event
  if (btn.isFirstHold())
  {
    // Use of the getHoldTime() method
    Serial.print(F("Button held for "));
    Serial.print(btn.getHoldTime() / 1000);
    Serial.println(F(" seconds."));
  } else {
    // Print dots as long as the button is pressed
    Serial.print('.');
  }
}

/**
   CALLBACK FOR BUTTON RELEASE EVENT
*/
void button_on_release(GFButton & btn)
{
  // Use of the getPin() method
  Serial.print(F("Button on pin "));
  Serial.print(btn.getPin());
  
  // example for wasLongPress() method
  if (btn.wasLongPress()) {
    Serial.println(F(" released after long press"));
  } else {
    Serial.println(F(" released after short press"));
  }
}

/**
   CALLBACK FOR CLICK EVENT
*/
void button_on_click(GFButton & button)
{
  // Use of the getClicks() method
  Serial.print(F("Button multiple clicks detected (within 1 sec): "));
  Serial.println(btn.getClicks());
}

It compiles for me.
Your code when I rejoin it has this error.

'button_on_hold' was not declared in this scope

Tom... :grinning: :+1: :coffee: :australia:

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