Quick question regarding MobaTools

Using MobaTools when one prints the state of a button what should one expect:
0 or 1
true or false

as in:

if ( Buttons.pressed(0) ) {
    sprintf( txtBuf, " -Button 0 pressed -  %d", Buttons.state(0));
    Serial.println( txtBuf );
  }

Look at the .h for the prototype of the function

A bool can be promoted to an int so you’ll see 0 or 1, ideally you would take into account it’s a bool

Serial.println(Buttons.pressed(0)? "Pressed" : "not pressed");

In the above case, I believe "true" means "LOW"

In a library example, the buttons are tied HIGH with INPUT_PULLUP... which is LOW when pressed.

this setup():

void setup()
{
  Serial.begin(115200);
  while(!Serial);       // only for Leonardo/Micro ( mega32u4 based boards 
  
  for (int i = 0; i < anzahlButtons; i++)  {    
    // buttons must switch to Gnc
    pinMode(buttonPin[i], INPUT_PULLUP); 
  }
  Serial.println("Starting loop");
  sprintf( txtBuf, "max. managable buttons: %d", sizeof(button_t)*8 );
  Serial.println( txtBuf );
  Buttons.forceChanged();
}

from this example:

Thank you - so if I wanted to do it conditionally would this be OK

 if ( Buttons.pressed(0) ) {
   // do something
  }
  if ( !Buttons.pressed(0) ) {
  .. do some else
  }

Just an if/else

if ( Buttons.pressed(0) ) {
   // do something
} else {
  // do something else
}

got it thanks

Maybe its best to look into the documentation :wink: :

bool myButtons.pressed( uint8_t buttonNbr );
True if button ‘buttonNbr’ was pressed. This event is set when the button was pressed and
reset after the call of this method or if it is released.

Buttons.pressed(0) tells you the moment when the button becomes pressed ( state change detection ). So !Buttons.pressed(0) ( or the 'else' clause ) is nearly always true - even while you hold the botton down.

bool myButtons.state( uint8_t buttonNbr );
Get static state of button ‘buttonNbr’ (debounced).

Buttons.state(0) can be used to check if the button is held down. It's always true as long as the button is held down. So !Buttons.state(0) makes also sense as it is only true as long as the button is not held down.

OK I think I got that, I was thinking that if I Serial printed the state it would show either o or 1 true or false which does not appear to be the case.

From the processor's point of view everything is a binary number. 'false' is represented as 0 and everything else is treated as 'true'. If a function returns a 'bool', at the end it returns 0 or 1. It's simply a thing how you interpret it. And Serial.print() interprets it as a number.

Thank you I got it now.

In C++ a bool is a type and true and false not guaranteed to be internally represented by 0 and 1 by the norm (although it makes some sense they are).

When true and false are used in contexts where integral promotion occurs, they are implicitly promoted to int values with true becoming 1 and false 0.

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