How to use Keypad_MC17 library in Arduino-Ide - arduino

I want to know the button pressed in the Serial-Monitor in Arduino-Ide. I am using Arduino-Uno Board and I have connected the MCP23017 IC via I2C Connection with Arduino-Uno board. I have connected the 5*5 Push-button Matrix with the pins of MCP23017. I am using the joe young/arduino_keypads library.I am not able to view the button pressed in the Serial-Monitor.
#include <Keypad_MC17.h>
#include <Keypad.h>
#include <Wire.h>
#include <Adafruit_MCP23017.h>
#define I2CADDR 0
Adafruit_MCP23017 mcp_1;
const byte ROWS = 5; // five rows
const byte COLS = 5; // five columns
//define the symbols on the buttons of the keypads
char hexaKeys[ROWS][COLS] = {
{'1','2','3','4','5'},
{'6','7','8','9','A'},
{'B','C','D','E','F'},
{'G','H','I','J','K'},
{'L' ,'M','N','O','P'}
};
byte rowPins[ROWS] = { 15 , 14 , 13 , 12 , 11 }; //connect to the row pinouts of the keypad
byte colPins[COLS] = { 10 , 9 ,8 , 7 , 6 }; //connect to the column pinouts of the keypad
//initialize an instance of class NewKeypad
Keypad_MC17 customKeypad( makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS, I2CADDR);
void setup(){
Serial.begin(9600);
Serial.println("Program Started" );
//Begin I2C Devices
Wire.begin();
Wire.beginTransmission(0);
if (Wire.endTransmission(0) == 0) Serial.println("I2C Device Not Found 0x00");
else Serial.println("I2C 0x00 Connected Successfully");
mcp_1.begin();
// Begin Keypad Device
customKeypad.begin( );
}
void loop(){
char customKey = customKeypad.getKey();
if (customKey != NO_KEY) {
Serial.println(customKey);
}
}

Related

Nokia 5110 display does not change on the key event in WeMos D1 with matrix keypad 3x4

I am working on a project with wemos, Nokia 5110 lcd and matric keypad 3x4.
I want to go to another screen on a specific key. The keyEvent() is the function I want to call when I trigger a key. The function is called correctly as it turns the led on when I press 3 on keypad, but the screen does not change.
Here is my code:
#include <SPI.h>
#include <Adafruit_GFX.h>
#include <Adafruit_PCD8544.h>
#include <Keypad.h>
Adafruit_PCD8544 display = Adafruit_PCD8544(D7, D6, D5, D4, D3);
const byte ROWS = 4;
const byte COLS = 3;
char keys[ROWS][COLS] = {
{'1','2','3'},
{'4','5','6'},
{'7','8','9'},
{'*','0','#'}
};
byte rowPins[ROWS] = {D6, D5, D4, D3};
byte colPins[COLS] = {D2, D1, D0};
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
void keyEvent(int page, char key)
{
if(page == 1 && key == '3')
{
digitalWrite(D8, HIGH); //This line works fine. But the below code is not executed.
display.begin();
display.clearDisplay();
display.setCursor(0, 0);
display.println("Line 4");
display.println("Line 5");
display.println("Line 6");
display.display();
}
}
int list = 1;
void setup()
{
pinMode(D8, OUTPUT);
Serial.begin(9600);
}
void loop()
{
char key = keypad.getKey();
if (key)
{
keyEvent(list, key);
}
display.begin();
display.setContrast(100);
display.clearDisplay();
display.setTextSize(0);
display.setTextColor(BLACK);
display.setCursor(0, 0);
display.println("Line 1");
display.println("Line 2");
display.println("Line 3");
display.display();
}

Problems with interrupt arduino nano with NRF24le1

