Hello i have 2x ardiono Mega 2560 with LCD and keypad connected using NRF24L01 module. It is working only sometimes so i dont know where is problem in code or in hardware? Please look at my code and tell me if it is functional. Thanks for help.
//klávesnice
#include <Keypad.h>
const byte ROWS = 4; //four rows
const byte COLS = 4; //three columns
char keys[ROWS][COLS] = {
{
'1','2','3','A' }
,
{
'4','5','6','B' }
,
{
'7','8','9','C' }
,
{
'*','0','#','D' }
};
char rec;
byte rowPins[ROWS] = {
34, 36, 38, 40}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {
42, 44, 46, 48};//connect to the column pinouts of the keypad
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
//RF
#include <SPI.h>
#include <Mirf.h>
#include <nRF24L01.h>
#include <MirfHardwareSpiDriver.h>
//LCD
#include <Wire.h>
#include <LCD.h>
#include <LiquidCrystal_I2C.h>
#define I2C_ADDR 0x20
#define BACKLIGHT_PIN 7
#define En_pin 4
#define Rw_pin 5
#define Rs_pin 6
#define D4_pin 0
#define D5_pin 1
#define D6_pin 2
#define D7_pin 3
#define LED_OFF 0
#define LED_ON 1
//String count;
LiquidCrystal_I2C lcd(I2C_ADDR,En_pin,Rw_pin,Rs_pin,D4_pin,D5_pin,D6_pin,D7_pin);
void setup(void)
{
Serial.begin(9600);
//LCD
{
lcd.begin (16,2);
lcd.setBacklightPin(BACKLIGHT_PIN,NEGATIVE);
lcd.setBacklight(LED_ON);
lcd.backlight();
}
Mirf.cePin = 9;
Mirf.csnPin = 10;
Mirf.spi = &MirfHardwareSpi;
Mirf.init();
String rec = setAddress("Receiving address");
String tar = setAddress("Target address");
Mirf.setRADDR((byte *)&rec);
Mirf.setTADDR((byte *)&tar);
//Mirf.setRADDR((byte *)"00002");
//Mirf.setTADDR((byte *)"00001");
Mirf.payload = sizeof(char);
Mirf.config();
lcd.setCursor(0,0);
lcd.print("Send:");
lcd.setCursor(0,1);
lcd.print("Rec:");
}
void loop(void)
{
char key = keypad.getKey();
if (key != NO_KEY){
Mirf.send((byte *) &key);
lcd.setCursor(5,0);
lcd.print(key);
while(Mirf.isSending()){
}
}
if(Mirf.dataReady()){
char get;
lcd.setCursor(5,1);
Mirf.getData((byte *) &get);
lcd.print(get);
}
}
String setAddress(String rec){
int pos = 0;
String add = "";
lcd.setCursor(0,0);
lcd.print(rec);
char key = keypad.getKey();
char keyOld;
while(pos<5){
if (key != NO_KEY){
lcd.setCursor(pos,1);
pos++;
add +=key;
lcd.print(key);
}
key = keypad.getKey();
}
while(true){
if(key == 'D'){
lcd.clear();
return add;
}
if(key == 'C'){
lcd.clear();
return setAddress(rec);
}
key = keypad.getKey();
}
}