Code freezing Perhaps

Hi I have the following functional Code that seems to work once i turn on the 2 arduino mini and if i let them for say 2 - hours standby and i come back to trigger the TX the RX doesn’t respond.

I am using power adapter to supply voltage for both so ok.
nrf24 running P_High for both.

do you think:

A-that something missing in the code causing it to overwork? crash?

B-Am i missing radio sleep maybe?

C-if you feel this can be improved please let me know! appreciate it

suggestions very much appreciated!

TIA.

TX

// Define variables
int L_Button = LOW;
int R_Button = LOW;


#include <SPI.h>
#include "RF24.h"
#include "printf.h"

RF24 radio(9, 10);
// Controller Address

const uint64_t Controller_1 = 0xE8E8F0F0E1LL;
/*-----( Declare Variables )-----*/
uint8_t command[4]; // 2 element array of unsigned 8-bit type, holding readings


void setup()
{
  Serial.begin(115000);
  printf_begin();
  pinMode(2, INPUT); // A Button
  pinMode(3, INPUT); // B Button
  radio.begin();
  radio.setDataRate(RF24_250KBPS);
  radio.openWritingPipe(Controller_1);
  radio.printDetails();
}

void loop()
{
  radio.powerUp();
  Buttons();
  delay(50);
  radio.powerDown();

}


void Buttons(void)
{
  // Check Buttons
  L_Button = digitalRead(2);
  R_Button = digitalRead(3);

  if (R_Button == 1)
  {
    command[0] = 1; // Command to turn on Right indicator
    Serial.println("R1");
  }
  else
  {
    command[0] = 0; // Turn off Indicators
  }

  if (L_Button == 1)
  {
    command[1] = 1; // Command to turn on Left indicator
    Serial.println("L1");
  }
  else
  {
    command[1] = 0; // Turn off Indicators
  }
  Send_Data();
}

// Function to send the data
void Send_Data(void)
{
  radio.write(command, sizeof(command));
  //Serial.println("0");
}

RX:

// Variables used in the program
byte Data_Val_1 = 0; // Stores the data value
byte Data_Val_2 = 0; // Stores the data value
byte Old_Data_Val_1 = 0; // Stores the data value
byte Old_Data_Val_2 = 0; // Stores the data value

uint8_t buf[4];//2 element array of unsigned 8-bit type, holding Joystick readings

#include <SPI.h>
#include "RF24.h"
#include "printf.h"

int timer = 100;
int timer1 = 100;

RF24 radio(9, 10);

// Controller Addresses
const uint64_t Controller_2 = 0xE8E8F0F0E1LL;

void setup()
{
  Serial.begin(115000);
  printf_begin();
  pinMode(2, OUTPUT); // Front Work lights
  pinMode(3, OUTPUT); // Rear Work Lights
  pinMode(4, OUTPUT); // Rear Work Lights
  pinMode(5, OUTPUT); // Rear Work Lights
  pinMode(6, OUTPUT); // Rear Work Lights
  pinMode(7, OUTPUT); // Rear Work Lights


  radio.begin();  
  radio.setDataRate(RF24_250KBPS);
  radio.openReadingPipe(1, Controller_2);
  radio.startListening();
  radio.printDetails();
}

void loop()
{
  if (radio.available())

  {
    radio.read(buf, 4);
    Data_Val_1 = buf[0]; // Store the controller left
    Data_Val_2 = buf[1]; // Store the controller right
  }
  LED_Control();
}

void LED_Control()
{
  if (Data_Val_1 != Old_Data_Val_1)
  {
    if (Data_Val_1 == 1)
    { // Work Lights
      //digitalWrite(2, HIGH); // turn it on
      Serial.println(Data_Val_1);
      blinkLEDLeft();
      goto loop();
    }
    else
    {
      digitalWrite(2, LOW); // turn it on
      digitalWrite(3, LOW); // turn it on
      digitalWrite(4, LOW); // turn it on
    }
    Old_Data_Val_1 = Data_Val_1;
  }
  //********************************************************************************//

  if (Data_Val_2 != Old_Data_Val_2)
  {
    if (Data_Val_2 == 1) {
      //digitalWrite(5, HIGH); // turn it on
      blinkLEDRight();
      Serial.println(Data_Val_2);
    }
    else
    {
      digitalWrite(5, LOW); // turn it off
      digitalWrite(6, LOW); // turn it off
      digitalWrite(7, LOW); // turn it off
    }
    Old_Data_Val_2 = Data_Val_2;
  }
}

