I'm using the arduino's 4x4 keypad to replace the keyboard on my Laptop, but I don't know where should I insert the code, should I insert in loop or send? and can I hold the password until I pressed # then send?
// TX
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
#include <Key.h>
#include <Keypad.h>
#define CE_PIN 7
#define CSN_PIN 8
const byte slaveAddress[5] = {'R','x','A','A','A'}; //slaveaddress
const byte masterAddress[5] = {'T','X','a','a','a'}; //masteraddress
RF24 radio(CE_PIN, CSN_PIN); // Create a Radio
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]={5,4,3,2};
byte colPins[COLS]={14,15,16,17};
char dataReceived[15]; // to hold the data from the slave - must match replyData[] in the slave
bool newData = false;
char customKey=0;
unsigned long currentMillis;
unsigned long prevMillis;
unsigned long txIntervalMillis = 500; // 每半秒讀取一次
char cmd[15];
char password[4];
char test[2]={'O','K'};
Keypad customKeypad = Keypad(makeKeymap(keys),rowPins,colPins,ROWS,COLS);
char customKey;
char a[5];
//============
void setup() {
Serial.begin(115200);
Serial.println("MasterSwapRoles Starting");
radio.begin();
radio.setDataRate( RF24_250KBPS );
radio.openWritingPipe(slaveAddress);
radio.openReadingPipe(1, masterAddress);
radio.setRetries(3,5); // delay, count
send(); // to get things started
prevMillis = millis(); // set clock
}
//=============
void loop() {
currentMillis = millis();
if (currentMillis - prevMillis >= txIntervalMillis) {
send();
prevMillis = millis();
}
getData();
showData();
showDatafailed();
}
//====================
void send() {
radio.stopListening();
bool rslt;
if(Serial.available())
{
Serial.readBytes(cmd,100);
Serial.println(atol(cmd));
rslt = radio.write( &cmd, sizeof(cmd) );
radio.startListening();
Serial.println("Data Sent");
if (rslt) {
Serial.println("Received...OK");
}
else {
Serial.println("Received...failed");
}
}
}
//================
void getData() {
int a=6;
int i=0;
int j=0;
if ( radio.available() ) {
radio.read( &dataReceived, sizeof(dataReceived) );
newData = true;
for(i=0;i<a;i++)
{
if(dataReceived[i] == test[i])
{
showData();
}
else
{
showDatafailed();
}
}
}
}
//================
void showData() {
if (newData == true) {
Serial.print("Data received...");
Serial.print(dataReceived);
Serial.println("\n");
newData = false;
}
}
//================
void showDatafailed() {
if (newData == true) {
Serial.print("Data received...");
Serial.print(dataReceived);
Serial.println("\n");
newData = false;
}
}
// RX
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
#define CE_PIN 7
#define CSN_PIN 8
const byte slaveAddress[5] = {'R','x','A','A','A'};
const byte masterAddress[5] = {'T','X','a','a','a'};
char pass[4]={'7','4','1','4'};
RF24 radio(CE_PIN, CSN_PIN); // Create a Radio
char dataReceived[15]; // must match dataToSend in master
char replyData[2]= {'O','K'}; // the two values to be sent to the master
char replyDatafalse[6]={'f','a','i','l','e','d'};
int newData = 0;
unsigned long currentMillis;
unsigned long prevMillis;
unsigned long txIntervalMillis = 500; // 每半秒讀取一次
void setup() {
Serial.begin(115200);
Serial.println("SlaveSwapRoles Starting");
radio.begin();
radio.setDataRate( RF24_250KBPS );
radio.openWritingPipe(masterAddress); // NB these are swapped compared to the master
radio.openReadingPipe(1, slaveAddress);
radio.setRetries(3,5); // delay, count
radio.startListening();
}
//====================
void loop() {
getData();
showData();
send();
}
//====================
void send() {
if (newData == 1) {
radio.stopListening();
bool rslt;
rslt = radio.write( &replyData, sizeof(replyData) );
radio.startListening();
Serial.print("Reply Sent: ");
Serial.print("OK \n");
if (rslt) {
Serial.println("Received...OK\n");
}
else {
Serial.println("Received...failed\n");
}
Serial.println();
newData = 0;
}
else if(newData==2)
{
radio.stopListening();
bool rslt;
rslt = radio.write( &replyDatafalse, sizeof(replyDatafalse) );
radio.startListening();
Serial.print("Reply Sent: ");
Serial.print("failed \n");
if (rslt) {
Serial.println("Received...OK\n");
}
else {
Serial.println("Received...failed\n");
}
Serial.println();
newData=0;
}
}
//================
void getData() {
int a=4;
int i=0;
int j=0;
int k=0,z=0;
if ( radio.available() ) {
radio.read( &dataReceived, sizeof(dataReceived) );
for(i=0;i<a;i++)
{
if(dataReceived[i]== pass[i])
{
k++;
}
else
{
z++;
}
}
if(k==a)
{
newData=1;
}
else
{
newData=2;
}
}
}
//================
void showData() {
if (newData == 1) {
Serial.print("Data received...OK\n");
}
else if(newData == 2)
{
Serial.print("Data received...OK\n");
}
}
//================