I can't store to eeprom when * is pressed - arduino

I have a problem , I am trying to get the key pressed in a keypad and store them in EEPROM and read the EEPROM when a key is pressed. In this case the key is "*"
#include <Keypad.h>
#include<EEPROM.h>
int i=0;
static char liters[4];
const byte rows = 4; //number of the keypad's rows and columns
const byte cols = 4;
char keys [rows] [cols] = { //define the cymbols on the buttons of the keypad
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'}
};
byte rowPins [rows] = {4, 5, 6, 7}; //pins of the keypad
byte colPins [cols] = {8, 9, 10, 11};
Keypad myKeypad = Keypad( makeKeymap(keys), rowPins, colPins, rows, cols);
void setup() {
Serial.begin(9600);
}
void loop() {
if (i<4) {
char key = myKeypad.getKey();
if (key) {
liters[i]=key;
Serial.println(key);
EEPROM.write(i,key);
i++;
// delay(300);
// Serial.print(EEPROM.read(i));
}
}
if (key=='*') {
Serial.print(EEPROM.read(i));
}
When the key * is pressed the keys that were pressed will be stored in the EEPROM. I don't know why the code is not working please help

This
if(key=='*'){
Serial.print(EEPROM.read(i));
}
is in the outer of two scope in wich you have a char key = myKeypad.getKey(); each.
It hence uses the outer key, which uses the first key read from keyboard, while the inner scope uses the second read key.
Whatever problem you observe, it is probably caused by the two read keys being different.

Related

dynamic array declaration in arduino

I am trying to make a code that unlocks a door when a correct passcode is given. But, while doing that, I came across a problem. I had to store the passcode entered in a dynamic array. I know that there is a package for making a dynamic array But I don't know the code for making the dynamic array. I am posting the code I have done till now that is, only starting up the keypad.
#include <Keypad.h>
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] = { 8, 7, 6, 9 };
byte colPins[COLS] = { 5, 4, 3, 2 };
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
}
void loop() {
// put your main code here, to run repeatedly:
}
int* dynamicArray;
dynamicArray = new int[arraySize];
if(dynamicArray == nullptr) {
//if memory could not be assigned
}
//do stuff with your array
delete[] dynamicArray; //delete when not in use anymore

Displaying the input from keypad on a LCD screen using Arduino

