GY-63_MS5611/libraries/I2CKeyPad/examples/I2Ckeypad_demo03/I2Ckeypad_demo03.ino

113 lines
2.1 KiB
Arduino
Raw Normal View History

2021-01-29 06:31:58 -05:00
//
// FILE: I2CKeyPad_demo03.ino
// AUTHOR: Rob Tillaart
// PURPOSE: demo reading a larger value from the keyPad
// URL: https://github.com/RobTillaart/I2CKeyPad
//
// PCF8574
// pin p0-p3 rows
2021-11-08 07:13:29 -05:00
// pin p4-p7 columns
2021-01-29 06:31:58 -05:00
// 4x4 or smaller keypad.
2021-05-08 03:52:18 -04:00
2021-01-29 06:31:58 -05:00
#include "Wire.h"
#include "I2CKeyPad.h"
const uint8_t KEYPAD_ADDRESS = 0x38;
2021-05-08 03:52:18 -04:00
I2CKeyPad keyPad(KEYPAD_ADDRESS);
2021-01-29 06:31:58 -05:00
uint32_t start, stop;
uint32_t lastKeyPressed = 0;
uint32_t value = 0;
2021-12-19 14:23:20 -05:00
2021-01-29 06:31:58 -05:00
void setup()
{
Serial.begin(115200);
Serial.println(__FILE__);
Wire.begin();
Wire.setClock(400000);
2021-05-08 03:52:18 -04:00
if (keyPad.begin() == false)
2021-01-29 06:31:58 -05:00
{
Serial.println("\nERROR: cannot communicate to keypad.\nPlease reboot.\n");
while(1);
}
}
void loop()
{
uint32_t now = millis();
if (now - lastKeyPressed >= 100)
{
lastKeyPressed = now;
start = micros();
char c = handleKeyPadValue(value);
if (value > 125000) value = 125000; // some sample max.
stop = micros();
Serial.print(millis());
Serial.print("\t");
Serial.print(value);
Serial.print("\t");
Serial.print((char) c);
Serial.print("\t");
Serial.println(stop - start);
}
}
// handleKeyPadValue is used to read a uint32_t from the keypad
// every digit must be pressed separately
// the last key pressed is also returned.
//
// 0..9 adds to the number
// * means remove last digit
// # resets to zero
// ABCD are not mapped.
char handleKeyPadValue(uint32_t &value)
{
2021-11-08 07:13:29 -05:00
char v[19] = "123A456B789C*0#DNF"; // N = NoKey, F = Fail
2021-01-29 06:31:58 -05:00
static uint8_t lastKey = 0;
uint8_t idx = keyPad.getKey();
char c = v[idx];
if (lastKey != c)
{
lastKey = c;
switch (c)
{
case '0' ... '9':
value *= 10;
value += c - '0';
break;
case '*':
if (value > 0) value /= 10;
break;
case '#':
value = 0;
break;
case 'A' ... 'D':
// e.g. store value in EEPROM
break;
case 'F':
Serial.println("FAIL");
break;
case 'N':
Serial.println("NOKEY");
break;
default:
break;
}
}
return c;
}
2021-11-08 07:13:29 -05:00
// -- END OF FILE --