SOLVED: Global boolean

I have defined a lot of integers and floats; they work fine within several procedures.

But it seems that my global defined blnLongRight (= result of a long push on button Right) doesn't change. It should be handy to check true or false.

Question: it is possible to define global booleans???

I use Arduino 1.6.1

Hi ArduinoStarter1

Global booleans are allowed. Please post your code (put it between code tags, the </> button on the toolbar above the reply window).

Regards

Ray

it is possible to define global booleans???

Yes.
How is your attempt at doing it declared and where ? You have not by any chance declared the boolean variable both as a global and a local, but different, varible, have you ?
Can you post a small program to illustrate the problem ?

ArduinoStarter1:
I have defined a lot of integers and floats; they work fine within several procedures.

But it seems that my global defined blnLongRight (= result of a long push on button Right) doesn't change. It should be handy to check true or false.

Question: it is possible to define global booleans???

I use Arduino 1.6.1

Could you post the code snippet which suppose to change the blnLongRight?

Just curious how you determine the button has been pushed "long time"?

Hi Vaclav,

My problems with the gobal blnLongRight are solved.

Clikbutton.h gives you a lot of possibilities to detect:

  • normal single short push
  • long push
  • to count several short pushes in a while

I use a calibration routine with two (changeble) preset values.

  • A single push up/down will add/decrease the preset value with 1
  • Fast multiple pushes up/down will add/decrease the value with 10
  • With a long push right you will go to the next step.

The whole sample code is too long to enclose (the complete code is attached), but here a part:

/*
Programname: Sunlogger 023.ino 
Compiler: Arduino 1.6.1
The hardware circuit:
-   Arduino MEGA 2560
-   LCD 220*4 via I2C          Address 0x27
-   4 keys to digital inputs (pins 30, 31, 32, 33)
Version:     023
Author:      C.W.A. Baltus
Created:     9-5-2013
Modified:    04-07-2015
Blockdiagram: Ontwerp_Sunlogger.xlsm / Total operation
*/

//================================================================================================
//Libraries
#include <Wire.h>                 //Enables I2C communication
#include <ClickButton.h>          //Enables to detect key entries
#include <LiquidCrystal_I2C.h>    //Enables LCD via I2C

// Define ClickButton objects 
ClickButton keyLeft(33, LOW, CLICKBTN_PULLUP);
ClickButton keyUp(32, LOW, CLICKBTN_PULLUP);
ClickButton keyDown(31, LOW, CLICKBTN_PULLUP);
ClickButton keyRight(30, LOW, CLICKBTN_PULLUP);

//Define global parameters
boolean blnIRQflag, blnUp, blnDoubleUp, blnDown, blnDoubleDown, blnLongLeft, blnRight, blnLongRight;int intMenu, intMenu0=1 , intMenu1  =1, intMenu2 =1;
boolean blnUpdateDisplay = true;
String strLine1, strLine2, strLine3, strLine4;
  
//Declaration of parameters for calibration routines
float     sngY1 = 135, sngY2 = 1200, sngDec= 1, sngDelta = 1;  //Y1, Y2, decimals, delta
String    strSymbol = "Gi", strDimension = " W/m2" ;

//Define I2C addresses
#define adrLCD   0x27      //A0=A1=A2=1
LiquidCrystal_I2C lcd(adrLCD, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);  // Set the LCD I2C address to 0x27 for a 20 chars and 4 line display

//================================================================================================
void setup(){
  Serial.begin(9600);      // Open the serial port at 9600 bps:
  lcd.begin(20, 4);        // set the LCD address to 0x20 for a 16 chars and 2 line display
  
  //Prepare button functions, Setup button timers (all in milliseconds / ms)
  keyLeft.debounceTime = 20;    keyRight.debounceTime = 20;    keyUp.debounceTime = 20;    keyDown.debounceTime = 20;     // Debounce timer in ms
  keyLeft.multiclickTime = 250; keyRight.multiclickTime = 250; keyUp.multiclickTime = 250; keyDown.multiclickTime = 250;  // Time limit for multi clicks
  keyLeft.longClickTime = 1000; keyRight.longClickTime = 1000; keyUp.longClickTime = 1000; keyDown.longClickTime = 1000;  // Time until long clicks register}}

  //A lot of code is removed (not necessary for this demonstation)
  intMenu = 40;   //We start with Settingmenu
  intMenu1 = 1;    //Choose for submenu (Calibration) 
  blnUpdateDisplay = true;
  
  }  //End of setup

//================================================================================================
void loop() {
  //Check key entries
  keyLeft.Update(); 
  keyUp.Update();    
  keyDown.Update();    
  keyRight.Update();   
  
  if (keyUp.clicks   == 1) {blnUp = true;} else {blnUp=false;}
  if (keyDown.clicks == 1) {blnDown = true;} else {blnDown= false;}
  if (keyRight.clicks ==1) {blnRight = true;} else {blnRight = false;}
  if (keyLeft.clicks ==-1) {blnLongLeft = true;} else {blnLongLeft = false;}
  if (keyRight.clicks==-1) {blnLongRight = true;} else {blnLongRight = false;}
  
  switch (intMenu) {
    case 0:      //Introduction menu
        //Blabla
        break;
    case 40:     //Navigation within Settingmenu
      //Navigation within Settingmenu
      //Toggle between first and last measurements in Loggingmenu
      if (blnDown) {if (intMenu1 <6) {intMenu1++; blnUpdateDisplay=true;}}
      if (blnUp)   {if (intMenu1 >1) {intMenu1--; blnUpdateDisplay=true;}}
        
      if ( intMenu1 <= 3) {
            strLine1 = "SETTING MENU";
            strLine2 = "1. Set date/time";
            strLine3 = "2. Imp sett. SDcard";
            strLine4 = "3. Verify settings"; }
        else
            {strLine1 = "4. Calibrate Gi";
            strLine2 = "5. Calibrate Tamb";
            strLine3 = "6. Calibrate Tm";
            strLine4 = "";
         }  //End if

      if (blnUpdateDisplay == true) {
          Update_display();

Voorbeeld.ino (10.7 KB)

My problems with the gobal blnLongRight are solved.

For the benefit of others what was the problem and how did you solve it ?

I made an extract of my own program (see above) and it works!

Now I have to find out where the wrong instruction in my original code is.

Great,
now we know how it is done.

Last question - the class function returns positive or negative count ( state machine) to indicate how long was the button pushed.

You could use that return instead of setting bool flag.