1

I have 2 arduinos I made some code and I managed to send from the first to the second and from the second to the first with two different pairs of code. What I want to do is to send from the first to the second and from the second to the first with no code change. I added in my current code some monitoring commands but what I can see in my Serial monitor is that not all characters are passed thru. I attach something like my code below only more simplified because it is a big project.

1st arduino:

radio.stopListening();
  for(int i=0; i<lnth; i++){
      chartosend=data.charAt(i);
      radio.write( &chartosend, sizeof(chartosend) );
  }
  radio.write(&endchar,1);
radio.startListening();
  if(radio.available()){
    radio.read( &inchar, sizeof(inchar) );
    inradio+=inchar;
    //Serial.println(inchar);
  }

2nd Arduino:

radio.startListening();
  if(radio.available()){
    radio.read( &data, sizeof(data) );
    inradio+=data;
  }
radio.stopListening(); 
  if(analogRead(0)>20){
     for(int i=0; i<6; i++){
      chartosend=pir1.charAt(i);
      radio.write( &chartosend, sizeof(chartosend) );
    }
    radio.write( &endchar, sizeof(endchar) );
  }

Where is the problem and how I could solve this?

  • 1
    Why do you only send one character at a time? (You can send 32 bytes per package, if I remember correctly.) This will create problems, because you are sending lnth packets per loop, but only read one packet per loop. So the buffer will never become empty. You might like to try while( radio.available() ){ instead of the if statement, to partially fix it. – Gerben May 27 '15 at 9:08
  • Big part of the code is missing. What if the radios are misconfigured? Also what if this is a hardware problem (missing cap to smooth the voltage on the nrf24l01+)? – Avamander Aug 17 '15 at 23:23
1

I dunno know about the radio interface you are using. But I can see something which will break your code for sure.

The sections, where you read data from the interface on both Arduinos are passed without a delay, interrupt, polling loop or something else.

Which means: your "read" code section is in no way synchronised with the write code section on the other arduino and vice versa. At the point when you call the function "radio.available()" (which probably tests if there's any data available at the receiver fifo) there is possibly one or any number of bytes available, but not necessary the whole transmission from the other arduino. You have to make sure — somehow — that the transmission is finished, before you can call it a day and go on with processing the received data.

To do this you have to

  1. synchronise the beginning and the end of your transmissions on both Arduinos
  2. poll the data on the respective receiver side, until you are satisfied
  3. or run into a timeout you have to apply in case there's a byte or more lost by chance.

I hope this gives you a little bit of orientation where to start.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.