element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • About Us
  • Community Hub
    Community Hub
    • What's New on element14
    • Feedback and Support
    • Benefits of Membership
    • Personal Blogs
    • Members Area
    • Achievement Levels
  • Learn
    Learn
    • Ask an Expert
    • eBooks
    • element14 presents
    • Learning Center
    • Tech Spotlight
    • STEM Academy
    • Webinars, Training and Events
    • Learning Groups
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • Technology Groups
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents Projects
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • Project Groups
  • Products
    Products
    • Arduino
    • Avnet Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • Store
    Store
    • Visit Your Store
    • Choose another store...
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
  • Settings
Arduino
  • Products
  • More
Arduino
Arduino Forum NRF24L01 Problem simplifying code
  • Blog
  • Forum
  • Documents
  • Quiz
  • Events
  • Polls
  • Files
  • Members
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
Join Arduino to participate - click to join for free!
Actions
  • Share
  • More
  • Cancel
Forum Thread Details
  • State Verified Answer
  • Replies 16 replies
  • Answers 3 answers
  • Subscribers 394 subscribers
  • Views 2313 views
  • Users 0 members are here
  • nrf24l01
Related

NRF24L01 Problem simplifying code

billpenner
billpenner over 10 years ago

I am using the NRF24 for receiver/transmitter on a link between a Uno and a Mega. the basis for the code is http://maniacbug.github.io/RF24 I have finally gotten the units working super. My project is to input a four digit code on the transmitter, send it to the receiver, which will echo back the data sent for accuracy check. Then the code will be displayed on a  three inch 7 segment display. My problem is: The code from the above is impossible for me to modify without breaking it. It seems that most of the work is done in the library RF24.h. The code is great for debugging and was very helpful for getting the NRF24s working. However, All my efforts to simplify the code to insert the data and extract it results in compiler errors. Does anyone have any help or a source for a more easily modified code for the NRF24?

Thanks to a bunch of great resource and to the dedication to us who are trying to learn.

Bill

  • Sign in to reply
  • Cancel

Top Replies

  • balearicdynamics
    balearicdynamics over 10 years ago +2
    Hello Bill, Please can you put here the parts of code that you have in mind to break and simplify ? Maybe that there is some parts global that should be replaced in a different way. Thank you. Enrico
  • m.ratcliffe
    m.ratcliffe over 10 years ago +1
    The Blue link sends me back to this page, is the library you are using upto date? I got the following from: NRF24: NRF24 library for Arduino " This NRF24 library has now been superceded by the RadioHead…
  • Robert Peter Oakes
    Robert Peter Oakes over 10 years ago +1 suggested
    you may find the code here useful for reference, I used the NRF modules in last XMAS contest and provided all the code http://www.element14.com/community/groups/arduino/blog/2015/01/24/byob-party-15-news…
Parents
  • balearicdynamics
    0 balearicdynamics over 10 years ago

    Hello Bill,

     

    Please can you put here the parts of code that you have in mind to break and simplify  ? Maybe that there is some parts global that should be replaced in a different way.

     

    Thank you. Enrico

    • Cancel
    • Vote Up +2 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
Reply
  • balearicdynamics
    0 balearicdynamics over 10 years ago

    Hello Bill,

     

    Please can you put here the parts of code that you have in mind to break and simplify  ? Maybe that there is some parts global that should be replaced in a different way.

     

    Thank you. Enrico

    • Cancel
    • Vote Up +2 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
