/* STM32 Blue Pill project using the STM32 Arduino Core (stm32duino) */
#include <I2CKeyPad.h>
#include <LiquidCrystal_I2C.h>
#define deltaTime(val) (millis() - val)
I2CKeyPad keyPad(0x3F);
char keypad_layout[19] = "123A456B789C*0#D"; // N = NO_KEY, F = FAILED
LiquidCrystal_I2C lcd(0x27, 16, 2); // Change 0x27 to the correct I2C address for your LCD
// Just to avoid spamming
unsigned long lastKeyPress = 0;
unsigned long keyDelay = 200;
int Wire_Scanner(int Except)
{
int I2C_Address = 0;
for (I2C_Address = 0; I2C_Address < 128; I2C_Address++)
{
if(I2C_Address != Except)
{
Wire.beginTransmission(I2C_Address);
if (Wire.endTransmission() == 0)
{
// Serial1.print("\r\n ==== WIRE I2C_Address : "); Serial1.print(I2C_Address, HEX);
delay(1);
break;
}
}
}
return I2C_Address;
}
void setup() {
Serial.begin(4800);
pinMode(PA8, OUTPUT);
digitalWrite(PA8, 1);
Serial.print("\n ======= System Started =======\n");
int First_Value = Wire_Scanner(0);
Serial1.print("\n ==== First_Value : ");Serial1.print(First_Value , HEX);
int Second_Value = Wire_Scanner(First_Value);
Serial1.print("\n ==== Second_Value : ");Serial1.print(Second_Value , HEX);
lcd.init();
lcd.backlight(); // You can remove this line if your LCD doesn't have backlight control
lcd.setCursor(0, 0);
lcd.print("Press a key:");
Serial.print("\n ======= Press a key: =======\n");
if (!keyPad.begin())
{
Serial.print("Cannot connect to I2C keypad.\n");
}
Serial.print("=== Before keyPad.loadKeyMap .\n");
keyPad.loadKeyMap(keypad_layout);
Serial.print("=== After keyPad.loadKeyMap .\n");
}
void loop()
{
if (keyPad.isPressed() && deltaTime(lastKeyPress) > keyDelay) {
lastKeyPress = millis();
char ch = keyPad.getChar();
Serial.print("\n ==> Pressed: ");
Serial.print(ch , HEX);
lcd.setCursor(0, 1);
lcd.print("Pressed: ");
lcd.print(ch , HEX);
}
// Makes the code run faster
delay(10);
}
Loading
stm32-bluepill
stm32-bluepill