Week 11_ 4/24/19


To understand the NRF24L01 code, I tried to use the module functions to simply turn on an LED. The code is shown below.

I believe that once the initial parameters are set up in and before the setup, the code should be relatively simple. The radio.read and radio.write functions are necessary and for the master code used for our purposes, these functions simply need to be placed in the correct locations to get data transferred from the mater bogie to the LCD screen and remote.



Transmitter Code to turn on an LED:
#include <SPI.h>
#include "RF24.h"
const int led = 43;
int ledState = 0;

RF24 radio (7, 8); // CE, CSN. “radio” can be renamed to anything
byte address[6] = {"0"};

char text[100] = "Hi";


void setup() {
 Serial.begin (9600);
 delay(1000);
 radio.begin();
 radio.setChannel(115); // set frequency to channel 115
 radio.setPALevel(RF24_PA_MAX);
 radio.setDataRate (RF24_250KBPS);
 radio.openWritingPipe(address[0]);
 pinMode(led,OUTPUT);
 
 delay(1000);
}

void loop() {
digitalWrite (led,HIGH); // LOW = off, HIGH = on
ledState = digitalRead (led);
 radio.write(&text, sizeof(text));
 radio.write(&ledState, sizeof(ledState));
 Serial.print(text);
 Serial.print("\n");
 Serial.print(ledState);
 Serial.print("\n");
 delay(1000);
}


Receiver code to turn on an led:

#include <SPI.h>
#include "RF24.h"
RF24 radio(7,8);

byte address[6] = {"0"};

char text[100]= " ";
const int ledPin = 43;
const int SW1 = 2;
int ledState;

void setup() {
 Serial.begin(9600);
 delay(1000);
 radio.begin();
 radio.setChannel(115);
 radio.setPALevel(RF24_PA_MAX);
 radio.setDataRate(RF24_250KBPS);
 radio.openReadingPipe(1,address[0]);
 
 
 pinMode(ledPin,OUTPUT);
 pinMode(SW1,INPUT);

}

void loop() {
 radio.startListening();
 delay(10);
 if(radio.available()) {
   while(radio.available())
   {
     radio.read(&text, sizeof(text));
     radio.read(&ledState, sizeof(ledState));
   }

   Serial.print(text);
   Serial.print("\n");

   if(ledState == 1) {
     digitalWrite(ledPin,LOW); //LED OFF
     Serial.print("OFF");
     Serial.print("\n");
   }

   else if (ledState == 0){
     digitalWrite (ledPin,HIGH);
     Serial.print("ON");
     Serial.print("\n");
   }
   }

else{
   radio.stopListening();
   int buttonState = digitalRead(SW1);
   Serial.print(buttonState);
   Serial.print("\n");
   radio.write(&buttonState, sizeof(buttonState));
   //radio.write(&buttontext, sizeof(&buttontext);

   delay(1000);
}

 }



Comments

Popular posts from this blog

Week 3_ 2/20/19

Week 7_ 3/20/19