Children
  • billpenner
    0 billpenner over 10 years ago in reply to balearicdynamics

    Thanks for the reply, here is a section of the code with my problems;

    I have added  my confusion in bold text after the lines giving me problems. there  are others but if I can understand these areas, maybe I can proceed...

    Thanks for your help

    Bill

     

     

    void loop(void)
    {
      //
      // Ping out role.  Repeatedly send the current time
      //

      if (role == role_ping_out)
      {
        // First, stop listening so we can talk.
        radio.stopListening();
         // this is the data to be sent
        // Take the time, and send it.  This will block until complete
        unsigned long time = millis();
        printf("Now sending %lu...",time);                                                                
        bool ok = radio.write( &time, sizeof(unsigned long) );         // this is writing ok but if  change  anything to

                                                                                                    //write my data ( 4bytes) it wont compile...    

       if (ok)
          printf("ok...");
        else
          printf("failed.\n\r");

        // Now, continue listening
        radio.startListening();

        // Wait here until we get a response, or timeout (250ms)
        unsigned long started_waiting_at = millis();
        bool timeout = false;
        while ( ! radio.available() && ! timeout )         // I think this says " while the radio is available, and not

                                                                               //  timeout,
          if (millis() - started_waiting_at > 200 )          // if I have been waiting longer than 200 uS we have a timeout

                                                                               // with no response
            timeout = true;                                           // if all the above then we have timeout and then we will print

                                                                              //"failed etc as below.

        // Describe the results
        if ( timeout )
        {
          printf("Failed, response timed out.\n\r");
        }
        else                                                                                                   // if not timed out, get data from receiver

                                                                                                                   // buffer
        {
          // Grab the response, compare, and send to debugging spew        //  what does spew mean ?

         unsigned long got_time;                                                                    // its this creating "got_time" ?       

          // Spew it
          radio.read( &got_time, sizeof(unsigned long) );                             // If I try to use my data here, instead

                                                                                                                 // of " &got_time, sizeof(unsigned long)

                                                                                                                  // it wont complile.

          // Spew it
          printf("Got response %lu, round-trip delay: %lu\n\r",got_time,millis()-got_time);
        }

        // Try again 1s later
        delay(1000);
      }

      //
      // Pong back role.  Receive each packet, dump it out, and send it back
      //

      if ( role == role_pong_back )
      {
        // if there is data ready
        if ( radio.available() )
        {
          // Dump the payloads until we've gotten everything
          unsigned long got_time;
          bool done = false;
          while (!done)
          {
            // Fetch the payload, and see if this was the last one.
            done = radio.read( &got_time, sizeof(unsigned long) );         // I don't  understand all after ..radio.read(...

            // Spew it
            printf("Got payload %lu...",got_time);

    // Delay just a little bit to let the other unit
    // make the transition to receiver
    delay(20);
          }

          // First, stop listening so we can talk
          radio.stopListening();

          // Send the final one back.
          radio.write( &got_time, sizeof(unsigned long) );
          printf("Sent response.\n\r");

          // Now, resume listening so we catch the next packets.
          radio.startListening();
        }
      }

      //
      // Change roles
      //

      if ( Serial.available() )
      {
        char c = toupper(Serial.read());                                             //What is "toupper" ?
        if ( c == 'T' && role == role_pong_back )
        {
          printf("*** CHANGING TO TRANSMIT ROLE -- PRESS 'R' TO SWITCH BACK\n\r");

          // Become the primary transmitter (ping out)
          role = role_ping_out;
          radio.openWritingPipe(pipes[0]);
          radio.openReadingPipe(1,pipes[1]);
        }
        else if ( c == 'R' && role == role_ping_out )
        {
          printf("*** CHANGING TO RECEIVE ROLE -- PRESS 'T' TO SWITCH BACK\n\r");
         
          // Become the primary receiver (pong back)
          role = role_pong_back;
          radio.openWritingPipe(pipes[1]);
          radio.openReadingPipe(1,pipes[0]);
        }
      }
    }

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • balearicdynamics
    0 balearicdynamics over 10 years ago in reply to billpenner

    Bill, please in future insert the code with the syntax highlighting so it is more readable and easier to manage (i.e. try to insert in some editor locally). image

    Then also report the compilation errors (the full compilation output where the errors are explained - bad -)

        bool ok = radio.write( &time, sizeof(unsigned long) );        // this is writing ok but if  change  anything to

                                                                                                    //write my data ( 4bytes) it wont compile...

    IMO the definition while reading is wrong. Define it then make the operation. It is inside a loop and the defined variables exists just from where they are defined to the more internal parts of the code. If you have not problems of memory, define the boolean ok with another non-misunderstandable name (e.g. my_Ok) outside the loop as global then initialise it in the setup with the correct value you expect initially (true or false) then use it with the read operation in the loop as you are doing.

    while ( ! radio.available() && ! timeout )        // I think this says " while the radio is available, and not

                                                                              //  timeout,

     

    You check the negate value for both, so it says "while the radio is NOT available AND NOT timeout" that seems generating a never available condition.

    radio.read( &got_time, sizeof(unsigned long) );                            // If I try to use my data here, instead

                                                                                                                // of " &got_time, sizeof(unsigned long)

    // it wont complile.

     

    Please show how you are using your data here, to have an idea why it does not compile.

     

    'toupper()' usage

     

    To be sure that all data in both sides are always checked correctly ignoring the uppercase/lowercase case, despite what is the input string (e.g. all lower case, all uppercase or mixed) the call to toupper forces all the characters to uppercase so the conditions are always checked with the right characters.

     

    Enrico


    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Reject Answer
    • Cancel
  • billpenner
    0 billpenner over 10 years ago in reply to balearicdynamics

    Thank you:

    I don't know how to highlight syntax. could you explain?

    I will try to re-submit the question correctly.

    Thank you for toupper  I should have recognized that it meant "To Upper Case".

    Bill

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • balearicdynamics
    0 balearicdynamics over 10 years ago in reply to billpenner

    Hi Bill,

     

    The following image shows how to catch the syntax highlighting in the editor image

     

    Enrico

     

    image

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Reject Answer
    • Cancel
  • billpenner
    0 billpenner over 10 years ago in reply to balearicdynamics

    I found how to use highlighting. Thanks for the advice.

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • balearicdynamics
    0 balearicdynamics over 10 years ago in reply to billpenner

    Bill,

     

    I suggest you that you set useful answer or correct answer to the post that was correct and helped you to solve the problem, instead of assumed answered the question.

     

    Enrico

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
element14 Community

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2025 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

ICP 备案号 10220084.

Follow element14

  • X
  • Facebook
  • linkedin
  • YouTube