Help with pointers

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!~”):wink: works fine. It’s when I try to send the string as a variable (i.e transmit(inputString):wink: 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.

boolean isCommand = false;  //whether a command has been recieved

The name isCommand implies that something is, or is not, a command. The name gorCommand implies something different. Which do you really want to be using?

      Serial.println((const char *) &inputString);

When you want to cast something, what you cast has to ACTUALLY be interpretable as the cast type. A String can NOT be cast to a char pointer, any more than a dog can be cast as a Packard.

Why ARE you doing this cast?

You need to extract the string from the String, using String::toCharArray(). Better still would be to shitcan the String class altogether.

. . . and the detailed reply I wrote was just lost >:( . . .

Long story short:

isCommand is there as a flag if the string is a valid command to keep junk from being transmitted. I'll add more to it later.

I used a String because that's what was being used in the SerialEvent example upon which I built.

The mirf code I was borrowing had the transmit function sending a pointer char of the first element of a string and then working from that which is why I was trying to recast the string since transmitting the string itself didn't work.

My solution was to write my own transmit function for a char array, use the toCharArray() function on the string and send that.

In the future, I'll go back and remove the string and use a char array entirely, but for now, the code works and that's good enough for me.

Thanks for the help.