Programm simple battery ok / not led blink

hello,

i tried to do a simple led on batt ok ( >3,2V ) , led blink batt ( v, 3,2V);
the batt is in der sender , which when awake reads and sends a bool true or false . the receiver should simply read the status and blink or not blink .

problem : it only works it the transmitter is aktiv . once in sleep , the led starts blinking . why ? who changes the bool to false of no data comes in ?

#include <SPI.h>
#include <RF24.h>
#include <avr/wdt.h>
#include "nRF24L01.h"
#include <movingAvg.h>                   
movingAvg potifilter(25);                  // eine Instanz genannt potifilter(xxx),   mit xxx werte werden aufsummiert und geteilt 

uint8_t adresse[][6] = {"MGP#1", "MGE#1"};                                                                                                                                     
int timeout = 250;

const byte CE_nrf = 8;                       // pin12
const byte CS_nrf = 10;                      // pin 14
RF24 radio(CE_nrf, CS_nrf);                  // Instanz erstellen genannt radio
const byte CS_mcp41hv51 = 9 ;                // Chip select MCP41HV51 pin 13 arduino 9
const byte LED_Pedal_ready = 14;             // pin 23 arduino 14
const byte LED_pedal_on = 3;                 // arduino 3 pin 1 
const byte Opto_on = 2 ;                     // arduino 2 pin 32 
int avg_pedalwert; 
int potivalue ;
int  pedalwert;
unsigned long last_datainput;
unsigned long last_blink;
bool ledState;
bool batt_ok;
bool lastbatt_ok;
struct meine_struktur {
       int raw_poti;
       bool batt_voll;
       };

meine_struktur   Dateneingang;


//******************************************************************* SETUP *****************************************************************************************************************

void setup(){
  //wdt_enable(WDTO_30MS);                                  // mogliche Werte 15MS 30MS 60MS 120MS  250MS  500MS  1S  2S  4S  8S
  pinMode(LED_Pedal_ready, OUTPUT);                       // led fur anyeige dass vcc da ist, bzw. atmega lauft  
  pinMode(LED_pedal_on, OUTPUT);                          // pedal ist gedruckt 
  pinMode(Opto_on, OUTPUT);                               // schaltet das schweissgerat ein
  pinMode(CS_mcp41hv51, OUTPUT);                          // CS SPI yum nahlen des MCP41HV51 , (regelt den schweisstrom als digitales poti) 
  digitalWrite(CS_mcp41hv51, HIGH);                       // CS HIGH ist passiv nicht angewahlt
  SPI.begin();                                            // spi beginn vor radio begin
  radio.begin();                                          // radio begin
  radio.setPALevel(RF24_PA_MAX);                          // RF24_PA_MIN=-18dBm, RF24_PA_LOW=-12dBm, RF24_PA_HIGH=-6dBM, and RF24_PA_MAX=0dBm.; 
  radio.setDataRate(RF24_1MBPS);                          // RF24_250KBPS for 250kbs, RF24_1MBPS for 1Mbps, or RF24_2MBPS for 2Mbps 
  radio.setChannel(50);                                   // At 1MHZ channel bandwidth, (max freq - min freq) 2525 - 2400 MHz = 125 channels + 1
  radio.setAutoAck(1);                                    // confirmation mode, 1 on 0 off
  radio.setRetries(0, 0);                                 // How long to wait between each retry, in multiples of 250us, max is 15. 0 means 250us, 15 means 4000us. count  How many retries before giving up, max 15
  radio.setPayloadSize(4);                                // 1-32 byte  bei byte variable = 1 
  radio.stopListening ();                                 // erst stop lisiting before open writing pipe
  radio.openWritingPipe (adresse[1]);                     // adresse an die geschrieben wird , empfangeradresse 
  radio.openReadingPipe (0, adresse[0]);                  //  adresse  von der gelesen werden soll also    sendeadresse des empfangers   die 1 ist erste pipe  
  radio.startListening();
  potifilter.begin();                                     // initialize the moving average
}


//******************************************************************* LOOP *****************************************************************************************************************
void loop(){ 
    
if ( radio.available()) {                                     // falls was empfangen , in variable pedalwert_empfangen einlesen                                             
  while (radio.available()) {                                 // while damit alles gelesen wird 
  radio.read(&Dateneingang, sizeof(meine_struktur));          // einlesen
  last_datainput = millis();}}                                // timeout nachstellen bei jedem datainput
 
 
 potivalue = Dateneingang.raw_poti ;
 avg_pedalwert = potifilter.reading(potivalue);              // calculate the moving average 
 pedalwert = map(avg_pedalwert, 0, 1023, 0, 255);            // durchschnittwert skalieren auf MCP41HV51  max.0- 255  schwelle 10
 
 if(pedalwert >=1){   
     digitalWrite(LED_pedal_on, HIGH);                        
     digitalWrite(Opto_on, HIGH);
     digitalPotWrite(pedalwert);} 
 
 if(pedalwert <1 ){
     digitalWrite(LED_pedal_on, LOW);                        // LED aus
     digitalWrite(Opto_on, LOW);                             // opto aus 
     digitalPotWrite(pedalwert);}                                      
          
 if(millis()- last_datainput >= timeout){                      // nach xx millisek. keien daten mehr empfangen ,poti auf 0 stellen . 
    pedalwert = 0;}

bool batt_ok = Dateneingang.batt_voll;
if (batt_ok){
     digitalWrite( LED_Pedal_ready, HIGH);}  
    else {blink();}    

                                                    
wdt_reset();                                                  // wdt reset
}



