I am using the following code to send a char array of six digits over a wireless link. When it is received at the other end I need to convert it back to a number. My problem is that the array which is 199324 is converted by atoi to 2716.
Obviously I am doing something wrong and would appreciate any help.
TX code
/*
* Arduino Wireless Communication Tutorial
* Example 1 - Transmitter Code
*
* by Dejan Nedelkovski, www.HowToMechatronics.com
*
* Library: TMRh20/RF24, https://github.com/tmrh20/RF24/
*/
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
RF24 radio(7, 8); // CE, CSN
const byte address[6] = "00001";
void setup() {
radio.begin();
radio.openWritingPipe(address);
radio.setPALevel(RF24_PA_MIN);
radio.stopListening();
Serial.begin(9600);
}
void loop() {
// put your main code here, to run repeatedly:
int runs = 199;
int wkts = 3;
int overs = 24;
char cruns [4];
sprintf(cruns, "%03d", runs);
char cwkts [2];
sprintf(cwkts, "%01d", wkts);
char covers [3];
sprintf(covers, "%02d", overs);
Serial.println(cruns);
Serial.println(cwkts);
Serial.println(covers);
String StringRuns = String(cruns);
String StringWkts = String(cwkts);
String StringOvers = String(covers);
String Output = String (StringRuns+StringWkts+StringOvers);
Serial.print ("The runs are ");
Serial.println(StringRuns);
Serial.print ("The wkts are ");
Serial.println(StringWkts);
Serial.print ("The overs are ");
Serial.println(StringOvers);
Serial.println(Output);
char charBuf [Output.length()+1] ; // charBuf is the array we send by radio
Output.toCharArray(charBuf, Output.length()+1);
radio.write(&charBuf, sizeof(charBuf));
delay(1000);
}
RX Code
/*
Arduino Wireless Communication Tutorial
Example 1 - Receiver Code
by Dejan Nedelkovski, www.HowToMechatronics.com
Library: TMRh20/RF24, https://github.com/tmrh20/RF24/
*/
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
RF24 radio(7, 8); // CE, CSN
const byte address[6] = "00001";
void setup() {
Serial.begin(9600);
radio.begin();
radio.openReadingPipe(0, address);
radio.setPALevel(RF24_PA_MIN);
radio.startListening();
}
void loop() {
if (radio.available()) {
char text[7] = "";
radio.read(&text, sizeof(text));
int digits = atoi(text);
Serial.print ("array = ");
Serial.println (text);
Serial.print ("integer = ");
Serial.println (digits);
delay (1000);
}
}