Serial to NRF24L01

Hello.

Im currently working on a project, were I have one arduino uno with NRF2L01 as a basestation, and one Arduino Nano with NRF2L01 mounted on a model train. If i preset the values in the arduino uno program, the train reacts to the commands ( A =Speed, B = direction and C = on/off) .

But now I want the uno to read the commands from Serial. I've testet several ways to Serial.read(), but i havent find any way to store the readings from serial , into A, B and C.
I think the problem is that serial is ASCII and i need to send it as an integer.

So, anyone have an idea for the code in void loop, for reading serial and store as int inside A,B and C?

Code from the basestation

#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
#include <stdlib.h>


const uint64_t pipe = 0xE8E8F0F0E1LL; // Define the transmit pipe
RF24 radio(9, 10); // Create a Radio


int A = 255; //Speed between 0 and 255
int B = 1;   //Direction - 1: forward, 0:Back
int C = 1;   //1 = enable 2 = disable
int verdier[3] = {A,B,C};// 3 element array holding verdier reading

void setup()  
{
    Serial.begin(9600);
    radio.begin();
    radio.stopListening();   
    radio.openWritingPipe(pipe);
    delay(400);
}

void loop()
{ 
  
  if (Serial.available() > 0)
  
         {
          // ???
         }
          


  radio.write( verdier, sizeof(verdier) );
  delay(150); }

Here is the code at the train :

#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
const uint64_t pipe = 0xE8E8F0F0E1LL; // Define the transmit pipe
RF24 radio(9,10); // Create a Radio
int verdier[3];  // 3 element array holding verdier readings

void setup()  
{
    Serial.begin(9600);
    delay(1000);
    radio.begin();
    radio.openReadingPipe(1,pipe);
    radio.startListening();  
    pinMode(3, OUTPUT);
    pinMode(4, OUTPUT);
    pinMode(5, OUTPUT);
}
void loop() 
{ 
  if (radio.available())
  {
        // Read the data payload until we've received everything
        bool done = false;
        while (!done)
  {
        done = radio.read(verdier,sizeof(verdier));   
  } 
  }
  else
  {
    
  }  
 
  
  if(verdier[2]==0)
  {                  
    analogWrite(5,0);
    digitalWrite(4,LOW);
    digitalWrite(3,LOW);
  } 
  else if (verdier[2]==1) //
  {      
    if (verdier[1]==0)
    {                
      analogWrite(5,verdier[0]);
      digitalWrite(4,HIGH);
      digitalWrite(3,LOW);
    }
    else if (verdier[1]==1)
    {              
      analogWrite(5,verdier[0]);
      digitalWrite(4,LOW);
      digitalWrite(3,HIGH);
    }
    else
    {      
      analogWrite(5,0);
      digitalWrite(4,LOW);
      digitalWrite(3,LOW);
    }
  }
  else
  {  
    analogWrite(5,0);
    digitalWrite(4,LOW);
    digitalWrite(3,LOW); 
  }

delay(500);
}

I've testet several ways to Serial.read(), but i havent find any way to store the readings from serial , into A, B and C.
I think the problem is that serial is ASCII and i need to send it as an integer.

What do you suppose that the Stream::parseInt() function does? Serial is an instance of HardwareSerial, which derives from Stream.

Have a look at the examples in Serial Input Basics - simple reliable ways to receive data. There is also a parse example which shows how to place data in variables.

I am using the 3rd example to receive data from my PC and pass it on to my model trains with an nRF24L01+

This Simple nRF24L01+ Tutorial may also be of interest. I use the second example (using the ackPayload feature) so my locos can send back their battery voltage.

...R

Thanks Robin! Good tutorial