Here is a demo for entering a multi digit float value via a 4x4 keypad attached to and Uno and sending the number as a string with a rf24 radio module to another Uno with a rf24 radio attached. I use the Keypad library and code from Robin2's excellent simple rf24 tutorial. The Keypad library is available via the IDE library manager.
Keypad and sender:
// enter multi digit number via keypad and send
// as a string with rf24 radio
// by c goulding aka groundfungus
#include <Keypad.h>
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.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'}
};
byte rowPins[ROWS] = {2, 3, 4, 5}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {6, 7, 8, A0}; //connect to the column pinouts of the keypad
const byte CE_PIN = 9;
const byte CSN_PIN = 10;
const byte slaveAddress[5] = {'R', 'x', 'A', 'A', 'A'};
char dataToSend[25];
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
RF24 radio(CE_PIN, CSN_PIN); // Create a Radio
char keyBuffer[25];
bool newEntry = false;
void setup()
{
Serial.begin(115200);
Serial.println("Enter number from keypad");
Serial.println("# will enter and send the number");
Serial.println("keypad D = delete, * = .(DP), # = enter");
radio.begin();
//RF24_1MBPS, RF24_2MBPS, RF24_250KBPS
radio.setDataRate( RF24_250KBPS );
radio.setRetries(3, 5); // delay, count
//radio.setChannel(76); // 76 is library default
//RF24_PA_MIN = 0,RF24_PA_LOW, RF24_PA_HIGH, RF24_PA_MAX
radio.setPALevel(RF24_PA_HIGH);
radio.openWritingPipe(slaveAddress);
}
void loop()
{
char key = keypad.getKey();
if (key)
{
Serial.println(key);
getEntry(key);
}
if (newEntry == true)
{
Serial.print("new number entered = ");
Serial.println(keyBuffer);
strncpy(dataToSend, keyBuffer, 22);
send();
newEntry = false;
}
}
void send()
{
bool rslt;
rslt = radio.write( &dataToSend, sizeof(dataToSend) );
// Always use sizeof() as it gives the size as the number of bytes.
// For example if dataToSend was an int sizeof() would correctly return 2
Serial.print("Data Sent ");
Serial.print(dataToSend);
if (rslt)
{
Serial.println(" Acknowledge received");
}
else
{
Serial.println(" Tx failed");
}
}
void getEntry(char key)
{
static boolean entryStarted = false;
static byte keyBufferIndex = 0;
if (key == 'D') // delete last key entry (backspace)
{
if (keyBufferIndex > 0)
{
keyBufferIndex --;
//Serial.println("deleted");
return;
}
}
// start receiving a new number
if (entryStarted == false)
{
keyBufferIndex = 0;
entryStarted = true;
keyBuffer[keyBufferIndex] = key;
//Serial.println(keyBuffer);
newEntry = false;
keyBufferIndex++;
}
// add new key to string and increment index
else if (entryStarted == true && key != '#')
{
keyBuffer[keyBufferIndex] = key;
//Serial.println(keyBuffer);
keyBufferIndex++;
}
// number complete clean up and exit
else if (key == '#')
{
keyBuffer[keyBufferIndex] = '\0';
entryStarted = false;
newEntry = true;
//Serial.println(keyBuffer);
}
}
float entryToFloat(char* entry)
{
return (atof(entry));
}
int entryToInt(char* entry)
{
return (atoi(entry));
}
Receiver:
// receive multi digit numer as a string from keypad sender
// by c goulding aka groundfungus
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
const byte CE_PIN = 9;
const byte CSN_PIN = 10;
const byte thisSlaveAddress[5] = {'R', 'x', 'A', 'A', 'A'};
RF24 radio(CE_PIN, CSN_PIN);
char recvData[25];
bool newData = false;
//===========
void setup()
{
Serial.begin(115200);
Serial.println("Receive number from wireless keypad");
radio.begin();
radio.setDataRate( RF24_250KBPS );
radio.setRetries(3, 5); // delay, count
radio.setPALevel(RF24_PA_LOW);
radio.openReadingPipe(1, thisSlaveAddress);
radio.startListening();
}
//=============
void loop()
{
getData();
showData();
}
//==============
void getData()
{
if ( radio.available() )
{
radio.read( &recvData, sizeof(recvData) );
newData = true;
}
}
void showData()
{
if (newData == true)
{
Serial.print("Data received >> ");
Serial.print("number = ");
Serial.println(recvData); // string
// use atof() to convert to a float number
newData = false;
}
}