Storing input from 4x4 keypad for project

Hi, I need to know how to store a multi-digit input from a 4x4 keypad. My system includes an FSR, buzzer, ir sensor, 16x2 lcd, and a 4x4 keypad. What I would like to do is store the keypad input so when the FSR load reaches the entered value the buzzer goes off. Right now I have it set up to beep when the FSR reaches 100 grams.

My code:

#include <LiquidCrystal_I2C.h>
#include <SharpIR.h>
#include <Keypad.h>
#define IRPin A1
#define model 1080

SharpIR mySensor = SharpIR(IRPin, model);
LiquidCrystal_I2C lcd(0x27, 20, 4);

int distance_cm;

const int FSR_PIN = A0;
const float VCC = 4.98;
const float R_DIV = 3230.0; 

int buzzer = 10;

const byte ROWS = 4;
const byte COLS = 4;
char keys[ROWS][COLS] = { {'1','2','3','A'},{'4','5','6','B'},{'7','8','9','C'},{'*','0','#','D'} };
byte rowPins[ROWS] = {5,4,3,2};
byte colPins[COLS] = {9,8,7,6};
Keypad keypad = Keypad (makeKeymap(keys), rowPins, colPins, ROWS, COLS );


void setup() 
{
  Serial.begin(9600);
  pinMode(FSR_PIN, INPUT);
  lcd.init();
  lcd.backlight();
  Serial.begin(9600);
  pinMode (buzzer, OUTPUT);
}

void loop() 
{
  int fsrADC = analogRead(FSR_PIN);
  distance_cm = mySensor.distance();
  char key = keypad.getKey();
  
  if (fsrADC != 0)
  {
    float fsrV = fsrADC * VCC / 1023.0;
    float fsrR = R_DIV * (VCC / fsrV - 1.0);
    float force;
    float fsrG = 1.0 / fsrR;
    if (fsrR <= 600) 
    force = (fsrG - 0.00075) / 0.00000032639;
    else
    force =  fsrG / 0.000000642857;
    
    if (force >= 100)
    { digitalWrite (buzzer, HIGH); }
    else
    {
    noTone(buzzer);
    }

    lcd.setCursor(1, 0);
    lcd.print("Force: " + String(force) + "g");
    lcd.setCursor(0, 1);
    lcd.print("Distance: " + String(distance_cm) + "cm");
    delay(1000);
    lcd.clear();
  }
  else
  {}
}

Welcome to the forum

This should help

I would suggest to study Serial Input Basics to handle this but instead of listening asynchronously for the Serial line, you just listen to to keypad.getKey()

a block of code in loop() can check for key values from getKey(). (isn't NO_KEY(?) a possible return value)? that value is then appended to a keypad value after translated from ASCII

val = 10*val + val - '0';

one of the other keys (e.g. '*', '#') indicates when the value is complete

indeed if OP only wants to collect a number that will be enough, no need to store the string and convert it later.

You explicitly compare your value to 100. If you replaced that with a variable who's value is 100, and then wrote your code to modify that variable with the received value, wouldn't that do what you want?

it would but i have only used c++ and ide for this sketch. first time using this software and the language

Never mind. follow along with post #9.

Here is your sketch that is adjusted to contain codes to receive 4-digit decimal data from Keypad The received data is saved in global variable myData. In this sketch, the Keypad data is taken in every 5-sec interval. Tested using UNO, Serial Monitor, and 4x4 Keypad.

//--Entry of * puts the Keypad in data enry mode
//--and then allows to take four digit input: 0000 to 9999
#include <LiquidCrystal_I2C.h>
#include <SharpIR.h>
#include <Keypad.h>
#define IRPin A1
#define model 1080
unsigned long presentMillis;

SharpIR mySensor = SharpIR(IRPin, model);
LiquidCrystal_I2C lcd(0x27, 20, 4);

int distance_cm;

const int FSR_PIN = A0;
const float VCC = 4.98;
const float R_DIV = 3230.0;

int buzzer = 10;

const byte ROWS = 4;
const byte COLS = 4;
char keys[ROWS][COLS] = { {'1', '2', '3', 'A'}, {'4', '5', '6', 'B'}, {'7', '8', '9', 'C'}, {'*', '0', '#', 'D'} };
byte rowPins[ROWS] = {5, 4, 3, 2};
byte colPins[COLS] = {9, 8, 7, 6};
Keypad customKeypad = Keypad (makeKeymap(keys), rowPins, colPins, ROWS, COLS );

bool flag = false;
char keyCode[5] = {0};
int arrayIndex = 0;
int myData;

void setup()
{
  Serial.begin(9600);
  pinMode(FSR_PIN, INPUT);
  lcd.begin();//init();
  lcd.backlight();
  Serial.begin(9600);
  pinMode (buzzer, OUTPUT);
}

void loop()
{
  presentMillis = millis();
  while (millis() - presentMillis < 5000)
  {
    int fsrADC = analogRead(FSR_PIN);
    distance_cm = mySensor.distance();
    //char key = keypad.getKey();

    if (fsrADC != 0)
    {
      float fsrV = fsrADC * VCC / 1023.0;
      float fsrR = R_DIV * (VCC / fsrV - 1.0);
      float force;
      float fsrG = 1.0 / fsrR;
      if (fsrR <= myData)//<= 600)
        force = (fsrG - 0.00075) / 0.00000032639;
      else
        force =  fsrG / 0.000000642857;

      if (force >= 100)
      {
        digitalWrite (buzzer, HIGH);
      }
      else
      {
        noTone(buzzer);
      }

      lcd.setCursor(1, 0);
      lcd.print("Force: " + String(force) + "g");
      lcd.setCursor(0, 1);
      lcd.print("Distance: " + String(distance_cm) + "cm");
      delay(1000);
      lcd.clear();
    }
    else
    {}
  }
  Serial.println("Press * to enter data from Keypad.");
  myData = keyPad();
  Serial.print("Received from Keypad: "); Serial.println(myData, DEC);
}

int keyPad()
{
  Serial.println("Enter 4-digit from Keypad");
  while (true)
  {
    char customKey = customKeypad.getKey();
    if (customKey != 0x00)
    {
      if (flag == false)
      {
        if (customKey == '*')
        {
          flag = true;
        }
      }
      else
      {
        keyCode[arrayIndex] = customKey;
        Serial.print(keyCode[arrayIndex]);
        arrayIndex++;
        if (arrayIndex == 4)
        {
          keyCode[arrayIndex] = '\0'; //insert null-charcater
          Serial.println();
          // myData = atoi(keyCode);
          arrayIndex = 0;
          //Serial.print("You have entered : "); Serial.println(keyCode);
          flag = false;
          return (atoi(keyCode));
        }
      }
    }
  }
}

Output:

Press * to enter data from Keypad.
Enter 4-digit from Keypad
4567
Received from Keypad: 4567

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