Hi Guys , I need help with my code for starters - do not get any errors when I Compile but some part of the code is not working as it should - in the Rx part I put in a section of the code to help me know if the module is not connected by printing " Signal Lost" but all it does is just printing constantly with no values
I have attached both Tx and Rx Code any help will be highly appreciated
Tx
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
#include <Wire.h>
//#include <LiquidCrystal.h>
/*-----( Declare Constants and Pin Numbers )-----*/
#define CE_PIN 9
#define CSN_PIN 10
#define Vin1 A0
#define Vin2 A1
//#define Temp1 A2
//#define Temp2 A3
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
//LiquidCrystal lcd(7, 6, 5, 4, 3, 2); // sets LCD pins
// NOTE: the "LL" at the end of the constant is "LongLong" type
const uint64_t pipe = 0xE8E8F0F0E1LL; // Define the transmit pipe
/*-----( Declare objects )-----*/
RF24 radio(CE_PIN, CSN_PIN); // Create a Radio
/*-----( Declare Variables )-----*/
int value[4]; // 2 element array holding value readings
void setup()
{
radio.begin();
radio.setDataRate(RF24_250KBPS);
radio.openWritingPipe(pipe);
}//(end of setup)---
void loop() /********lOOP: RUNS CONSTANTLY********/
{
value[0] = analogRead(Vin1);
value[1] = analogRead(Vin2);
//value[3] = analogRead(Temp1);
//value[4] = analogRead(Temp2);
radio.write(value,sizeof(value) );
}
RX code
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
#include <Wire.h>
// two libraries inserted into the 'libraries' folder
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
//#include <LiquidCrystal.h>
/*-----( Declare Constants and Pin Numbers )-----*/
#define CE_PIN 9
#define CSN_PIN 10
//LiquidCrystal lcd(7, 6, 5, 4, 3, 2); // sets LCD pins
// NOTE: the "LL" at the end of the constant is "LongLong" type
const uint64_t pipe = 0xE8E8F0F0E1LL; // Define the transmit pipe
/*-----( Declare objects )-----*/
RF24 radio(CE_PIN, CSN_PIN); // Create a Radio
/*-----( Declare Variables )-----*/
int analogInput1 = A0; // Define Analog pin A0 f to read potential Battery 1
int analogInput2 = A1; // Define Analog pin A1 f to read potential Battery 2
float vout1 = 0.0;
float vout2 = 0.0;
float vin1 = 0.0;
float vin2 = 0.0;
float R1 = 30000.0; //
float R2 = 7500.0; //
int value1 = 0;
int value2 = 0;
//Orginal code
int value[2]; // 2 element array holding value readings
//const int calVal = 509; // Value of current sensor at 0.00 Amps
//const int mVpA = 66; // Millivolts per Amp of current sensor
//const int averages = 20; // # of sensor averages that are taken
const int maxfail = 500; // # of fails before timeout
int fail = 0; // # of times signal was not received
int count = 0; // # of times loop has run successfully
//0ld Code
//float VoltCal = -.12; // offset to calibrate voltage reading
//float voltCount = 0.00; // used to find average Voltage
//float ampCount = 0.00; // used to find average current
//float averageAmps = 0.00;
//float averageVolts = 0.00;
//float averageWatts = 0.00;
#define OLED_RESET 4
Adafruit_SSD1306 display(OLED_RESET);
void setup() /****** SETUP: RUNS ONCE ******/
{
//lcd.begin(16, 2); // start LCD
//lcd.print("Initializing...");
//lcd.setCursor(0, 1);
//lcd.print("Version 1.5");
// Intializing the OLED
display.begin(SSD1306_SWITCHCAPVCC, 0x3C); // initialize with the I2C addr 0x3D (for the 128x64)
display.display(); // show splashscreen
delay(2000);
display.clearDisplay(); // clears the screen and buffer
//Intializing RF 24 Radio
radio.begin(); // start radio
radio.setDataRate(RF24_250KBPS); // Lower data rate to increase range
radio.openReadingPipe(1, pipe);
radio.startListening();
delay(1000);
}//--(end setup )---
void loop() /****** LOOP: RUNS CONSTANTLY ******/
{
if ( radio.available() ) // if signal is received
{
{
// Fetch the data payload
radio.read( value, sizeof(value) );
// read the value at analog input
int value1 = (value[0]) ;//reading from the array of the tx value 0
int value2 = (value[1]) ;//reading from the array of the tx value 1
//value1 = analogRead(analogInput1); test for without RF24
//value2 = analogRead(analogInput2); test for without RF24
vout1 = (value1 * 5.0) / 1024.0; // see text store input value for batt1 with multilpier
vout2 = (value2 * 5.0) / 1024.0; // see text store input value for batt1 with multilpier
vin1 = vout1 / (R2 / (R1 + R2)); // Calculate Inout voltage
vin2 = vout2 / (R2 / (R1 + R2)); // Calculate Inout voltage
//Serial.print("BATT1 V= ");
//Serial.println(vin1, 2);
//Serial.print("BATT2 V= ");
//Serial.println(vin2, 2);
delay(500);
// old code as guideline
//int val = (value[0]) ;
//int valAdj = val - calVal; // finds raw value (0-1024) of the current sensor
//float milliVolts = ((valAdj * 5.00) / 1024) * 1000; // calculates millivolts of the current sensor
//float Amps = milliVolts / mVpA; // calculates amperage from current sensor
//float Volts = ((value[1]) * 50.00) / 1024; // calculates voltage
//Volts = Volts + VoltCal; // sets voltage with calibration
//if (Amps > -0.20 && Amps < 0.20) {
//Amps = 0; // amps will display 0 if less that + or - 0.20
//}
//ampCount = ampCount + Amps; // adds amperage together for averaging
//voltCount = voltCount + Volts; // adds voltage together for averaging
fail = 0; // sets fail to 0 since a signal was received
//count ++; // adds 1 to count value for averaging
//if (count == averages) { // if desired # of averages is reached
// routine for displaying text for Voltage/temp readout
display.clearDisplay();
display.setTextSize(.6);
display.setTextColor(WHITE);
display.setCursor(0,0);
display.println("Engineering V1");
display.print("BATT1 V= ");
display.println(vin1, 2);
display.print("BATT2 V= ");
display.println(vin2, 2);
display.display();
delay(2000);
//averageAmps = ampCount / averages; // calculates average current
//averageVolts = voltCount / averages; // calculates average amperage
//averageWatts = averageVolts * averageAmps; // calculates average wattage
//lcd.clear(); // clears LCD
//lcd.print(averageVolts); // writes information to the LCD
//lcd.print("V");
//lcd.setCursor(8, 0);
//lcd.print(averageAmps);
//lcd.print("A");
//lcd.setCursor(0, 1);
//lcd.print(averageWatts);
//lcd.print("W");
//voltCount = 0; // set numbers used for averaging to 0
//ampCount = 0;
count = 0;
}
//}
delay(50);
}
else // if a signal is not detected
{
fail ++; // the value fail raises by 1
//delay(50);
}
if (fail > maxfail) { // if the maximum # of fail is exceeded
display.clearDisplay(); // clear the LCD
display.print("Signal Lost!!!"); // Print "Signal Lost" on the LCD
delay(250);
}
}
Did you get the radios to work with simple examples from the library before writing the more complex code?
Here are some things that helped me and others to get those radios to work.
If you read and, closely, follow Robin2's simple rf24 tutorial you should be able to get them working. That tutorial sure helped me. The code in the examples has been proven to work many many times. If it does not work for you, there is likely a hardware problem.
Run the CheckConnection.ino (look in reply #30 in the tutorial) to verify the physical wiring between the radio module and its processor (Arduino).
Make sure the rf24 power supply can provide enough current. This is especially true for the high power (external antenna) modules. I use homemade adapters like these. They are powered by 5V and have a 3.3V regulator on the board. Robin2 also has suggested trying with a 2 AA cell battery pack.
If using the high powered radios make sure to separate them by a few meters. They may not work too close together. Try the lower power settings.
Reset the radios by cycling power to them after uploading new code. I have found that to help. They do not reset with the Arduino.
Switch to 1MB data rate to catch the not so cloned clones. 'radio.setDataRate( RF24_1MBPS );'
Also for some clones, change TMRh20's RF24.cpp line 44 _SPI.setClockDivider(SPI_CLOCK_DIV2);
Into _SPI.setClockDivider(SPI_CLOCK_DIV4);
Thanks to @6v6gt
I've slightly changed your code and made a struct which is a better way of doing it and added to the RX side a routine if there is no data coming in then it should display signal lost and completely avoid using delay's any where in your code this will stop the code how long your delay is and will have an effect on your lost signal part.
Now providing that your hardware is all correct and working and your code sent the and received the data ok then this should work as I've used the lost signal routine code in on of my projects and works very well.
If you still having trouble in receiving the data then my advise would be follow Robin2's tutorial as mentioned above and get the basic working first.
TX code
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
#include <Wire.h>
//#include <LiquidCrystal.h>
/*-----( Declare Constants and Pin Numbers )-----*/
#define CE_PIN 9
#define CSN_PIN 10
#define Vin1 A0
#define Vin2 A1
#define Temp1 A2
#define Temp2 A3
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
//LiquidCrystal lcd(7, 6, 5, 4, 3, 2); // sets LCD pins
// NOTE: the "LL" at the end of the constant is "LongLong" type
const uint64_t pipe = 0xE8E8F0F0E1LL; // Define the transmit pipe
/*-----( Declare objects )-----*/
RF24 radio(CE_PIN, CSN_PIN); // Create a Radio
/*-----( Declare Variables )-----*/
struct MyData { //create a struct format and data must be the same on RX side
int Voltage_1; //throttle signal
int Voltage_2; //throttle signal
int Voltage_3; //throttle signal
int Voltage_4; //throttle signal
};
MyData data;// holds the data to send
void setup()
{
data.lost_data = 10;
radio.begin();
radio.setDataRate(RF24_250KBPS);
radio.openWritingPipe(pipe);
}//(end of setup)---
void loop() /********lOOP: RUNS CONSTANTLY********/
{
data.Voltage_1 = analogRead(Vin1);
data.Voltage_2 = analogRead(Vin2);
data.Voltage_3 = analogRead(Temp1);
data.Voltage_4 = analogRead(Temp2);
radio.write(&data,sizeof(MyData));
}
RX code:
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
#include <Wire.h>
// two libraries inserted into the 'libraries' folder
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
//#include <LiquidCrystal.h>
/*-----( Declare Constants and Pin Numbers )-----*/
#define CE_PIN 9
#define CSN_PIN 10
//LiquidCrystal lcd(7, 6, 5, 4, 3, 2); // sets LCD pins
// NOTE: the "LL" at the end of the constant is "LongLong" type
const uint64_t pipe = 0xE8E8F0F0E1LL; // Define the transmit pipe
unsigned long lastRecvTime = 0;
/*-----( Declare objects )-----*/
RF24 radio(CE_PIN, CSN_PIN); // Create a Radio
/*-----( Declare Variables )-----*/
int analogInput1 = A0; // Define Analog pin A0 f to read potential Battery 1
int analogInput2 = A1; // Define Analog pin A1 f to read potential Battery 2
float vout1 = 0.0;
float vout2 = 0.0;
float vin1 = 0.0;
float vin2 = 0.0;
float R1 = 30000.0; //
float R2 = 7500.0; //
int value1 = 0;
int value2 = 0;
//Orginal code
int value[2]; // 2 element array holding value readings
//const int calVal = 509; // Value of current sensor at 0.00 Amps
//const int mVpA = 66; // Millivolts per Amp of current sensor
//const int averages = 20; // # of sensor averages that are taken
const int maxfail = 500; // # of fails before timeout
int fail = 0; // # of times signal was not received
int count = 0; // # of times loop has run successfully
//0ld Code
//float VoltCal = -.12; // offset to calibrate voltage reading
//float voltCount = 0.00; // used to find average Voltage
//float ampCount = 0.00; // used to find average current
//float averageAmps = 0.00;
//float averageVolts = 0.00;
//float averageWatts = 0.00;
#define OLED_RESET 4
Adafruit_SSD1306 display(OLED_RESET);
struct MyData { //create a struct format and data must be the same on RX side
int Voltage_1; //throttle signal
int Voltage_2; //throttle signal
int Voltage_3; //throttle signal
int Voltage_4; //throttle signal
};
MyData data;// holds the data to send
void resetData()
{
display.clearDisplay(); // clear the LCD
display.print("Signal Lost!!!"); // Print "Signal Lost" on the LCD
}
void setup() /****** SETUP: RUNS ONCE ******/
{
//lcd.begin(16, 2); // start LCD
//lcd.print("Initializing...");
//lcd.setCursor(0, 1);
//lcd.print("Version 1.5");
// Intializing the OLED
display.begin(SSD1306_SWITCHCAPVCC, 0x3C); // initialize with the I2C addr 0x3D (for the 128x64)
display.display(); // show splashscreen
//delay(2000); not required ?
display.clearDisplay(); // clears the screen and buffer
//Intializing RF 24 Radio
radio.begin(); // start radio
radio.setDataRate(RF24_250KBPS); // Lower data rate to increase range
radio.openReadingPipe(1, pipe);
radio.startListening();
delay(1000);
}//--(end setup )---
void loop() /****** LOOP: RUNS CONSTANTLY ******/
{
recvData();
unsigned long now = millis();
if ( now - lastRecvTime > 500 ) { //if data stops coming turn everything off with in a second
// signal lost?
resetData();
}
vout1 = (data.Voltage_1 * 5.0) / 1024.0; // see text store input value for batt1 with multilpier
vout2 = (data.Voltage_2* 5.0) / 1024.0; // see text store input value for batt1 with multilpier
/*
if ( radio.available() ) // if signal is received
{
{
// Fetch the data payload
radio.read( value, sizeof(value) );
// read the value at analog input
int value1 = (value[0]) ;//reading from the array of the tx value 0
int value2 = (value[1]) ;//reading from the array of the tx value 1
//value1 = analogRead(analogInput1); test for without RF24
//value2 = analogRead(analogInput2); test for without RF24
vout1 = (value1 * 5.0) / 1024.0; // see text store input value for batt1 with multilpier
vout2 = (value2 * 5.0) / 1024.0; // see text store input value for batt1 with multilpier
vin1 = vout1 / (R2 / (R1 + R2)); // Calculate Inout voltage
vin2 = vout2 / (R2 / (R1 + R2)); // Calculate Inout voltage
//Serial.print("BATT1 V= ");
//Serial.println(vin1, 2);
//Serial.print("BATT2 V= ");
//Serial.println(vin2, 2);
delay(500);
// old code as guideline
//int val = (value[0]) ;
//int valAdj = val - calVal; // finds raw value (0-1024) of the current sensor
//float milliVolts = ((valAdj * 5.00) / 1024) * 1000; // calculates millivolts of the current sensor
//float Amps = milliVolts / mVpA; // calculates amperage from current sensor
//float Volts = ((value[1]) * 50.00) / 1024; // calculates voltage
//Volts = Volts + VoltCal; // sets voltage with calibration
//if (Amps > -0.20 && Amps < 0.20) {
//Amps = 0; // amps will display 0 if less that + or - 0.20
//}
//ampCount = ampCount + Amps; // adds amperage together for averaging
//voltCount = voltCount + Volts; // adds voltage together for averaging
fail = 0; // sets fail to 0 since a signal was received
//count ++; // adds 1 to count value for averaging
//if (count == averages) { // if desired # of averages is reached
// routine for displaying text for Voltage/temp readout
display.clearDisplay();
display.setTextSize(.6);
display.setTextColor(WHITE);
display.setCursor(0,0);
display.println("Engineering V1");
display.print("BATT1 V= ");
display.println(vin1, 2);
display.print("BATT2 V= ");
display.println(vin2, 2);
display.display();
delay(2000);
//averageAmps = ampCount / averages; // calculates average current
//averageVolts = voltCount / averages; // calculates average amperage
//averageWatts = averageVolts * averageAmps; // calculates average wattage
//lcd.clear(); // clears LCD
//lcd.print(averageVolts); // writes information to the LCD
//lcd.print("V");
//lcd.setCursor(8, 0);
//lcd.print(averageAmps);
//lcd.print("A");
//lcd.setCursor(0, 1);
//lcd.print(averageWatts);
//lcd.print("W");
//voltCount = 0; // set numbers used for averaging to 0
//ampCount = 0;
count = 0;
}
//}
delay(50);
}
else // if a signal is not detected
{
fail ++; // the value fail raises by 1
//delay(50);
}
if (fail > maxfail) { // if the maximum # of fail is exceeded
display.clearDisplay(); // clear the LCD
display.print("Signal Lost!!!"); // Print "Signal Lost" on the LCD
delay(250);
}
*/
}
void recvData()
{
while ( radio.available() ) {
radio.read(&data, sizeof(MyData));
lastRecvTime = millis();
}
}