Sorry for the delay in coming back this project, Have been away and not had chance to play.
I've been carrying out some test and added to the RX to display on the LCD if it the TX signal is lost.
After doing so testing I found that the TX SIGNAL lost comes up at random places, Bearing in mind I'm using the hardware set up that I used in another project so I can kind of count the hardware as with my wireless mulitmeter code doe not do this, But this does not have if TX signal is lost code and seems work in the same place as the digital level.
I think it may be to do with the extra code and the way it's set up, I've not really noticed if did not do this before hand. I need some sort of way if the RX does not receive any TX signal as a sort of bit of safety telling the user it's lost or not receiving data from the TX unit.
Here is the latest version of the RX code:
//####################################################
//# Include all the libraries #
//####################################################
#include <LiquidCrystal.h>// Highlight out when ready to use mian display
#include <SPI.h> // Comes with Arduino IDE
#include "RF24.h" // Download and Install (See above)
#include <TimedAction.h>
#include <AutoPower.h>
//####################################################
//# Define the output,input pins & voltage referance #
//####################################################
#define LED A2 // Pin 5, Connects to LED wired to Gnd via suitable resistor, such as 220 Ohms
#define HOLD 9 // Pin 2, Connects to HOLD pin on AutoPower module
#define BTN A1 // Pin 3, Connects to BTN pin on AutoPower module
//const uint64_t pipeIn = 0xB3B4B5B6A3LL;//0xE8E8F0F0E1LL64 bit encrypicted code only left as trying new way(Test 1)
byte addresses[][6] = {"1Node"}; // Create address for 1 pipe. Test 2
//####################################################
//# Define the Variables #
//####################################################
#define CE_PIN 6 // set up CE pin fro NRF24l01
#define CSN_PIN 10 // set up CSN pin fro NRF24l01
RF24 Radio(CE_PIN, CSN_PIN); //set the pins
const byte numChars = 8; //After testing this is all I need same setting for the TX
char receivedChars[numChars]; // an array to store the received data
bool newData = false; // se if incoing data is been TX
int dataReceived; // Data that will be received from the transmitter
const long TX_interval = 500; //Error flag timer for not recieving from the tx
unsigned long lastRecvTime = 0; // escape from recieve data
unsigned long TX_previousMillis = 0; // stores the last mills
#define Error_led 2
//bool newData = false;
bool lostData = false;
//####################################################
//# Specifying the CE and CS pins & LCD Pins #
//####################################################
LiquidCrystal lcd(2, 4, 5, 3, 7, 8);
//####################################################
//# SwitchManger routines checks switches #
//####################################################
// Note: last parameter is optional (defaults to 2 seconds if omitted)
AutoPower power(HOLD, BTN, LED, 2); // Hold button 2 seconds to power off
void TimerService02();// ADD THIS FUNCTION PROTOTYPE
TimedAction Timedact02 = TimedAction(80, TimerService02);// mS
void resetData()
{
receivedChars[8] = "0.00";
unsigned long TX_Error = millis();
newData = false;
if (TX_Error - TX_previousMillis >= TX_interval) {
// save the last time you blinked the LED
TX_previousMillis = TX_Error;
digitalWrite(Error_led, !digitalRead(Error_led));
}
if (newData == false) { //new data received from eLevel
//newData = false;
lcd.setCursor(0, 0);
lcd.print("TX SIGNAL");
lcd.setCursor(0, 1);
lcd.print("LOST");
}
}
void setup() /****** SETUP: RUNS ONCE ******/
{
pinMode(Error_led, OUTPUT);
digitalWrite(Error_led, LOW);
Serial.begin(9600);
lcd.begin(8, 2); // Initialize the LCD.
pinMode(LED, OUTPUT);
digitalWrite(BTN, LOW);
digitalWrite( HOLD, HIGH); //Turn it back on after 1.5 seconds
lcd.clear();
power.setTimeout(300);
/*
radio.begin();
radio.setAutoAck(false);
radio.setDataRate(RF24_250KBPS); // Both endpoints must have this set the same
radio.setChannel(108); // Above most Wifi Channels
radio.setPALevel(RF24_PA_MAX); // Uncomment for more power
radio.openReadingPipe(1, pipeIn);
radio.startListening();
lcd.createChar(1, upArrow);
lcd.createChar(2, downArrow);
*/
Radio.begin(); // Start up the physical nRF24L01 Radio
Radio.setChannel(108); // Above most Wifi Channels
Radio.setPALevel(RF24_PA_MAX); // Uncomment for more power
Radio.setDataRate(RF24_250KBPS); // Fast enough.. Better range
Radio.openReadingPipe(1, addresses[0]); // Use the first entry in array 'addresses' (Only 1 right now)
Radio.startListening();
resetData();
}
void recvData()
{
while ( Radio.available() ) {
Radio.read( &receivedChars, sizeof(receivedChars) ); // Get the data payload (You must have defined that already!)
lastRecvTime = millis();
newData = true;
}
}
void loop() /****** LOOP: RUNS CONSTANTLY ******/
{
recvData();
Timedact02.check();
power.updatePower();
if (newData == true) { //new data received from eLevel
lcd.setCursor(0, 0);
lcd.print("TX GOOD");
}
unsigned long now = millis();
if ( now - lastRecvTime > 1000 ) { //if data stops coming turn everything off with a second
// signal lost?
resetData();
}
digitalWrite(Error_led, LOW);
}
void TimerService02() {
if (newData == true) { //new data received from eLevel
lcd.setCursor(0, 1);
lcd.print(receivedChars);
}
// lcd.setCursor(0, 1);
// lcd.print(receivedChars);
// lcd.print(" ");
// Serial.println(dataReceived);
}
And here is the TX code
#include <SPI.h>
#include "RF24.h" // Download and Install (See above)
//#######################
//#include <nRF24L01.h>
//#include <RF24.h>
//#########################
//#include <LiquidCrystal_I2C.h>
#include <Wire.h>
//#include <SoftwareSerial.h>
//SoftwareSerial mySerial(6, 5); // RX, T 2,3
//LiquidCrystal_I2C lcd(0x03f, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE); // Set the LCD I2C address
//#define mySerial Serial1
//#define ECHO_TO_SERIAL 1 //change to zero = no serial output
//#include <TimedAction.h>
const byte numChars = 8; //After testing this is all I need same setting for the RX
char receivedChars[numChars]; // an array to store the received data
boolean newData = false;
#define CE_PIN 9 // set up CE pin fro NRF24l01
#define CSN_PIN 10 // set up CSN pin fro NRF24l01
RF24 Radio(CE_PIN, CSN_PIN); // Create a Radio
//######################################################
//const uint64_t pipeOut = 0xB3B4B5B6A3LL;//0xE8E8F0F0E1LL; //IMPORTANT: The same as in the receiver
byte addresses[][6] = {"1Node"}; // Create address for 1 pipe.
void setup() {
Serial.begin(9600);
// Serial.println("TEST");
// mySerial.begin(9600);
/* radio.begin();
radio.setAutoAck(false);
radio.setDataRate(RF24_250KBPS);
radio.setChannel(108); // Above most Wifi Channels
radio.setPALevel(RF24_PA_MAX); // Uncomment for more power
radio.openWritingPipe(pipeOut);
*/
Radio.begin(); // Start up the physical nRF24L01 Radio
Radio.setChannel(108); // Above most Wifi Channels
Radio.setPALevel(RF24_PA_MAX); // Uncomment for more power
Radio.setDataRate(RF24_250KBPS); // Fast enough.. Better range
Radio.openWritingPipe( addresses[0]); // Use the first entry in array 'addresses' (Only 1 right now)
}
void loop() {
recvWithEndMarker();
showNewData();
// radio.write( &data, sizeof(MyData) ); // Transmit the data
}
void recvWithEndMarker() {
static byte ndx = 0;
char endMarker = '\n';
char rc;
while (Serial.available() > 0 && newData == false) {
rc = Serial.read();
if (rc != endMarker) {
receivedChars[ndx] = rc;
ndx++;
if (ndx >= numChars) {
ndx = numChars - 1;
}
}
else {
receivedChars[ndx] = '\0'; // terminate the string
ndx = 0;
newData = true;
}
}
}
void showNewData() {
if (newData == true) {
// Serial.print("This just in ... ");
// Serial.println(receivedChars);
Radio.write( &receivedChars, sizeof(receivedChars) ); // Transmit the data
newData = false;
}
}
So I guess this what I mean by optimising the code as there may be an more of an elegant way