void blinkLEDLeft()
{
  digitalWrite(2, HIGH);
  delay(timer);
  digitalWrite(2, LOW);
  delay(timer);

  digitalWrite(3, HIGH);
  delay(timer);
  digitalWrite(3, LOW);
  delay(timer);

  digitalWrite(4, HIGH);
  delay(timer);
  digitalWrite(4, LOW);
  delay(timer);

  digitalWrite(2, HIGH);
  digitalWrite(3, HIGH);
  digitalWrite(4, HIGH);
  delay(timer1);

  digitalWrite(2, LOW);
  digitalWrite(3, LOW);
  digitalWrite(4, LOW);
  delay(timer1);

  digitalWrite(2, HIGH);
  digitalWrite(3, HIGH);
  digitalWrite(4, HIGH);
  delay(timer1);

  digitalWrite(2, LOW);
  digitalWrite(3, LOW);
  digitalWrite(4, LOW);
  delay(timer1);
}
void blinkLEDRight()
{
  digitalWrite(5, HIGH);
  delay(timer);
  digitalWrite(5, LOW);
  delay(timer);

  digitalWrite(6, HIGH);
  delay(timer);
  digitalWrite(6, LOW);
  delay(timer);

  digitalWrite(7, HIGH);
  delay(timer);
  digitalWrite(7, LOW);
  delay(timer);

  digitalWrite(5, HIGH);
  digitalWrite(6, HIGH);
  digitalWrite(7, HIGH);
  delay(timer1);

  digitalWrite(5, LOW);
  digitalWrite(6, LOW);
  digitalWrite(7, LOW);
  delay(timer1);

  digitalWrite(5, HIGH);
  digitalWrite(6, HIGH);
  digitalWrite(7, HIGH);
  delay(timer1);

  digitalWrite(5, LOW);
  digitalWrite(6, LOW);
  digitalWrite(7, LOW);
  delay(timer1);
}
void LED_Control()
{
  if (Data_Val_1 != Old_Data_Val_1)
  {
    if (Data_Val_1 == 1)
    { // Work Lights
      //digitalWrite(2, HIGH); // turn it on
      Serial.println(Data_Val_1);
      blinkLEDLeft();
      goto loop();
    }
    else

It is normally not a good idea to use a goto to exit a function.
Maybe a return would serve your needs instead of a goto.

Actually the reason i had it in there, is because i was trying to get the LED control() to repeat as long as TX pin2 or pin 3 was high , and I am not able to achieve this ..

will take it out but even without still behaving the same.

Do you know whether the problem is in the TX or the RX device ?

Can you add code to blink an LED constantly as a tell-tale to show that the device is working ?

Does this

radio.read(buf, 4);

read 4 characters ? Serial.read() would only read one char.

What happens if there are not 4 chars available - you are aonly checking for 1 with radio.available()

I had not noticed goto loop() - that MUST be removed The correct way to exit a function is return

...R

Actually sending a Low or high from each pin ( total 4) , so i figured(buff,4) perhaps i am wrong?

on the RX side pretty sure , because when i connect to TX i see sending the high over. on RX no incoming , but when i let them off for a an hour or so and back on they reinitialize

Calling loop() from ANYWHERE will crash your program!

Mark

taken out :slight_smile: - no more call to loop().

what would be an ideal call to repeat blink left on RX while pin 1 high on TX

// Variables used in the program
byte Data_Val_1 = 0; // Stores the data value
byte Data_Val_2 = 0; // Stores the data value
byte Old_Data_Val_1 = 0; // Stores the data value
byte Old_Data_Val_2 = 0; // Stores the data value

uint8_t buf[4];//2 element array of unsigned 8-bit type, holding Joystick readings

#include <SPI.h>
#include "RF24.h"
#include "printf.h"

int timer = 100;
int timer1 = 100;

RF24 radio(9, 10);

// Controller Addresses
const uint64_t Controller_2 = 0xE8E8F0F0E1LL;

void setup()
{
  Serial.begin(115000);
  printf_begin();
  pinMode(2, OUTPUT); // Front Work lights
  pinMode(3, OUTPUT); // Rear Work Lights
  pinMode(4, OUTPUT); // Rear Work Lights
  pinMode(5, OUTPUT); // Rear Work Lights
  pinMode(6, OUTPUT); // Rear Work Lights
  pinMode(7, OUTPUT); // Rear Work Lights


  radio.begin(); 
  radio.setDataRate(RF24_250KBPS);
  radio.openReadingPipe(1, Controller_2);
  radio.startListening();
  radio.printDetails();
}

void loop()
{
  if (radio.available())

  {
    radio.read(buf, 4);
    Data_Val_1 = buf[0]; // Store the controller left
    Data_Val_2 = buf[1]; // Store the controller right
  }
  LED_Control();
}

void LED_Control()
{
  if (Data_Val_1 != Old_Data_Val_1)
  {
    if (Data_Val_1 == 1)
    { // Work Lights
      //digitalWrite(2, HIGH); // turn it on
      Serial.println(Data_Val_1);
      blinkLEDLeft();
    }
    else
    {
      digitalWrite(2, LOW); // turn it on
      digitalWrite(3, LOW); // turn it on
      digitalWrite(4, LOW); // turn it on
    }
    Old_Data_Val_1 = Data_Val_1;
  }
  //********************************************************************************//

  if (Data_Val_2 != Old_Data_Val_2)
  {
    if (Data_Val_2 == 1) {
      //digitalWrite(5, HIGH); // turn it on
      Serial.println(Data_Val_2);
      blinkLEDRight();
    }
    else
    {
      digitalWrite(5, LOW); // turn it off
      digitalWrite(6, LOW); // turn it off
      digitalWrite(7, LOW); // turn it off
    }
    Old_Data_Val_2 = Data_Val_2;
  }
}

void blinkLEDLeft()
{
  digitalWrite(2, HIGH);
  delay(timer);
  digitalWrite(2, LOW);
  delay(timer);

  digitalWrite(3, HIGH);
  delay(timer);
  digitalWrite(3, LOW);
  delay(timer);

  digitalWrite(4, HIGH);
  delay(timer);
  digitalWrite(4, LOW);
  delay(timer);

  digitalWrite(2, HIGH);
  digitalWrite(3, HIGH);
  digitalWrite(4, HIGH);
  delay(timer1);

  digitalWrite(2, LOW);
  digitalWrite(3, LOW);
  digitalWrite(4, LOW);
  delay(timer1);

  digitalWrite(2, HIGH);
  digitalWrite(3, HIGH);
  digitalWrite(4, HIGH);
  delay(timer1);

  digitalWrite(2, LOW);
  digitalWrite(3, LOW);
  digitalWrite(4, LOW);
  delay(timer1);
}
void blinkLEDRight()
{
  digitalWrite(5, HIGH);
  delay(timer);
  digitalWrite(5, LOW);
  delay(timer);

  digitalWrite(6, HIGH);
  delay(timer);
  digitalWrite(6, LOW);
  delay(timer);

  digitalWrite(7, HIGH);
  delay(timer);
  digitalWrite(7, LOW);
  delay(timer);

  digitalWrite(5, HIGH);
  digitalWrite(6, HIGH);
  digitalWrite(7, HIGH);
  delay(timer1);

  digitalWrite(5, LOW);
  digitalWrite(6, LOW);
  digitalWrite(7, LOW);
  delay(timer1);

  digitalWrite(5, HIGH);
  digitalWrite(6, HIGH);
  digitalWrite(7, HIGH);
  delay(timer1);

  digitalWrite(5, LOW);
  digitalWrite(6, LOW);
  digitalWrite(7, LOW);
  delay(timer1);
}

destiny2008: Actually sending a Low or high from each pin ( total 4) , so i figured(buff,4)

What does that mean ?

...R

what I mean is when press pin2 it's output is always 1,1,1,1,1,1,1,1,etc...

and when it is off it is sending 0,0,0,0,0,0,0,0,0 etc..

pin2 = 0 ,1 ===> 2 bytes pin3 = o, 1 ====> 2 bytes

or should it be 16 instead of 4??

Respondin to Reply #8

The idea of transmitting two series of 1s or 0s depending on whether two buttons are, or are not pushed is doomed to failure.

First of all, there will only be two chars at any one time 0,0 or 0,1 or 1,0 or 1,1

At the very least put the 2 chars between start and markers such as <0,1> and use the third example in serial input basics to receive them.

…R

thanks Robin, will try it .

the pin2 and pin3 on the TX is to mimic the turn signal voltage coming from a car relay 0V, 12V,0V, 12V and so on until it is always 0V not in use.(OFF)