//******************************************************************* BLINK *****************************************************************************************************************

void blink(){
       if(millis() - last_blink >= 750 ){          
       last_blink = millis();     
       if (ledState == LOW) {
       ledState=HIGH;}                                     
       else {ledState = LOW;} 
       digitalWrite( LED_Pedal_ready, ledState);}
       } 

//******************************************************************* digitalPotWrite *****************************************************************************************************************
void digitalPotWrite( int value) {
     digitalWrite(CS_mcp41hv51, LOW);
     SPI.transfer(0);                                      
     SPI.transfer(value);
     digitalWrite(CS_mcp41hv51, HIGH);}type or paste code here

Please follow the advice given in the link below when posting code, in particular the section entitled 'Posting code and common code problems'

Use code tags (the </> icon above the compose window) to make it easier to read and copy for examination

too long for simple led on

if ( radio.available()) {                                     // falls was empfangen , in variable pedalwert_empfangen einlesen                                             
  while (radio.available()) {                                 // while damit alles gelesen wird 
  radio.read(&Dateneingang, sizeof(meine_struktur));          // einlesen
  last_datainput = millis();}}                                // timeout nachstellen bei jedem datainput
 


batt_ok = Dateneingang.batt_voll; 
 if (batt_ok == 1){
     digitalWrite( LED_Pedal_ready, HIGH);}  
 if (batt_ok == 0){
     blink();} `Preformatted text`type or paste code here
