Writing your own code

We will transmit integers and strings here to get some experience and fluency in writing our own codes.

The trick to write any code with minimum or no errors is to first draw a detailed flowchart or algorithm that you will follow. I know it’s trivial but believe me people who are new to programming just don’t do that. I believe that this should be followed even if the code is too simple. OK enough of this preaching , let’s get back to business….

One of the node first stays in TX mode and the other in RX mode. If the message is transmitted properly then it goes into RX mode. Same thing happens for the other node.It first stays in RX mode and then goes to TX mode.The changes in mode is handled with the help of a boolean variable which ensures that the mode doesn’t change unless the function of the mode it’s in is completed.

So the TX code is…

//these programs have a problem that both of them might be in the same mode at the same time if reset button is pressed so they wont work !!
#include<SPI.h>
#include"nRF24L01.h"
#include"RF24.h"
int msg = 1,rec;
bool stat = true;
RF24 radio(9,10);
const uint64_t pipe[2] = {0xF0F0F0F0E1LL, 0xF0F0F0F0D2LL} ;
void setup()
{
  Serial.begin(57600);
  radio.begin();
  Serial.println(radio.getPayloadSize());
  radio.openReadingPipe(1,pipe[0]);
  radio.openWritingPipe(pipe[1]);
  radio.startListening();
  radio.printDetails();
}
void loop()
{
  Serial.print("status is : ");
  Serial.println(stat);
  if(stat)
  {
    transmit();
  }
  else
  {
    receiver();
  }
}
void transmit()
{
  radio.stopListening();
  if(radio.write(&msg,sizeof(msg)))
  {
    Serial.println("transmitted msg successfully");
    stat = false;
    msg++;
    if(msg >= 100)
    {msg = 1;}
    radio.startListening();
    delay(20);
    
  }
  else
  {Serial.println("failed to transmit msg");}
 
}
void receiver()
{
  if(radio.available())
  {
    Serial.println("payload available...");
    bool t2 =false;
   while(!t2)
  {t2 = radio.read(&rec,sizeof(rec));}
    if(t2)
    {
      Serial.print("received msg is : ");
      Serial.println(rec);
      stat = true;
    }
    else
    {
      Serial.println("failed to receive msg");
    }
  }
  else
  {
    Serial.println("payload not available.");
  }
}

And the RX code is–

#include<SPI.h>
#include"nRF24L01.h"
#include"RF24.h"
int rec;
int rex;
bool stat = false;
RF24 radio(9,10);
const uint64_t pipe[2] = {0xF0F0F0F0E1LL , 0xF0F0F0F0D2LL};
void setup()
{
  Serial.begin(57600);
  radio.begin();
  Serial.println(radio.getPayloadSize());
  radio.openReadingPipe(1,pipe[1]);
  radio.openWritingPipe(pipe[0]);
  radio.startListening();
  radio.printDetails();
}
void loop()
{
  Serial.print("status is : ");
  Serial.println(stat);
  if(stat == false)
  {receiver();}
  else
  {transmit();}
}
void receiver()
{
  if(radio.available())
  {
    Serial.println("payload available...");
    bool t2 =false;
    while(!t2)
    {t2 = radio.read(&rec,sizeof(rec));}
    if(t2)
    {
      Serial.print("received msg is : ");
      Serial.println(rec);
      stat = true;
      rex = rec*10;
    }
    else
    {Serial.println("failed to receive msg");}
  }
  else
  {
    Serial.println("payload not available.");
  }
}
void transmit()
{
  radio.stopListening();
  if(radio.write(&rex,sizeof(rex)))
  {
     Serial.println("transmitted msg successfully");
     stat = false;
     radio.startListening();
     delay(20);
  }
  else
  {Serial.println("failed to transmit msg");}
}

 

 

 

 

Advertisements

About Shantam Raj

Currently I am a final year B.Tech undergraduate majoring in “Electronics and Communication Engineering” from IIT Guwahati. I am passionate about electronics and robotics. Apart from that I love writing, visit https://medium.com/@shantam_raj for some of my works. I am a die hard soccer fan. I also love to play badminton. I am a hardware hacking enthusiast and a tinkerer !!.
This entry was posted in Arduino, nRF24L01+, wireless communication and tagged , . Bookmark the permalink.

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s