Good evening everyone!
I would like your help on this small project I am working on and hope to expand in the future, I'm sending ten variables through the nRF24L01 radio transceiver. five values correspond to variable resistors and another five correspond to the state of buttons.
The receiver receives the data without problem and shows it on a LCD 16x2 display. By pressing one button the data I can see the data of one variable and so on just like a menu, and with another button I can see the previous data
The problem is that after some 10 minutes, the values of the potentiometers become zero and the ones of the buttons become negative on the serial monitor which isn't supposed to happen. I have to reset the receiver manually and everything starts working again.
These are the negative values, I receive after a few minutes:
Code of the transmitter circuit:
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
const int pinCE = 9;
const int pinCSN = 10;
RF24 radio(pinCE, pinCSN);
const byte address[5] = "CANAL";
const int PushButton1 = 2;
const int PushButton2 = 3;
const int PushButton3 = 4;
const int PushButton4 = 5;
const int PushButton5 = 6;
int ButtonStateP2 = 0;
int ButtonStateP3 = 0;
int ButtonStateP4 = 0;
int ButtonStateP5 = 0;
int ButtonStateP6 = 0;
float Analog[5];
int Digital[5];
unsigned long LastReading = 0;
void setup()
{
Serial.begin(9600);
radio.begin();
radio.openWritingPipe(address);
radio.stopListening();
pinMode(PB1,INPUT);
pinMode(PB2,INPUT);
pinMode(PB3,INPUT);
pinMode(PB4,INPUT);
pinMode(PB5,INPUT);
}
void loop()
{
if(millis() - LastReading > 1000)
{
bool ok = radio.write(&Analog, sizeof(Analog)) && radio.write(&Digital, sizeof(Digital));
if(ok)
{
POTENTIOMETERS();
BUTTONS();
Serial.println("");
}
else
{
Serial.println("DATA NOT SENT");
}
LastReading = millis();
}
}
void POTENTIOMETERS()
{
int sensorValue1 = analogRead(A0);
float sensorValueResult = sensorValue1 * (5.0/1023.0);
Analog[0] = sensorValueResult;
int sensorValue2 = analogRead(A1);
float sensorValueResult2 = sensorValue2 * (5.0/1023.0);
Analog[1] = sensorValueResult2;
int sensorValue3 = analogRead(A2);
float sensorValueResult3 = sensorValue3 * (5.0/1023.0);
Analog[2] = sensorValueResult3;
int sensorValue4 = analogRead(A3);
float sensorValueResult4 = sensorValue4 * (5.0/1023.0);
Analog[3] = sensorValueResult4;
int sensorValue5 = analogRead(A4);
float sensorValueResult5 = sensorValue5 * (5.0/1023.0);
Analog[4] = sensorValueResult5;
for(int i = 0; i < 5; i++)
{
Serial.print(Analog[i]);
Serial.print(",");
}
}
void BUTTONS()
{
ButtonStateP2 = digitalRead(PushButton1);
Digital[0] = ButtonStateP2;
ButtonStateP3 = digitalRead(PushButton2);
Digital[1] = ButtonStateP3;
ButtonStateP4 = digitalRead(PushButton3);
Digital[2] = ButtonStateP4;
ButtonStateP5 = digitalRead(PushButton4);
Digital[3] = ButtonStateP5;
ButtonStateP6 = digitalRead(PushButton5);
Digital[4] = ButtonStateP6;
for(int j = 0; j < 5; j++)
{
Serial.print(Digital[j]);
Serial.print(",");
}
}
Code of the receiver circuit:
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
#include <LiquidCrystal.h>
LiquidCrystal lcd(2,3,4,5,6,7); /* RS -> PIN 2, ENABLE -> PIN 3, D4 -> PIN 4, D5 -> PIN 5, D6 -> PIN 6, D7 -> PIN 7 */
const int next = A0;
const int previous = A1;
int value_next;
int value_previous;
int value = 0;
int lastButtonState=0;
int lastButtonState2=0;
unsigned long LastReading = 0;
unsigned long LastReadingButton = 0;
unsigned long value_button_next = 0;
unsigned long value_button_previous = 0;
const int Buzzer = 8;
const int pinCE = 9;
const int pinCSN = 10;
RF24 radio(pinCE, pinCSN);
const byte address[5] = "CANAL";
float Analog[5];
int Digital[5];
void setup()
{
Serial.begin(9600);
radio.begin();
radio.openReadingPipe(1, address);
radio.startListening();
lcd.begin(16,2);
lcd.clear();
pinMode(Buzzer,OUTPUT);
}
void loop()
{
if(millis()-LastReading > 1000)
{
if (radio.available())
{
radio.read(&Analog,sizeof(Analog));
radio.read(&Digital,sizeof(Digital));
for(int i = 0; i < 5; i++)
{
Serial.print(Analog[i]);
Serial.print(",");
}
for(int j = 0; j < 5; j++)
{
Serial.print(Digital[j]);
Serial.print(",");
}
Serial.println("");
ALARM();
}
else
{
Serial.println("NO DATA AVAILABLE");
digitalWrite(Buzzer,HIGH);
}
LastReading = millis();
}
MENU_BUTTONS();
}
void MENU_BUTTONS()
{
value_next = analogRead(next);
value_previous = analogRead(previous);
if(value_next != lastButtonState)
{
if(value_next >= 100)
{
if(millis() - value_button_next > 250)
{
value = value + 1;
value_button_next = millis();
}
}
if(value == 10)
{
value = 0;
}
}
lastButtonState = value_next;
if(value_previous != lastButtonState2)
{
if(value_previous >= 100)
{
if(millis() - value_button_previous > 250)
{
value = value - 1;
value_button_previous = millis();
}
}
if(value == -1)
{
value = 9;
}
}
lastButtonState2 = value_previous;
if(millis() - LastReadingButton > 500)
{
if(value == 0)
{
lcd.clear();
lcd.setCursor(0,0);
lcd.print("POT 1");
lcd.setCursor(0,1);
lcd.print(Analog[0]);
}
if(value == 1)
{
lcd.clear();
lcd.setCursor(0,0);
lcd.print("POT 2");
lcd.setCursor(0,1);
lcd.print(Analog[1]);
}
if(value == 2)
{
lcd.clear();
lcd.setCursor(0,0);
lcd.print("POT 3");
lcd.setCursor(0,1);
lcd.print(Analog[2]);
}
if(value == 3)
{
lcd.clear();
lcd.setCursor(0,0);
lcd.print("POT 4");
lcd.setCursor(0,1);
lcd.print(Analog[3]);
}
if(value == 4)
{
lcd.clear();
lcd.setCursor(0,0);
lcd.print("POT 5");
lcd.setCursor(0,1);
lcd.print(Analog[4]);
}
if(value == 5)
{
lcd.clear();
lcd.setCursor(0,0);
lcd.print("BUTTON 1");
if(Digital[0] == 1)
{
lcd.setCursor(0,1);
lcd.print("ON");
}
else
{
lcd.setCursor(0,1);
lcd.print("OFF");
}
}
if(value == 6)
{
lcd.clear();
lcd.setCursor(0,0);
lcd.print("BUTTON 2");
if(Digital[1] == 1)
{
lcd.setCursor(0,1);
lcd.print("ON");
}
else
{
lcd.setCursor(0,1);
lcd.print("OFF");
}
}
if(value == 7)
{
lcd.clear();
lcd.setCursor(0,0);
lcd.print("BUTTON 3");
if(Digital[2] == 1)
{
lcd.setCursor(0,1);
lcd.print("ON");
}
else
{
lcd.setCursor(0,1);
lcd.print("OFF");
}
}
if(value == 8)
{
lcd.clear();
lcd.setCursor(0,0);
lcd.print("BUTTON 4");
if(Digital[3] == 1)
{
lcd.setCursor(0,1);
lcd.print("ON");
}
else
{
lcd.setCursor(0,1);
lcd.print("OFF");
}
}
if(value == 9)
{
lcd.clear();
lcd.setCursor(0,0);
lcd.print("BUTTON 5");
if(Digital[4] == 1)
{
lcd.setCursor(0,1);
lcd.print("ON");
}
else
{
lcd.setCursor(0,1);
lcd.print("OFF");
}
}
LastReadingButton = millis();
}
}
void ALARM()
{
if(Analog[0] > 3.5 || Analog[1] > 3.5 || Analog[2] > 3.5 || Analog[3] > 3.5 || Analog[4] > 3.5 || Digital[3] == 1)
{
digitalWrite(Buzzer,HIGH);
}
else
{
digitalWrite(Buzzer,LOW);
}
}