I want an Arduino code to give the output in following format on LCD display
If the user click 'A', 1, 2, 3 from the keypad, LCD should display Hi:1,2,3,
This is what I have tried but I cannot figure out a way to build the code as I am a beginner in arduino
#include <Keypad.h>
#include <LiquidCrystal.h>
const byte numRows= 4;
const byte numCols= 4;
char keymap[numRows][numCols]= {
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'}
};
byte rowPins[numRows] = {9,8,7,6}; // Pin Assign
byte colPins[numCols] = {5,4,3,2}; // Pin Assign
Keypad myKeypad= Keypad(makeKeymap(keymap), rowPins, colPins, numRows, numCols);
// LCD
// RS E D4 D5 D6 D7
LiquidCrystal lcd(A0,A1,A2,A3,A4,A5); // Pin Assign
void setup()
{
lcd.begin(16, 2);
lcd.clear();
lcd.print("PUSH ANY KEY! ");
lcd.cursor();
lcd.blink();
}
void loop(){
char keypressed = myKeypad.getKey();
if (keypressed != NO_KEY){
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(keypressed);
lcd.print(':');
lcd.setCursor(3, 0);
}
}
You may need to create a state machine, where the change in the state happens when you receive the 'A' char. So, while you do not receive the 'A' char, your state machine keeps in the busy-wait (while (keypressed != 'A') ).
Once the 'A' is received, you then are going to check the next 3 chars received, and verify if they match what you expect ('1', '2', '3').
If you receive it in any different order, then the loop breaks, and the verification fails at the statement if (i == 4). In other words, as soon as you receive a number that you do not expect, the loop breaks and the verification fails.
Here is what you could add in your loop() code:
void loop() {
char expected[3] = { '1', '2', '3' };
int i = 0;
char keypressed = myKeypad.getKey();
while (keypressed != 'A')
;
while (i < 4) {
for (i = 0; i < 4; i++) {
if (keypressed != expected[i])
break;
}
if (i == 4)
lcd.print("Hi:1,2,3");
}

Arduino Keypad Library on NodeMCU buttons mapped the wrong way

I am attaching a 4x4 membrane keypad to my NodeMCU (ESP8266-12E).
I am using the standard Keypad library for Arduino.
The layout of the keypad look like this:
1 2 3 A
4 5 6 B
7 8 9 C
* 0 # D
Everything works fine except the buttons A and B, which will behave like the buttons 1 and 4, so Pressing A gives me 1, and pressing B gives me 4.
I tried testing the same code on a Arduino Nano, the problem doesn't occur here.
I also tested the Keypad itself to see if it was wired correctly. The keypad is wired correctly.
What is causing the keypad to not work on the buttons A and B?
The code:
#include <Keypad.h>
const byte ROWS = 4; //four rows
const byte COLS = 4; //four columns
char keys[ROWS][COLS] = {
{'1','2','3', 'A'},
{'4','5','6', 'B'},
{'7','8','9', 'C'},
{'*','0','#', 'D'}
};
byte rowPins[ROWS] = {5,4,0,2};
byte colPins[COLS] = {14,12,13,15};
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
byte ledPin = 13;
boolean blink = false;
boolean ledPin_state;
void setup(){
Serial.begin(9600);
pinMode(ledPin, OUTPUT); // Sets the digital pin as output.
digitalWrite(ledPin, HIGH); // Turn the LED on.
ledPin_state = digitalRead(ledPin); // Store initial LED state. HIGH when LED is on.
keypad.addEventListener(keypadEvent); // Add an event listener for this keypad
}
void loop(){
char key = keypad.getKey();
if (key) {
Serial.println(key);
}
if (blink){
digitalWrite(ledPin,!digitalRead(ledPin)); // Change the ledPin from Hi2Lo or Lo2Hi.
delay(100);
}
}
// Taking care of some special events.
void keypadEvent(KeypadEvent key){
switch (keypad.getState()){
case PRESSED:
if (key == '#') {
digitalWrite(ledPin,!digitalRead(ledPin));
ledPin_state = digitalRead(ledPin); // Remember LED state, lit or unlit.
}
break;
case RELEASED:
if (key == '*') {
digitalWrite(ledPin,ledPin_state); // Restore LED state from before it started blinking.
blink = false;
}
break;
case HOLD:
if (key == '*') {
blink = true; // Blink the LED when holding the * key.
}
break;
}
}
The problem here appears to be the use of pin D8.
If you shift all of your connectors to D0 - D7 you will find that this works as expected

Why does the same code from Arduino IDE give different outputs on different laptops?

I was trying to interface a Keypad (4*4 membrane type) via I2C (PCF8574AT) converter module with Arduino Uno.
A1, A2, A3 pin grounded. So the I2CADDR is 0x38.
Compiled and ran this code on my Lenovo T440s and it showed:
Press anykey:
B
And then no matter how many times I press on the keypad nothing will come up on the serial monitor. On the other hand when I run the same on code on T430s it runs successfully.
Showing
Press any key:
and then on pressing key it was showing 1,2 ,3 etc
Arduino version used 1.8.2 on both.
Exact same code and library still different output.
Can anyone explain why?
Here is my code:
/* #file CustomKeypad.pde
|| #version 1.0
|| #author Alexander Brevig
|| #contact alexanderbrevig#gmail.com
||
|| #description
|| | Demonstrates changing the keypad size and key values.
|| #
Use with I2C i/o G. D. (Joe) Young Feb 28/12
*/
#include <Keypad_I2C.h>
#include <Keypad.h> // GDY120705
#include <Wire.h>
#define I2CADDR 0x38
const byte ROWS = 4; //four rows
const byte COLS = 4; //four columns
//define the symbols on the buttons of the keypads
char hexaKeys[ROWS][COLS] = {
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'}
};
byte rowPins[ROWS] = {0, 1, 2, 3}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {4, 5, 6, 7}; //connect to the column pinouts of the keypad
//initialize an instance of class NewKeypad
Keypad_I2C customKeypad( makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS, I2CADDR);
void setup() {
// Wire.begin();
customKeypad.begin(); // GDY120705
Serial.begin(9600);
Serial.println("Press Keys");
}
void loop() {
char customKey = customKeypad.getKey();
if (customKey != NO_KEY) {
Serial.println(customKey);
}
}
Verify your hardware at first.
I tried this issue when I was developing a project with 8*8 key matrix long time ago. It might be somewhere ports change too fast and without discharge thus makes false trigger.

Arduino Keypad simple code doesn't work

/* #file CustomKeypad.pde
|| #version 1.0
|| #author Alexander Brevig
|| #contact alexanderbrevig#gmail.com
||
|| #description
|| | Demonstrates changing the keypad size and key values.
|| #
*/
I don't understand this simple code that is supposly certified doesn't work. I get the message:
keypad:41: error: 'led_pin' was not declared in this scope
else digitalWrite(led_pin,LOW);
#include <Keypad.h>
int led_pin=13;
const byte ROWS = 4; //four rows
const byte COLS = 4; //four columns
//define the cymbols on the buttons of the keypads
char hexaKeys[ROWS][COLS] = {
{'1','2','3','A'},
{'4','5','6','B'},
{'7','8','9','C'},
{'*','0','#','D'}
};
byte rowPins[ROWS] = {9, 8, 7, 6}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {5, 4, 3, 2}; //connect to the column pinouts of the keypad
//initialize an instance of class NewKeypad
Keypad customKeypad = Keypad( makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS);
void setup(){
pinMode(led_pin,OUTPUT);
Serial.begin(9600);
}
void loop(){
char customKey = customKeypad.getKey();
if (customKey){
digitalWrite(led_pin,HIGH);
Serial.println(customKey);
delay(100);
}
else digitalWrite(led_pin,LOW);
}
And if I put an other ledd_pin declaration into the loop, I get the message:
In function 'void loop()':
keypad:35: error: 'customKeypad' was not declared in this scope
char customKey = customKeypad.getKey();
This song strange since everythig must work on that very simple program.
I think you should check the structure of the ' if statement in your loop.
void loop(){
char customKey = customKeypad.getKey();
if (customKey){
digitalWrite(led_pin,HIGH);
Serial.println(customKey);
delay(100);
} else {
digitalWrite(led_pin,LOW);
}
}

Resources