`
``

if no data comes from the nrf 24  it starts blinking , why is no data a false ?

The struct "Dateneingang" is declared

struct meine_struktur {
       int raw_poti;
       bool batt_voll;
       };

meine_struktur   Dateneingang;

but never set to any default value by the program itself. So it will have the values as initialized by the compiler code until you get the first valid data from RF. So Dateineingang.batt_voll will quite likely be zero which equals to false.

If you do not want this behavior you must check whether you have received valid data and - for example - switch off the LED in case you have not.

As the code is at the moment you will keep the last received batt state "for ever" if no further valid RF data arrive ... So it might probably be an interesting idea to check the last time you received data and show a different LED signal if a certain timeout has been reached?

There is also a "wdt_reset();" at the end of loop() while the wdt_enable() is commented out in setup() ... ?

i tried to initilise a value of true , but it does the same thing , and actually i want it to be to stay in the value last send , last send true stay true , but it does always fall to false

even if no false ever will bei transmitted , after the first true transmit ends , it falls to false and blicks , tried int 0 and 1 same , no more data incomming it does not stay in the last state

Understood:

As far as I see, the struct Dateneingang is only changed by radio.read() in loop().

Therefore two possibilities remain:

  • The receiver gets data with batt_voll = 0

or

  • The sketch restarts with an empty initialized struct.

The second seems to be more likely to me ... Can you check whether the sketch restarts if you lose RF connection?

#include <SPI.h>
#include <RF24.h>
#include <avr/wdt.h>
#include "nRF24L01.h"
#include <movingAvg.h>
movingAvg potifilter(25);                  // eine Instanz genannt potifilter(xxx),   mit xxx werte werden aufsummiert und geteilt

uint8_t adresse[][6] = {"MGP#1", "MGE#1"};
int timeout = 250;

const byte CE_nrf = 8;                       // pin12
const byte CS_nrf = 10;                      // pin 14
RF24 radio(CE_nrf, CS_nrf);                  // Instanz erstellen genannt radio
const byte CS_mcp41hv51 = 9 ;                // Chip select MCP41HV51 pin 13 arduino 9
const byte LED_Pedal_ready = 14;             // pin 23 arduino 14
const byte LED_pedal_on = 3;                 // arduino 3 pin 1
const byte Opto_on = 2 ;                     // arduino 2 pin 32
int avg_pedalwert;
int potivalue ;
int  pedalwert;
unsigned long last_datainput;
unsigned long last_blink;
bool batt_ok;
bool lastbatt_ok;
bool IsBlinkSet=false;
struct meine_struktur {
  int raw_poti=0;
  bool batt_voll=0;
};

meine_struktur   Dateneingang;


//******************************************************************* SETUP *****************************************************************************************************************

void setup() {
  //wdt_enable(WDTO_30MS);                                  // mogliche Werte 15MS 30MS 60MS 120MS  250MS  500MS  1S  2S  4S  8S
  pinMode(LED_Pedal_ready, OUTPUT);                       // led fur anyeige dass vcc da ist, bzw. atmega lauft
  pinMode(LED_pedal_on, OUTPUT);                          // pedal ist gedruckt
  pinMode(Opto_on, OUTPUT);                               // schaltet das schweissgerat ein
  pinMode(CS_mcp41hv51, OUTPUT);                          // CS SPI yum nahlen des MCP41HV51 , (regelt den schweisstrom als digitales poti)
  digitalWrite(CS_mcp41hv51, HIGH);                       // CS HIGH ist passiv nicht angewahlt
  SPI.begin();                                            // spi beginn vor radio begin
  radio.begin();                                          // radio begin
  radio.setPALevel(RF24_PA_MAX);                          // RF24_PA_MIN=-18dBm, RF24_PA_LOW=-12dBm, RF24_PA_HIGH=-6dBM, and RF24_PA_MAX=0dBm.;
  radio.setDataRate(RF24_1MBPS);                          // RF24_250KBPS for 250kbs, RF24_1MBPS for 1Mbps, or RF24_2MBPS for 2Mbps
  radio.setChannel(50);                                   // At 1MHZ channel bandwidth, (max freq - min freq) 2525 - 2400 MHz = 125 channels + 1
  radio.setAutoAck(1);                                    // confirmation mode, 1 on 0 off
  radio.setRetries(0, 0);                                 // How long to wait between each retry, in multiples of 250us, max is 15. 0 means 250us, 15 means 4000us. count  How many retries before giving up, max 15
  radio.setPayloadSize(4);                                // 1-32 byte  bei byte variable = 1
  radio.stopListening ();                                 // erst stop lisiting before open writing pipe
  radio.openWritingPipe (adresse[1]);                     // adresse an die geschrieben wird , empfangeradresse
  radio.openReadingPipe (0, adresse[0]);                  //  adresse  von der gelesen werden soll also    sendeadresse des empfangers   die 1 ist erste pipe
  radio.startListening();
  potifilter.begin();                                     // initialize the moving average
}


//******************************************************************* LOOP *****************************************************************************************************************
void loop() {
  while (radio.available()) {                                 // while damit alles gelesen wird
    radio.read(&Dateneingang, sizeof(meine_struktur));          // einlesen
    last_datainput = millis();// timeout nachstellen bei jedem datainput
  }
  potivalue = Dateneingang.raw_poti ;
  avg_pedalwert = potifilter.reading(potivalue);              // calculate the moving average
  pedalwert = map(avg_pedalwert, 0, 1023, 0, 255);            // durchschnittwert skalieren auf MCP41HV51  max.0- 255  schwelle 10

  if (pedalwert >= 1) {
    digitalWrite(LED_pedal_on, HIGH);
    digitalWrite(Opto_on, HIGH);
    digitalPotWrite(pedalwert);
  }

  if (pedalwert < 1 ) {
    digitalWrite(LED_pedal_on, LOW);                        // LED aus
    digitalWrite(Opto_on, LOW);                             // opto aus
    digitalPotWrite(pedalwert);
  }
  if (millis() - last_datainput >= timeout) pedalwert = 0;     // nach xx millisek. keine daten mehr empfangen ,poti auf 0 stellen .
  bool batt_ok = Dateneingang.batt_voll;
  if (batt_ok)digitalWrite( LED_Pedal_ready, HIGH);
  else IsBlinkSet=true;
  if(IsBlinkSet)blink();
  //wdt_reset();
}
//******************************************************************* BLINK *****************************************************************************************************************
void blink() {
  if (millis() - last_blink >= 750 ) {
    last_blink = millis();
    digitalWrite(LED_Pedal_ready, !digitalRead(LED_Pedal_ready));
  }
}
//******************************************************************* digitalPotWrite *****************************************************************************************************************
void digitalPotWrite(byte value) {
  digitalWrite(CS_mcp41hv51, LOW);
  SPI.transfer(0);
  SPI.transfer(value);
  digitalWrite(CS_mcp41hv51, HIGH);
} 

UPD

  • The sketch restarts with an empty initialized struct.

no was fixed

  • The receiver gets data with batt_voll = 0

not diectly

i found it

the transmitter did send an old code line , before I changend to struct variable , but only a non batt value was send , still dont get it , why it default to 0 .

It may have to do with the radio.read() function ... ?!?

see https://nrf24.github.io/RF24/classRF24.html#a8e2eacacfba96426c192066f04054c5b

Remarks

Remember that each call to read() fetches data from the RX FIFO beginning with the first byte from the first available payload. A payload is not removed from the RX FIFO until it's entire length (or more) is fetched using read().

  • If len parameter's value is less than the available payload's length, then the payload remains in the RX FIFO.
  • If len parameter's value is greater than the first of multiple available payloads, then the data saved to the buf parameter's object will be supplemented with data from the next available payload.
  • If len parameter's value is greater than the last available payload's length, then the last byte in the payload is used as padding for the data saved to the buf parameter's object. The nRF24L01 will repeatedly use the last byte from the last payload even when read() is called with an empty RX FIFO.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.