Think I’m out of my programming depth with this one:
Trying to send a command to an Arduino Uno through the serial and then have it transmitted using an nRF24L01 transmitter to another Arduino. Sending the command to the first Arduino works just fine; transmitting a written string (i.e. transmit(“Hello World!~”) works fine. It’s when I try to send the string as a variable (i.e transmit(inputString)
that things breakdown.
#include <SPI.h>
#include <Mirf.h>
#include <nRF24L01.h>
#include <MirfHardwareSpiDriver.h>
String inputString = ""; // a string to hold incoming data
boolean stringComplete = false; // whether the string is complete
boolean isCommand = false; //whether a command has been recieved
void setup() {
// initialize serial:
Serial.begin(9600);
Serial.print("Initializing . . . ");
// reserve 200 bytes for the inputString:
inputString.reserve(200);
Mirf.cePin = 8;
Mirf.csnPin = 7;
Mirf.spi = &MirfHardwareSpi;
Mirf.init();
Mirf.setRADDR((byte *)"ctrl1");
Mirf.setTADDR((byte *)"clnt1");
/*
* Set the payload length to sizeof(unsigned long) the
* return type of millis().
*
* NB: payload on client and server must be the same.
*/
Mirf.payload = 1;
/*
* Write channel and payload config then power up reciver.
*/
Mirf.config();
Serial.println("Control Initialized");
}
void loop() {
// print the string when a newline arrives:
if (stringComplete) {
Serial.println(inputString);
if (inputString.indexOf(':') != -1) {
isCommand = true;
inputString += "~";
Serial.print("Transmitting: ");Serial.print(inputString);Serial.print(" as ");
Serial.println((const char *) &inputString); //Prints a notice that a message is being transmitted
transmit((const char *) &inputString); //and what is being transmitted
delay(1000);
} else {
isCommand = false;
Serial.println("Invalid Command");
}
inputString = "";
stringComplete = false;
}
}
/*
SerialEvent occurs whenever a new data comes in the
hardware serial RX. This routine is run between each
time loop() runs, so using delay inside loop can delay
response. Multiple bytes of data may be available.
*/
void serialEvent() {
while (Serial.available()) {
// get the new byte:
char inChar = (char)Serial.read();
// add it to the inputString:
//inputString += inChar;
// if the incoming character is a newline, set a flag
// so the main loop can do something about it:
if (inChar == '\n') {
stringComplete = true;
} else {
inputString += inChar;
}
}
}
void transmit( float v)
{
byte c;
char buf[10];
dtostrf(v,9,3,buf);
for( int i=0 ; i = 8 ; i++ )
{
c = buf[i];
Mirf.send(&c);
while( Mirf.isSending() ) ;
//delay(100);
}
}
// sends a string via the nRF24L01
void transmit(const char *string)
{
byte c;
for( int i=0 ; string[i]!=0x00 ; i++ )
{
c = string[i];
Serial.println(c); //Serial print the char for debugging
Mirf.send(&c);
while( Mirf.isSending() ) ;
}
}
// send a CR/LF sequence via the nRF24L01
void transmitlf(void)
{
byte c;
c = '\r';
Mirf.send(&c);
while( Mirf.isSending() ) ;
c = '\n';
Mirf.send(&c);
while( Mirf.isSending() ) ;
}
There’s some debugging code (some serial prints) in there, but it looks like the transmit function is not looking at the proper memory location. I get the following:
Initializing . . . Control Initialized
Command:test
Transmitting: Command:test~ as HÈ
72
2
200
The ~ is a terminator symbol that tells the receiver the transmission is complete.
Any help other than RTFM (tried; hasn’t helped) would be appreciated.