Notifications
Clear all

Basic RadioHead Sketches *barely* working on Two Unos

29 Posts
4 Users
25 Likes
1,977 Views
(@leonardo1123)
Tinkerer
Joined: 3 years ago
Posts: 56
Topic starter  

@noweare Sounds good, thanks! Still working on getting the motors to behave how I want them to with the Joystick, I'll check it out shortly!

Fortune Favors the Bold


   
ReplyQuote
noweare
(@noweare)
Member
Joined: 4 years ago
Posts: 118
 

Keep tinkering and reading/learning. Keep it fun !


   
Leonardo1123 reacted
ReplyQuote
frogandtoad
(@frogandtoad)
Member
Joined: 5 years ago
Posts: 1458
 

@leonardo1123

Posted by: @leonardo1123

The Getting Started example kinda showed how to switch roles, but not dynamically, nor while performing other tasks. 

Any tips would be much appreicated!

Perform a search on this forum... I'm pretty sure we have discussed some examples of similar nature that might kick you off in the right direction 🙂

Cheers.


   
Centari and Leonardo1123 reacted
ReplyQuote
(@leonardo1123)
Tinkerer
Joined: 3 years ago
Posts: 56
Topic starter  

@frogandtoad Thanks a bunch! Will do; at a cabin with the heat not totally working at the moment haha... a little too cold to code. 

Fortune Favors the Bold


   
Centari reacted
ReplyQuote
(@leonardo1123)
Tinkerer
Joined: 3 years ago
Posts: 56
Topic starter  

Fortune Favors the Bold


   
ReplyQuote
noweare
(@noweare)
Member
Joined: 4 years ago
Posts: 118
 

There is a lot going on in that sketch. One thing that may help when designing a system is just do one thing first and get that one thing working then go on to the next thing. If you are trying get communication working strip out all other code that doesn't relate to the communication part.

I said strip-out but you can just start another project and copy the code from your current file to your new file. Then you'll end up with projects that do one thing and they can be used for future projects.

 


   
Centari and Leonardo1123 reacted
ReplyQuote
(@leonardo1123)
Tinkerer
Joined: 3 years ago
Posts: 56
Topic starter  

@noweare Thanks, I'll definitely do that! So am I on the right track with having them mainly listening, except putting themselves into send mode and sending data when the data changes? The only problem I can see is if they're both trying to send to each other, that won't work, and they won't know why... But I will come back with an update after the mini-project with either my solution or more clear questions!

Thanks again for all the help.

Fortune Favors the Bold


   
Centari reacted
ReplyQuote
noweare
(@noweare)
Member
Joined: 4 years ago
Posts: 118
 

@leonardo1123 The Gettingstarted.ino does what I think you want to do. The two boards (say remote and local) are communicating with each other.

local is set to TX, remote is set to RX. So local sends something and if no error occurred local changes to RX.

When the remote RX recieves it changes to TX. So they go back and forth without them being in the same state i.e. both RX or TX

 


   
Centari reacted
ReplyQuote
(@leonardo1123)
Tinkerer
Joined: 3 years ago
Posts: 56
Topic starter  

Okay @noweare I am sooooo close. I managed to get them to go back and forth an arbitrary number of times, but then they end up collapsing into a situation where they're both in the same state so they can't move forward. I took the getting started code, removed a lot of comments and extraneous code, but otherwise made no functional changes except for having them reverse roles automatically and putting the radio.start/stoplistening into the main loop so it did that correctly, as it was handled by the part that parsed for user input, which I removed. The same sketch is uploaded on both devices except for two spots I'll point out with comments.

I imagine what I need to do is either find a more concrete way to make sure their roles are opposite or else adding a way for them to be able to switch roles if their current role is failing but I don't know how I would implement that in a way that wouldn't leave it possible for both to flip from both false, to both true, etc. 

Attached is a picture of the serial monitor of one of the devices.

 

I'm super confused because the "reverse role" statement only happens when a transmission or reception occurs successfully, I don't know how they could be falling into the same role. 

Screenshot 2021 01 28 221659

 

#include <SPI.h>
#include "printf.h"
#include "RF24.h"

RF24 radio(8, 10);

uint8_t address[][6] = {"1Node", "2Node"};
bool radioNumber = 0; // this is set to 1 on the other device
bool role = false;  // this is set to true on the other device
float payload = 0.0;

void setup() {

  Serial.begin(115200);

  radio.begin();
  radio.setPALevel(RF24_PA_LOW);
  radio.setPayloadSize(sizeof(payload));
  radio.openWritingPipe(address[radioNumber]);
  radio.openReadingPipe(1, address[!radioNumber]);

} // setup

void loop() {
  if (role) {
    // This device is a TX node
    radio.stopListening();

    unsigned long start_timer = micros();
    bool report = radio.write(&payload, sizeof(float));
    unsigned long end_timer = micros();

    if (report) {
      Serial.print(F("Transmission successful! "));
      Serial.print(F("Time to transmit = "));
      Serial.print(end_timer - start_timer);
      Serial.print(F(" us. Sent: "));
      Serial.println(payload);
      payload += 0.01;

      role = !role;
    } else {
      Serial.println(F("Transmission failed or timed out"));
    }

    delay(1000); // slow transmissions down by 1 second

  } else {
    // This device is a RX node

    radio.startListening();
    uint8_t pipe;
    if (radio.available(&pipe)) {
      uint8_t bytes = radio.getPayloadSize();
      radio.read(&payload, bytes);
      Serial.print(F("Received "));
      Serial.print(bytes);
      Serial.print(F(" bytes on pipe "));
      Serial.print(pipe);
      Serial.print(F(": "));
      Serial.println(payload);

      role = !role;
    }
  } // role

} // loop

 

Fortune Favors the Bold


   
Centari reacted
ReplyQuote
noweare
(@noweare)
Member
Joined: 4 years ago
Posts: 118
 

Add a timeout. If either radio does not recieve anything in X seconds then have one board turn into a RX and the other TX. Then TX sends. Might have to reinit the boards. Not sure.


   
Centari and Leonardo1123 reacted
ReplyQuote
(@leonardo1123)
Tinkerer
Joined: 3 years ago
Posts: 56
Topic starter  

@noweare IT WORKS! I added a 5-failure timeout for transmission that totally solved the problem. Technically there's still a problem if both of them are listening, but that will go away when I implement the ACTUAL code structure, in which they'll both be listening by default and then transmitting if they have new data. This transmission catch-check solves that problem completely though. 

Thanks for sticking with me through this thread, I'm off to try to make a rudimentary sonar, I shall return in another thread if need be!

Once again, because I don't think it could be enough, Thank you. 

Fortune Favors the Bold


   
Centari reacted
ReplyQuote
noweare
(@noweare)
Member
Joined: 4 years ago
Posts: 118
 

That's great news!  Good job. Your a fast learner.

 


   
Leonardo1123 reacted
ReplyQuote
frogandtoad
(@frogandtoad)
Member
Joined: 5 years ago
Posts: 1458
 

@leonardo1123

Posted by: @leonardo1123

@noweare IT WORKS! I added a 5-failure timeout for transmission that totally solved the problem.

Also check out your library documentation... you may well find member functions to consider in your design, such as: auto ack, crc check, etc...

Cheers.


   
ReplyQuote
Centari
(@centari)
Member
Joined: 4 years ago
Posts: 44
 

@leonardo1123, Congratulations and well done.  I actually have a set of these on my wish list now.


   
ReplyQuote
Page 2 / 2