Arduino not working correctly after 30 seconds

My code seems to turn on the correct light according to my other wireless modules, but after around 30 seconds the Touch L+R lights no longer turn on. I'm using the millis() function to process their timing and I included a debug light to turn on after 30 seconds. Resetting the arduino after 30 seconds makes it work properly again for another 30 seconds, but I cant keep resetting it during use.

I know the code has octal in it, but that is what the nrf module uses so removing it isn't really an option

NRF24L01

#include "nRF24L01.h" // NRF24L01 library created by TMRh20 https://github.com/TMRh20/RF24
#include "RF24.h"
#include "SPI.h"

#define TouchLeft 4
#define BellLeft 5
#define BellRight 6
#define TouchRight 7
#define debug 3

int LTimer;
int RTimer;

int ReceivedMessage[1] = {000}; // Used to store value received by the NRF24L01
RF24 radio(9, 10); // NRF24L01 SPI pins. Pin 9 and 10 on the Nano

const uint64_t pipe = 0xE6E6E6E6E6E6; // Needs to be the same for communicating between 2 NRF24L01

void setup(void)
{
  radio.begin(); // Start the NRF24L01

  radio.openReadingPipe(1, pipe); // Get NRF24L01 ready to receive

  radio.startListening(); // Listen to see if information received

  pinMode(TouchRight, OUTPUT);
  pinMode(BellRight, OUTPUT);
  pinMode(BellLeft, OUTPUT);
  pinMode(TouchLeft, OUTPUT);
  pinMode(debug, OUTPUT);

  LTimer = 0;
  RTimer = 0;
}

void loop(void)
{
  while (radio.available())
  {
    radio.read(ReceivedMessage, 1); // Read information from the NRF24L01

    if (ReceivedMessage[0] == 110) // Indicates switch is pressed
    {
      digitalWrite(BellLeft, HIGH);
    }
    else if (ReceivedMessage[0] == 111)
    {
      TouchL();
    }
    else if (ReceivedMessage[0] == 100) // Indicates switch is pressed
    {
      digitalWrite(BellRight, HIGH);
    }
    else if (ReceivedMessage[0] == 011)
    {
      TouchR();
    }
    else
    {
      digitalWrite(TouchLeft, LOW);
      digitalWrite(BellLeft, LOW);
      digitalWrite(BellRight, LOW);
      digitalWrite(TouchRight, LOW);
    }
    if (millis() - LTimer < 1500)
    {
      digitalWrite(TouchLeft, HIGH);
    }
    else
    {
      LTimer = 0;
    }
    if (millis() - RTimer < 1500)
    {
      digitalWrite(TouchRight, HIGH);
    }
    else
    {
      RTimer = 0;
    }
    if (millis() > 30000)
    {
      digitalWrite(debug, HIGH);
    }
  }
}

void TouchR()
{
  if (millis() - LTimer <= 40)
  {
    RTimer = millis();
    LTimer = millis();
  }
  else if (RTimer > 0)
  {
    RTimer = millis();
  }
  else
  {
    RTimer = millis();
  }
}

void TouchL()
{
  if (millis() - RTimer <= 40)
  {
    RTimer = millis();
    LTimer = millis();
  }
  else if (LTimer > 0)
  {
    LTimer = millis();
  }
  else
  {
    LTimer = millis();
  }
}

Any help would be much appreciated

Link to proposed schematic
https://www.tinkercad.com/things/blMheWu7aHD-copy-of-fencing-scoring-box-new-schematic/editel?sharecode=HIKC0aHj59kcMyJ3eVXLa2RCzhCf86hg_nd5BUEvUkg

32.767 seconds, perhaps?

2 Likes

32767 looks like a familiar number--INT_MAX on an Arduino int - Arduino Reference

Hint: millis does not return int.

1 Like

Thank you. So using a long instead of an int should work then?

It'll work for longer.
Hint: millis doesn't return long.

2 Likes

https://www.arduino.cc/reference/en/language/functions/time/millis/ -- unsigned long would work best.

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