Enabling & Disabling keypad function using pusbutton toggle action

#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);

#include <Keypad.h>

const byte ROWS = 4; 
const byte COLS = 4; 

char hexaKeys[ROWS][COLS] = {
  {'1', '2', '3', 'A'},
  {'4', '5', '6', 'B'},
  {'7', '8', '9', 'C'},
  {'*', '0', '#', 'D'}
};

byte rowPins[ROWS] = {A15, A14, A13, A12}; 
byte colPins[COLS] = {A11, A10, A9, A8}; 

Keypad customKeypad = Keypad(makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS); 

#include <TM1637Display.h>
#define CLK 3
#define DIO 2
TM1637Display display = TM1637Display (CLK, DIO);

int count;

 const byte button = 22; // Push Button Pin D 2  
 const byte led = 4;
 int status = false;

 void setup()  
 {
  lcd.init();
  lcd.backlight();
  display.setBrightness(4); // activate display
  Serial.begin(9600);
  pinMode(led, OUTPUT);
  digitalWrite(led, LOW);
  pinMode(button, INPUT_PULLUP);
 }

 void loop()  
 {  
 if (digitalRead(button) == true)  
 {  
 status = !status;  
 digitalWrite(led, !status);
 }   
 while(digitalRead(button) == true);
 delay(50);

 char customKey = customKeypad.getKey();
  if (customKey)
  {
    Serial.println(customKey);
    lcd.setCursor(1, 1);
    lcd.print(customKey);
    count = customKey - '0';
    display.showNumberDec(count);
  }

 } 

With Arduino MEGA and above code I am trying to make a function like :

A push button (pin 22) acts as a toggle switch
An LED is present as indicator to show toggle action

Now this toggle switch system is added for the purpose to
activate the keypad keys to work as per it's code and
to deactivate the working of keypad completely

One of the toggle mode will activate and other will deactivate

I tried with code posted above, but it is working with a
"key press and hold" thing.

It has no relation with the toggle action added
If we use to press and hold the push button continuously
then the keypad use to get work and on release of the push button.

keypad function gets deactivate and no key gets print on LCD!!!!!

Please help !!!

My state change for active low inputs (switches) tutorial may help.

demo using the state changed detection method to toggle a flag (mode).

// by C Goulding aka groundFungus

const byte  buttonPin = A0;    // the pin to which the pushbutton is attached
const byte ledPin = 13;       // the pin to which the LED is attached

bool mode = false;

void setup()
{
   // initialize the button pin as a input with internal pullup enabled
   pinMode(buttonPin, INPUT_PULLUP);
   // initialize the LED as an output:
   pinMode(ledPin, OUTPUT);
   // initialize serial communication:
   Serial.begin(115200);
   Serial.println("Select mode with push button");
}

void loop()
{
   static unsigned long timer = 0;
   unsigned long interval = 50;  // check switch 20 times per second
   if (millis() - timer >= interval)
   {
      timer = millis();
      // read the pushbutton input pin:
      static bool lastButtonState = 1;     // previous state of the button
      bool buttonState = digitalRead(buttonPin);
      // compare the new buttonState to its previous state
      if (buttonState != lastButtonState)
      {
         if (buttonState == LOW)
         {
            // if the current state is LOW then the button
            // went from off to on:
            digitalWrite(ledPin, !digitalRead(ledPin)); // toggle the output
            
            mode = !mode;
            
            if (mode == true)
            {
               Serial.println("Manual mode\n");
            }
            else
            {
               Serial.println("Scan mode\n");
            }
         }
      }
      // save the current state as the last state,
      //for next time through the loop
      lastButtonState = buttonState;
   }
}

Ya I tried with code at very first attempt

but dint get into any hardware changes or some passive device add-on

One Question :

So is this the only way of adding components to solve the purpose ??

No other way of coding exist ??

I tried with this one and its working . . Thanks :blush:

Please check if any unnecessary things is present or no longer needed or bit different way I did

void loop()
{

   static unsigned long timer = 0;
   unsigned long interval = 20;
   if (millis() - timer >= interval)
   {
      timer = millis();
      
      // read the pushbutton input pin:
      buttonState = digitalRead(buttonPin);

      // compare the buttonState to its previous state
      if (buttonState != lastButtonState)
      {
         // if the state has changed, increment the counter
         if (buttonState == LOW)
         {
            // if the current state is LOW then the button went from off to on:
            buttonPushCounter++;
            Serial.println("on");
            Serial.print("number of button pushes: ");
            Serial.println(buttonPushCounter);
            
         }
         else
         {
            // if the current state is HIGH then the button went from on to off:
            Serial.println("off");
         }
          // save the current state as the last state, for next time through the loop
          lastButtonState = buttonState;
      }
  }

   // turns on the LED every four button pushes by checking the modulo of the
   // button push counter. the modulo function gives you the remainder of the
   // division of two numbers:
   if (buttonPushCounter % 2 == 0)// && buttonPushCounter > 0)
   {
      digitalWrite(ledPin, LOW);
   }
   else
   {
      digitalWrite(ledPin, HIGH);
      char customKey = customKeypad.getKey();
        if (customKey)
         {
            Serial.println(customKey);
            lcd.setCursor(1, 1);
            lcd.print(customKey);
            count = customKey - '0';
            display.showNumberDec(count);
         }
   }
   // delay(10);
}

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