Problem with nrf module

Hi, im using nrf module in my project for smart home, everything goes okay if i just testing nrf modules with some basic code, but if i type it in the one specific function in the code, module send data just for one time and if i want send it one more time it isnt able to send data again. Code seems very scary but if someone would be so nice to check it and think about it i would be so thankful. (its code for transmitter)

if you have questions about functions send me a message please

If you post your code, here, like shown in the "how to use this forum-please read" (see #7) more people will be able to see it. Members to not like to have to download code.

Have a look at this Simple nRF24L01+ Tutorial.

...R

here is the code, and i know how the nrf works, and know library functions very well, but in this specific function i just cant send data one more time and i dont know why.

    /* project yolanda *voice recognizer* of multiple slaves over the house. using nrf24L01 
     that provides very good range for the communication (over 20 meters in building) we can easily
     managing multiple slaves for instance (room light controlling) only with the voice, 
     for the voice recognition we are using SPCE (geetech speech recognition module) that after every 
     recognized command sends message (byte)  */
     
    #include <SPI.h>
    #include "nRF24L01.h"
    #include "RF24.h"
    int svetlo;
    float volts=0;
    int i=3,led=3,flag=0,j=0,flag2=0,number=1;
    long pTime = 0; 
    int chargerPin=4;
    long interval = 5000, interval2=200,interval3=150;
    int brightness = 0;    // how bright the LED is
    int fadeAmount = 5;    // how many points to fade the LED by
    const float referenceVolts = 218.313253; // the default reference on a 5-volt board
    const int batteryPin = A1;         // battery is connected to analog pin 0
    const uint64_t pipes[3] = { 0xF0F0F0F0E1LL, 0xF0F0F0F0D2LL, 0xF0F0F0F0D3LL };  // two adresses for two devices
    byte command;

    RF24 radio(9,10);
    
    void setup()
    {
    Serial.begin(9600);
    pinMode(chargerPin,INPUT);
    pinMode(3,OUTPUT);
    pinMode(5,OUTPUT);
    pinMode(6,OUTPUT);
    radio.begin();
    radio.setRetries(15,15);
    radio.openReadingPipe(1,pipes[1]);
    radio.startListening();
    radio.printDetails();
    radio.openWritingPipe(pipes[0]);    
    radio.openWritingPipe(pipes[2]);
    radio.openReadingPipe(1,pipes[1]);
    radio.stopListening();
    Serial.write(0xAA);
    Serial.write(0x37);
    delay(1000);
    Serial.write(0xAA);
    Serial.write(0x21);
    for(j; j<2; j++){
     for(i; i<=6; i++){
      if(i!=4){
    digitalWrite(i, HIGH);
    delay(100);
      }
    }
    delay(100);
     for(i; i>=3; i--){
      if(i!=4){
    digitalWrite(i, LOW);
    delay(100);
      }
    }
    delay(100);
  }
    }

    void charging()
    {
      Serial.print("nabija\n");
      int val = analogRead(batteryPin); // read the value from the sensor
      volts = val / referenceVolts;  // calculate the ratio
      unsigned long Time =millis();
      pTime=Time;
      while(volts<=4.3)
      {
        val = analogRead(batteryPin); // read the value from the sensor
        volts = val / referenceVolts;  // calculate the ratio
        if(Serial.available()){
        byte command = Serial.read();
        if(command==0x11){
        Serial.print(command);
        yolanda(command);
             }
        }
             
        if(Time-pTime>=interval3)
        {
            i=3;                   
            digitalWrite(i, HIGH);
            }
        if(Time-pTime>=interval3+150)
        {
            i=5;                   
            digitalWrite(i, HIGH);
            }
        if(Time-pTime>=interval3+300)
        {
            i=6;                   
            digitalWrite(i, HIGH);
            }
    if(Time-pTime>=interval3+500)
        {
            for(i; i>=3; i--){
            if(i!=4){
            digitalWrite(i, LOW);
              }
          pTime=Time;   
            }
    }
    if((digitalRead(chargerPin))==LOW || volts>=4.3)
    {
            for(i; i>=3; i--){
            if(i!=4){
            digitalWrite(i, LOW);
              }         
            }
      i=3;
      break;
    }
         Time =millis(); 
     
      }
    }

   void batterycheck()
   {
    int val = analogRead(batteryPin); // read the value from the sensor
    volts = val / referenceVolts;  // calculate the ratio
      unsigned long Time =millis();
      pTime=Time;
     Serial.println(volts, DEC);
      while(volts<=1.55)
      {
        command=0;
        val = analogRead(batteryPin); // read the value from the sensor
        volts = val / referenceVolts;  // calculate the ratio
        
       
       
        if (Serial.available()){
        if((command=Serial.read())==0x11)
        Serial.print(command, HEX);
          yolanda(command);
         
        }  
             
        if(Time-pTime>=interval2 && flag2==0)
        {
            for(i; i<=6; i++){
            if(i!=4){                                 
            digitalWrite(i, HIGH);
              }
          pTime=Time;
          flag2=1;     
            }
    }
    if(Time-pTime>=interval2 && flag2==1)
        {
            for(i; i>=3; i--){
            if(i!=4){
            digitalWrite(i, LOW);
              }
          pTime=Time;
          flag2=0;     
            }
    }
    if((digitalRead(chargerPin))==HIGH)
    {
    charging();
    }
         Time =millis(); 
     
      }
      if(flag2==1)
       {
            for(i; i>=3; i--){
            if(i!=4){
            digitalWrite(i, LOW);
              }
           flag2=0; 
            }
    }
   }
    
   void light()
  {
    j=0;
    Serial.print("svetlo\n"); 
   for(j; j<2; j++){
      for(i; i<=6; i++){
      if(i!=4){                                 // now has to be Y logo light off (without using fade) 
    digitalWrite(i, HIGH);
      }
    }
    delay(200);
     for(i; i>=3; i--){
      if(i!=4){
    digitalWrite(i, LOW);
      }
    }
    delay(200);
    }
    i=3;
    brightness=0;
    flag=0;
    delay(500);  
  }

 void obyvacka(int command)
 {
  
  radio.openWritingPipe(pipes[0]);
  radio.write( &command, sizeof(command) );
  light();
 }

  void izba(byte command)
 {
  Serial.print("funkcia\n");
  Serial.println(command, HEX);    //just testing what value has the variable after another command have been said 
  radio.openWritingPipe(pipes[0]);
  radio.write( &command, sizeof(command) );
  delay(500);
   light();
 }
   
   void garaz(int command)
 {

  radio.openWritingPipe(pipes[2]);
  radio.write( &command, sizeof(command) );
 }

   void yolanda(byte command)
 {
  Serial.print("yolanda\n");
  led=3;
  while(brightness<255)
    { 
      for(led; led<=6; led++)
         if(led!=4){
         analogWrite(led, brightness);
           }                              // now has to be Y logo light on (using fade) 
         led=3;
       brightness+=fadeAmount;
       delay(40);
    }
    unsigned long Time =millis();       // Time initializing 
    pTime = Time;
    while(Time-pTime<=interval)         // serial reading just for the chosen interval
    {
      if (Serial.available()){
       command = Serial.read();     
       Serial.print("command\n");        
       flag=1;
       break;
      }
      Time =millis();
    }
    if(flag==1)
    {
      if(command>=0x12 && command <=0x15)
      {
        izba(command);
      }
      
      if(command>=0x16 && command <=0x20)
      {
        obyvacka(command);
      }

      if(command>=0x20 && command <=0x24)
      {
        garaz(command);
      }
    }
    else
    {
      led =3;  
      while(brightness>=0)
    { 
      for(led; led<=6; led++)
         if(led!=4){
         analogWrite(led, brightness);
           }                              // now has to be Y logo light off (using fade) 
         led=3;
       brightness-=fadeAmount;
       delay(40);
             }
    }
 }
    void loop(void)
    {
      if(Serial.available()){
        command = Serial.read();
      
   if(command==0x11){
    Serial.print(command);
    yolanda(command);
      }
     }
    batterycheck();  
    
        }

What is the purpose of this?

radio.begin();
radio.setRetries(15,15);
radio.openReadingPipe(1,pipes[1]); //Open read pipe once
radio.startListening();
radio.printDetails();
radio.openWritingPipe(pipes[0]); //Open write pipe once
radio.openWritingPipe(pipes[2]); //Open write pipe twice
radio.openReadingPipe(1,pipes[1]); //Open read pipe twice
radio.stopListening(); //Close read pipe

The radio cannot read (listen) and write simultaneously, and since you do not read from the pipe there is no need for opening the reading pipe at all. Change the code above to:

radio.begin();
radio.setRetries(15,15);
radio.stopListening();
radio.printDetails();

And make sure that there is a 10uF capacitor on the radio.

pavolk:
but in this specific function i just cant send data one more time and i dont know why.

Can you describe in a few sentences in English the series of actions that you want to happen

Then describe what actually happens.

I see this

if(command==0x11){
    Serial.print(command);
    yolanda(command);

and I see this

if(flag==1)
    {
      if(command>=0x12 && command <=0x15)
      {
        izba(command);
      }
     
      if(command>=0x16 && command <=0x20)
      {
        obyvacka(command);
      }

      if(command>=0x20 && command <=0x24)
      {
        garaz(command);
      }
    }

and I don't think the second part cannot work if the first part is satisfied

Wireless problems are always hard to debug so it is much better to get the wireless stuff working first with a very simple program and then start adding the other stuff. Just send dummy values to start with.

...R

its hard to explain all the code, because i use also voice rocognition module, so first arduino gets some data from voice recognizer, for instance i said my room and the command for that is 0x12 hex, than know which room and which module has to call, so if it is 0x12 nrf module has to call other nrf module in my room and therefore it calls function room that opens pipe 1 to call module in my room.. and i tried just send some data from void loop, just numbers like 1 to reciever and everything was okay, but if i want to send it from function void izba(byte command), reciever gets data only once and than nothing and i have to reupload code for every arduino and than it goes one time again but than nothing.

pavolk:
its hard to explain all the code, because i use also voice rocognition module,

When you have a problem with a program simplify it.

For the moment forget about voice modules and anything else unconnected with the wireless.

Write a short program that changes the value of a variable once per second between the values that will be produced by the voice module.

And in that program use that value to send a message (the value of millis() is useful because it changes in a predictable way) to each of your slaves in turn.

If that short program does not work it will at least make it much easier to help you.

...R

so everything goes okay when i use for example this code

void loop(void)
    {
     radio.openWritingPipe(pipes[0]);
     radio.write( &command, sizeof(command) );
    
        }

but when i use code with waiting for voice recognizer, he sends data, i see it in serial that really data is 0x11 but radio cant send it in this example

void loop(void)
    {

      if(Serial.available()){

       command = Serial.read();
       Serial.println(command, HEX);

       if(command==0x11) 
                      {
       radio.openWritingPipe(pipes[0]);
       radio.write( &command, sizeof(command) );
                      }
     }

        }

im using Geeetech® Voice Recognition
here s the manual http://www.geeetech.com/wiki/images/6/69/Voice_Recognize_manual.pdf

here s the photo of it

What happens if you do not re-open the writing pipe in the second example? Or are you opening the wrong pipe address?

in the first example is the same opening and it sends data and reciever gets it, so i think its okay open it this way

And you are sure that "command" is actually 0x11 and not something else?

pavolk:
so everything goes okay when i use for example this code

void loop(void)

{
    radio.openWritingPipe(pipes[0]);
    radio.write( &command, sizeof(command) );
   
        }

That is not typical of what you want to do. Extend that example so it can send to your 3 slaves (or is it 2?) without the complexity of any voice recognition.

And if you need more help please post the complete program - the problem is often in the other part.

...R

the voice recognizer is like just serial input, so in the code is serial read exactly what comes from voice recognizer, check how the voice recognizer works here http://www.geeetech.com/wiki/images/6/69/Voice_Recognize_manual.pdf

and here is the code, its very simple, just wait for what data come from serial (this data send voice recognizer) and than send it, but this doesnt work. Without waiting for serial it works, like the example in previous post

#include <SPI.h>
    #include "nRF24L01.h"
    #include "RF24.h"
   
    const uint64_t pipes[3] = { 0xF0F0F0F0E1LL, 0xF0F0F0F0D2LL, 0xF0F0F0F0D3LL }; 
    byte command;

    RF24 radio(9,10);
    
    void setup()
    {
    Serial.begin(9600);
   
    radio.begin();
    radio.stopListening();
    Serial.write(0xAA);
    Serial.write(0x37);
    delay(1000);
    Serial.write(0xAA);
    Serial.write(0x21);
    }

void loop(void)
    {
      if(Serial.available()){
        command = Serial.read();
      
   if(command==0x11){
     radio.openWritingPipe(pipes[0]);
     radio.write( &command, sizeof(command) );
      }
     }

        }

If you indent your code consistently it makes it much easier to read

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

const uint64_t pipes[3] = { 0xF0F0F0F0E1LL, 0xF0F0F0F0D2LL, 0xF0F0F0F0D3LL };
byte command;

RF24 radio(9,10);

//=============
 
void setup() {
    
    Serial.begin(9600);
 
    radio.begin();
    radio.stopListening();
    Serial.write(0xAA);
    Serial.write(0x37);
    delay(1000);
    Serial.write(0xAA);
    Serial.write(0x21);
}

//=============

void loop(void) {
    
    if(Serial.available()) {
        command = Serial.read();
         
        if(command==0x11) {
            radio.openWritingPipe(pipes[0]);
            radio.write( &command, sizeof(command) );
        }
    }
}

With that program you have no means to know if you actually receive an 0x11 byte. That's why I am suggesting that you start simple.

Start with what I suggested in Reply #7

...R

:smiley: pls trust me i checked everything with serialprint, i know how the code works but i dont know why in this example transmitter doesnt send data, thats all

Feel free to ignore my advice. But just so you know, my projects work.

...R

just trust me what im saying, i did everything you advised to me, but there is still problem with this part

void loop(void)
    {

      if(Serial.available()){

       command = Serial.read();
       Serial.println(command, HEX); !! here i know that is 0x11 

       if(command==0x11) 
                      {
       
       radio.openWritingPipe(pipes[0]);
       radio.write( &command, sizeof(command) );

       Serial.println("if condition works"); !! here i know that transmitter has to send 0x11
                      }
     }

        }

this way it works i try it right know

 void loop(void)
    {
      while (Serial.available()){
        byte b = Serial.read();
      if(b >= 0x11 && b<= 0x15 ){
    radio.stopListening();
    Command=1;
   const char lol[]="zapat";
   const char omg[]="vypat";
   if(b == 0x11){
      
      radio.write( &lol, sizeof(lol) );
   }
     if(b == 0x12){
      
      radio.write( &omg, sizeof(omg) );
   }
    radio.startListening();
      }
    delay(50);
    }
    }

pavolk:
just trust me what im saying

You would be surprised how many Threads demonstrate that that would be unwise. :slight_smile:

Have you noticed that there is one significant difference between your two pieces of code? Something in the working one that is not in the other?

That is why a very systematic approach is essential.

And PLEASE use the AutoFormat tool to lay your code out consistently - it makes it so much easier to read.

...R