Understanding the codes in the library

Now we will try to understand the coding techniques of maniacbug and how the same code runs one transceiver in TX mode and the other in RX mode.

Lets start with the Getting started code.

  • First of all you write all the header files that will be needed for the working of the transceivers.
  • Since our ce pin is pin 9 and cs pin is pin 10 we pass the arguements 9,10 when we set up the radio.
  • Then you need to initialize the pipeline addresses thorough which the nodes will cmmunicate . A nice article on deciding what pipe addresses to choose and what not to chose can be found HERE . (btw i just copy paste the pipelines used in the library codes!!!)
  • Now he defines a user defined data type (enum) called “role_e” that can take the values “role_ping_out ” and “role_pong_back” only with the numeric value assigned to “role_ping_out” being 1 and 2 for “role_pong_back”.
  • Note that “role” variable of the datatype “role_e” is  “role_pong_back” that means numerical value of “role” = 2;
  • Now we enter the setup function.
  1. Here we begin the serial communication  and begin the radio.
  2. The author has created a library “printf_h which allows you to use the “printf” command just like you use in c programming.
  3. Since role = 2 hence it prints the third element of the string array.
  4. If you have the list of commands from the documentation and their utilities then you can clearly understand what the rest of the functions do.
  • Now we enter the loop part.
  1. Since the role is “role_pong_back” it enters the second “if” statement,but then it will do anything only if a payload is available which is NOT available so it keeps on coming out and checks whether a serial buffer(whatever you write in the serial monitor i.e ‘t’) is available or not.Once you have entered ‘t’ and since in the beginning role is “role_pong back” it make the node to be a transmitter by changing its role to “role_ping_out” and opening the first pipe for writing and the second one for reading.
  2. The else if part code the exact opposite things i.e changes the “role” to “role_pong_back” and opens the second pipe as a writing pipe and the first one for reading.
  • After this it will enter the first “if” statement and transmit something which will be received by the other node as we haven’t typed anything there it still looks for the payload(and of course the serial buffer from the serial monitor).
  • What does the TX node transmit?
  1. As soon as it enters the TX mode ,it first of all stops listening for any payload(that’s the constraint here with nrf24L01+ transceivers or the way this library has been designed) then it stores the current time in the time variable and then sends it.
  • Now lets have a look at the minute details–
  1. The write function returns a boolean datatype i.e true if it was executed properly and false if it failed to execute and this has been beautifully used as conditions for the “if” statements.


  • What the other node does meanwhile?

Now the RX node now which was looking for payloads will enter the if condition on the availability of the payload.Now we need to read the payload.If the payload was read successfully then we need to proceed to do the next thing ,but if the payload although available was not read properly then we need to keep on trying until it is read properly. For this we have beautifully used the while loop.First of all we  declare a boolean variable named “done” and initialize it as “false”. This necessarily makes the program to enter the while loop for the first time. Now if the payload was read then done will become “true” otherwise “false”. Suppose it was NOT read properly then it will still be “false” and this will cause the program to enter the while loop once again.But if the payload was read properly then it will exit the loop.Then it will stop listening and write a payload to be transferred and then start listening once again.

  • Now when the act of transmitting has been done it comes in RX mode as it starts listening due to the call of “radio.startListening” function call.
  • In the “started_waiting_at” variable it stores the time when it entered the RX mode.Then we declare a boolean variable called timeout and initialize it as “false”,now it checks the condition in the while loop.
    1. If payload is available then radio.available will return true otherwise false.
      Let’s assume that payload is NOT available then both the conditions in the while loop will be satisfied and it will enter the loop.inside the loop in the if condition it calls “millis()” once again and subtracts the time when it entered the rx mode and checks if it is greater than 200 ms , if it is then it modifies “timeout” to “true” which then causes it to exit the loop and then again causes it to enter the next if statement which then prints “failed,response timed out.” Now it is very possible that the program runs more than one iteration of while loop as the first call would not have lead the timeout to be > 200ms and causing the exit from while loop. To check that you can just add a “printf” statement which prints anything you like, so that the number of times it prints that particular statement that many times the loop has iterated.
  • Now lets check the the other possibility.
  1. Lets assume that the payload is available or that before the timeout becomes > 200 ms it detects a payload then it will either not enter the while loop or if it is inside it then it will exit when it checks the conditions for the next iteration.Since nature of timeout would still be false it will then enter the else statement and print the time sent by the TX node and subtract the sent time from the present time to give you the round trip delay.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s