I am trying to build a wireless control of an LED Stripe.
For this I use 2 Arduino Nano's with a NRF24le1 chip to communicate with each other.
Arduino 1 serves as a controller / master and has 3 buttons and 1 fader for the choice of the respective light mode / blackout, as well as the to serve light intensity.
I would like the values ​​as 6 bytes (button1, button2, button3,
faderintensiät) and received on the 2nd Arduino.
Not constant values ​​are sent, only at change of values.
On the 2nd Nano should then be activated by interrupt flags, if an button press what and the current value from Fader Held so that the loop stays clear and there via the RGBW Led Stripe, ever after which flag is selected, can be iterated.
How can I attach the interrupt? On my Transmitter Adruino the stream is running
This is my code:
#include <Adafruit_NeoPixel.h>
#ifdef __AVR__
#include <avr/power.h> // Required for 16 MHz Adafruit Trinket
#endif
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
#define LED_PIN 3
#define LED_COUNT 228
uint8_t BRIGHTNESS = 60;
// Declare our NeoPixel strip object:
Adafruit_NeoPixel strip(LED_COUNT, LED_PIN, NEO_GRBW + NEO_KHZ800);
RF24 wirelessSPI(7, 8); // CE, CSN
#define PinIRQ 3 // Arduino Uno, Mega und Nano: Pin 3
#define IRQ 1
const byte address[6] = "00001";
boolean light_on = false;
boolean mode_a_active = false;
boolean mode_b_active = false;
boolean receivedMessage = false;
int interruptcounter = 0;
uint8_t settings[6] = {0, 0, 0, 0, 0, 0}; //Saving the incoming data
void setup() {
pinMode(6, OUTPUT);
Serial.begin(9600);
wirelessSPI.begin();
//wirelessSPI.setAutoAck(1); //new
//wirelessSPI.enableAckPayload(); //new
//wirelessSPI.maskIRQ(1,1,0); //new
wirelessSPI.setPALevel(RF24_PA_MIN); //You can set this as minimum or maximum depending on the distance between the transmitter and receiver.
wirelessSPI.openReadingPipe(0, address); //Setting the address at which we will receive the data
wirelessSPI.startListening(); //This sets the module as receiver
strip.begin(); // INITIALIZE NeoPixel strip object (REQUIRED)
strip.show(); // Turn OFF all pixels ASAP
strip.setBrightness(50); // Set BRIGHTNESS to about 1/5 (max = 255)
attachInterrupt(IRQ, incomingmessage, HIGH);
}
void loop()
{
wirelessSPI.read(&settings, sizeof(settings)); //Reading the data
if(receivedMessage == true){
Serial.println("messange on interupt received");
Serial.print("interruptcounter counts: ");
Serial.println(interruptcounter);
Serial.println();
Serial.print(settings[0]);
Serial.print(settings[1]);
Serial.print(settings[2]);
Serial.print(settings[3]);
Serial.print(settings[4]);
Serial.println(settings[5]);
receivedMessage = false;
}
}
void incomingmessage()
{
interruptcounter++;
while (wirelessSPI.available()) {
wirelessSPI.read(&settings, sizeof(settings)); //Reading the data
receivedMessage = true; // Variable setzen, dass eine neue Nachricht zur Auswertung bereit steht
}
}

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

Arduino with two RC522

