nRF24L01+ and a project for a lazy guy

Hello,
I am building a sensor system that has to be installed on a IIm train car (1:22.5). This car should be able to measure:

  • distance (approximate, +/-50 cm maximum), solved with a reed and a magnet on the wheel
  • pitch and roll of the car (in order to check if rails are placed well), solved with a 9DoF FXOS8700 + FXAS21002 gyro, accelerometer and mag.
  • log the informations on an SD card, solved (pretty easy).
  • Display values on a local LCD, works great
  • Transmit data to a remote and receive a number from it. It WAS working one-way and now I’m lost.

The remote has an Arduino Uno, RF24L01+, a button and a led. It should display which data are displayed on the first LCD line and the values on the second. The button is used to switch between the shown data (I will call “data pages” from now on).
I have 5 data pages, selectables with an integer from 0 to 4. It has to be sent to the rail car every time the button is pressed.

The rail car will then choose which data to transmit back (16 characters, ready for my LCD).
This transmission is made at a rate around once per second. Or… -should be made-.

I managed to transmit one value from the car to the remote, but then I added the bi-directional transmission and changed a few things to make it work as desired. Now it is 4 days that I am trying to solve this problem. Any ideas? Thank you in advance!

My current code is the following (whole codes at the bottom):

Rail car/Arduino Mega: radio related instructions

//RADIO TRANSCEIVER
  //Radio transceiver module present 1=yes
  int txAct = 1;
  //Radio transceiver CSN connected to pin
  int txCsn = 8;
  //Radio transceiver CE connected to pin
  int txCe = 6;
  //Radio Mega to Uno channel
  const byte txaddress[6] = "00101";
  //Radio Uno to Mega channel
  const byte rxaddress[6] = "00011";
  RF24 radio(txCe, txCsn);
  

  radio.begin();
  radio.openReadingPipe(0, rxaddress);
  radio.setPALevel(RF24_PA_MIN);
  radio.startListening();
  radio.openWritingPipe(txaddress);


void texttx(void)
{
   Serial.print("transmitting ");
   char txstr[32];  
   int len;
   String txstring1="";
   String txstring2="";
   String fth, ftm, fts;
   float spitch, sroll;
   spitch=(round(pitch*10))/10;
   sroll=(round(roll*10))/10;
   switch(pagec)
   {
        case  1 :  //txstring2+=pagec;
                   txstring2+=distance;
                   txstring2+=odoUnit;
                   break;
        case  2 :  //txstring2+=pagec;
                   txstring2+="P";
                   txstring2+=spitch;
                   txstring2+=" R";
                   txstring2+=sroll;
                   break;
        case  3 :  //txstring2+=pagec;
                   txstring2+=heading;
                   break;
        case  4 :  //txstring2+=pagec;
                   txstring2+=pmpitch;
                   break;
        default :  //txstring2+=pagec;
                   if(th<100)
                   {
                     fth="0";
                   }
                   if(th<10)
                   {
                     fth="00";
                   }
                   if(tm<10)
                   {
                     ftm="0";
                   }
                   if(ts<10)
                   {
                     fts="0";
                   }
                   txstring2+=fth;
                   txstring2+=th;
                   txstring2+=":";
                   txstring2+=ftm;
                   txstring2+=tm;
                   txstring2+=":";
                   txstring2+=fts;
                   txstring2+=ts; 
                  break;
   }
   len=sizeof(txstring2);
   txstring2.toCharArray(txstr,len);
   radio.openWritingPipe(txaddress);
   Serial.println(txstring2);
   if (!radio.write(&txstr, len))
   {
       Serial.println(F("failed"));
   }
}

void comrx(void)
{
  if (radio.available()) 
     {
       char text[] = "";
       String stext;
       int itext;
       int len = radio.getDynamicPayloadSize();
       radio.read(&text, 16);
       stext=text;
       itext=stext.toInt();
       pagec=itext;
     } 
}

Arduino Uno: radio related instructions

    //Radio transceiver module present 1=yes
        int txAct = 1;
    //Radio transceiver CSN connected to pin
        int txCsn = 10;
    //Radio transceiver CE connected to pin
        int txCe = 9;
    //Radio Mega to Uno channel
        const byte txaddress[6] = "00101";
    //Radio Uno to Mega channel
        const byte rxaddress[6] = "00011";
    RF24 radio(txCe, txCsn);

      radio.begin();
      radio.openReadingPipe(1, txaddress);
      radio.setPALevel(RF24_PA_MIN);
      radio.startListening();
      radio.openWritingPipe(rxaddress);

void radiorx(void)
{
   if (radio.available()) {
    char text[32] = "";
    int len = radio.getDynamicPayloadSize();
    radio.read(&text, len);
    txStr=text;
    lcd.clear();
    lcd.setCursor(0,0);
    switch(pagec)
    {
        case 1 : lcd.print("Distance");
                 break;
        case 2 : lcd.print("Pitch and Roll °");
                 break;
        case 3 : lcd.print("Heading");
                 break;
        case 4 : lcd.print("Steep");
                 break;
        default : lcd.print("Inspection Train");
                 break;
    }
    lcd.setCursor(0,1);
    lcd.print(txStr);
  } 
  else
  {
    lcd.clear();
    lcd.setCursor(0,0);
    lcd.print("NO CONNECTION!");
  }
  delay(500);
}

void pagechange(void)
{
  int len; 
  pagec=pagec+1;
  if(pagec>4)
  {
    pagec=0; 
  }
  while(digitalRead(interruptPin)==buttonType)
  {
  } 
  delay(500);
  char txtext[5]="";
  itoa(pagec, txtext, 1);
  len=sizeof(txtext);
   radio.openWritingPipe(txaddress);
   if (!radio.write(&txtext, len))
   {
       //Serial.println(F("failed"));
   }
}

UNO_RX_v3.ino (8.21 KB)

MEGA_TX_v3.ino (14.9 KB)

Unfortunately, you have provided snippets that omit setup(), loop(), and other important pieces of the code.

Shorten up the spacer lines:

//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

Over 100 characters just on that one line. I think I counted 15 such lines, so that’s 1500 characters you could save.

You can also More:Modify your post and use the Attachments option to attach your .ino file and libraries so folks can duplicate your problem.

CrossRoads:
Shorten up the spacer lines:

//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

Over 100 characters just on that one line. I think I counted 15 such lines, so that’s 1500 characters you could save.

You can also More:Modify your post and use the Attachments option to attach your .ino file and libraries so folks can duplicate your problem.

I haven’t removed the separator characters but added the two .ino’s in the first post. Sorry, I was quite tired and desperate I just could not think properly.

thekillerch:
I managed to transmit one value from the car to the remote, but then I added the bi-directional transmission and changed a few things to make it work as desired.

I find it very hard to figure out what exactly you want help with.

  • Is it with wireless communication?
  • Is it with selecting from a menu?
  • Is it displaying data?

If you are having trouble with wireless communication have a look at this Simple nRF24L01+ Tutorial.

Wireless problems can be very difficult to debug so get the wireless part working on its own before you start adding any other features.

The examples are as simple as I could make them and they have worked for other Forum members. If you get stuck it will be easier to help with code that I am familiar with.

If you want bi-directional communication between a master control unit and a slave unit in a train then I recommend the use of the ackPayload feature - see my second example.

Separately, and regardless of what your current problem is, it is not a good idea to use the String (capital S) class on an Arduino as it can cause memory corruption in the small memory on an Arduino. This can happen after the program has been running perfectly for some time. Just use cstrings - char arrays terminated with '\0' (NULL).

...R