To pimp up my Carrera I'm going to build a round counter.
It contains an Arduino Nano, a lcd and 2 rc522 rfid-reader.
The readers share the pins for scd, miso, mosi and have own pins for sda and rst.
Actually I'm not able to get the two readers work together at the same time. Only if the one or the other reader is physically plugged (hardcore!) into the breadbord it works (without code changing). But not together.
It has to be an issue with my code, but where?
(The RFID-Communication ist inpired by the example from [addicore][1]http://www.addicore.com/v/vspfiles/downloadables/Product%20Downloadables/RFID_RC522/RFIDQuickStartGuide.pdf)
Is there anyone who has a hint for me?
#include <AddicoreRFID.h>
#include <SPI.h>
#include <LiquidCrystal.h>
#define uchar unsigned char
#define uint unsigned int
// create AddicoreRFID object to control the RFID module
/////////////////////////////////////////////////////////////////////
//set the pins
/////////////////////////////////////////////////////////////////////
//2 - SCK Digital 13
//3 - MOSI Digital 11
//4 - MISO Digital 12
const int SS1 = 8; //RFID1
const int RST1 = 9;
AddicoreRFID myRFID1 (SS1, RST1);
const int SS2 = 10; //RFID2
const int RST2 = A0;
AddicoreRFID myRFID2 (SS2, RST2);
//Maximum length of the array
#define MAX_LEN 16
//LCD init
// * LCD RS pin to digital pin 7
// * LCD Enable pin to digital pin 6
// * LCD D4 pin to digital pin 5
// * LCD D5 pin to digital pin 4
// * LCD D6 pin to digital pin 3
// * LCD D7 pin to digital pin 2
LiquidCrystal lcd1(7, 6, 5, 4, 3, 2);
//Counter
int a = 0;
int b = 0;
void setup() {
Serial.begin(9600);
//LCD init
init_lcd1();
myRFID1.AddicoreRFID_Init();
myRFID2.AddicoreRFID_Init();
}
void loop() {
uchar i, tmp, checksum1;
uchar status;
uchar str1[MAX_LEN];
uchar str2[MAX_LEN];
///////////// RFID1 ///////////////////
// 0x4400 = Mifare_UltraLight -Tag Type
str1[1] = 0x4400;
//Find tags, return tag type
// Manipuliert str1(!);
//AddicoreRFID::AddicoreRFID_Request(byte reqMode, byte *TagType)
status = myRFID1.AddicoreRFID_Request(PICC_REQIDL, str1);
if (status == MI_OK) {
serial_TagDetect(str1, 1);
}
//Anti-collision, return tag serial number 4 bytes
// Manipuliert str1(!);
status = myRFID1.AddicoreRFID_Anticoll(str1);
if (status == MI_OK) {
serial_TagData(str1);
lcd_counter(str1, lcd1);
//delay(500);
}
myRFID1.AddicoreRFID_Halt(); //Command tag into hibernation
///////////// RFID2 ///////////////////
str2[1] = 0x4400;
//Find tags, return tag type
status = myRFID2.AddicoreRFID_Request(PICC_REQIDL, str2);
if (status == MI_OK) {
serial_TagDetect(str2, 2);
}
//Anti-collision, return tag serial number 4 bytes
status = myRFID2.AddicoreRFID_Anticoll(str1);
if (status == MI_OK) {
serial_TagData(str2);
lcd_counter(str2, lcd1);
// liest sonst nonstop die Tags!
//delay(500);
}
myRFID2.AddicoreRFID_Halt(); //Command tag into hibernation
}
void init_lcd1() {
... inits the lcd ...
}
// Zählt das Auftreten der Tags
void lcd_counter (uchar *str, LiquidCrystal lcd ) {
... output to the lcd ....
}
// Meldet gefundenen Tag auf der Konsole
void serial_TagDetect(uchar *str, int reader) {
if (reader == 1) {
Serial.print("RFID1 tag detected: ");
} else {
Serial.print("RFID2 tag detected: ");
}
Serial.print(str[0], BIN);
Serial.print(" , ");
Serial.print(str[1], BIN); Serial.println(" ");
}
// gibt Tagdaten auf der Konsole aus
void serial_TagData(uchar *str) {
uchar checksum1 = str[0] ^ str[1] ^ str[2] ^ str[3];
Serial.print("The tag's number is: ");
//Serial.print(2);
Serial.print(str[0]);
Serial.print(" , ");
Serial.print(str[1], BIN);
Serial.print(" , ");
Serial.print(str[2], BIN);
Serial.print(" , ");
Serial.print(str[3], BIN);
Serial.print(" , ");
Serial.print(str[4], BIN);
Serial.print(" , ");
Serial.println(checksum1, BIN);
}
I wonder about that the same(!!) rfid tag has different values depending it gets read from RFID1 or RFID2:
RFID1 tag detected: 1000100 , 0
The tag's number is: 136 , 100 , 11100 , 1101011 , 11111011 , 11111011
RFID2 tag detected: 1000100 , 0
The tag's number is: 68 , 0 , 0 , 0 , 0 , 1000100
I use 10 RC522 together at the same time with Ardunio mega .
Try this example for 2 ncf reader, its work for me. I tested in Arduino Nano.
The readers share the pins for 3,3V, GND. SCD, MISO, MOSI, RST and have own pins for SDA. I don't use the IRQ pin.
#include <SPI.h>
#include <MFRC522.h>
#define RST_PIN 9 // Configurable, see typical pin layout above
#define SS1_PIN 10 // Configurable, see typical pin layout above
#define SS2_PIN 8 // Configurable, see typical pin layout above
MFRC522 mfrc522_1(SS1_PIN, RST_PIN); // Create MFRC522 instance
MFRC522 mfrc522_2(SS2_PIN, RST_PIN);
void setup() {
Serial.begin(9600); // Initialize serial communications with the PC
while (!Serial); // Do nothing if no serial port is opened (added for Arduinos based on ATMEGA32U4)
SPI.begin(); // Init SPI bus
mfrc522_1.PCD_Init(); // Init MFRC522
mfrc522_1.PCD_DumpVersionToSerial(); // Show details of PCD - MFRC522 Card Reader details
Serial.println(F("Scan PICC to see UID, SAK, type, and data blocks..."));
mfrc522_2.PCD_Init(); // Init MFRC522
mfrc522_2.PCD_DumpVersionToSerial(); // Show details of PCD - MFRC522 Card Reader details
}
void loop() {
// Look for new cards
if ( mfrc522_1.PICC_IsNewCardPresent() || mfrc522_2.PICC_IsNewCardPresent()) {
Serial.println(F("New card..."));
} else {
return;
}
// Select one of the cards
if ( ! mfrc522_1.PICC_ReadCardSerial() && ! mfrc522_2.PICC_ReadCardSerial()) {
Serial.println(F("Read..."));
return;
}
// Dump debug info about the card; PICC_HaltA() is automatically called
mfrc522_1.PICC_DumpToSerial(&(mfrc522_1.uid));
mfrc522_2.PICC_DumpToSerial(&(mfrc522_2.uid));
}
//Anti-collision, return tag serial number 4 bytes
status = myRFID2.AddicoreRFID_Anticoll(str1); // <<<< needs to be strl2

Arduino state not declared in this scope

I am currently working on a small project using arduino and the below is my code:
#include <SoftwareSerial.h>
#include <Adafruit_Fingerprint.h>
#include <Keypad.h>
const byte ROWS = 4; // Four rows
const byte COLS = 3; // Three columns
// Define the Keymap
char keys[ROWS][COLS] = {
{'1','2','3'},
{'4','5','6'},
{'7','8','9'},
{'#','0','*'}
};
// Connect keypad ROW0, ROW1, ROW2 and ROW3 to these Arduino pins.
byte rowPins[ROWS] = {2, 3, 4, 5};
// Connect keypad COL0, COL1 and COL2 to these Arduino pins.
byte colPins[COLS] = {6, 7, 8};
// Create the Keypad
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
#define ledpin 13
#define LOCKED 2
#define PASSWORD_OK 1
#define UNLOCKED 0
//State Variables; initialise to locked state
int LockState = LOCKED;
int position = 0;
void setup()
{
pinMode(ledpin,OUTPUT);
digitalWrite(ledpin, HIGH);
Serial.begin(9600);
//Initialize state and communications
setLockState(LOCKED);
Serial.begin(9600);
}
void loop()
{
char key = keypad.getKey();
if(key) // Check for a valid key.
{
switch (key)
{
// case '*':
// digitalWrite(ledpin, LOW);
// break;
// case '#':
// digitalWrite(ledpin, HIGH);
// break;
default:
Serial.print(key);
}
}
}
It was from Adafruit. however, when i upload to my uno, it keeps giving me the error: 'setLockState' was not declared in this scope.
Can anyone shed some light on this?
I think you did not declare the function setLockState() in your code. I don't know about your project details, but it looks like you want to lock a thing via keypad or fingerprint. You can find a great Tutorial (including the missing setLockState() function) for a biometric-security-box on the website of Adafruit.

Resources