We have 2 arduino's connected with an NRF24L01. There's 2 buttons on the transmitter to control the receiver. One button turns on the LED's and they change colour based off of the temperature of the room. and a second button to turn off the LED's. The problem is we had to force it by making button_state equal to high at the start of the loop and we're not sure how to make the second button make it low permanently until the ON button is pressed again. Files are attached. Any help is appreciated
Transmitter
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
RF24 radio(9, 10); // CE, CSN
const byte address[6] = "00001"; //Byte of array representing the address. This is the address where we will send the data. This should be same on the receiving side.
int button_pin = 2;
int button_pin2 = 4;
boolean button_state = 0;
boolean button_state2 = 0;
void setup() {
pinMode(button_pin, INPUT);
pinMode(button_pin2, INPUT);
radio.begin(); //Starting the Wireless communication
radio.openWritingPipe(address); //Setting the address where we will send the data
radio.setPALevel(RF24_PA_MAX); //You can set it as minimum or maximum depending on the distance between the transmitter and receiver.
radio.stopListening(); //This sets the module as transmitter
}
void loop()
{
Serial.begin(9600);
button_state = digitalRead(button_pin);
button_state2 = digitalRead(button_pin2);
if (button_state == HIGH)
{
button_state == HIGH;
const char text[] = "LED ON";
radio.write(&text, sizeof(text)); //Sending the message to receive
Serial.println("ON");
}
else if (button_state2 == HIGH)
{
button_state == LOW;
const char text[] = "LED OFF";
radio.write(&text, sizeof(text)); //Sending the message to receiver
Serial.println("OFF");
}
radio.write(&button_state, sizeof(button_state)); //Sending the message to receiver
radio.write(&button_state2, sizeof(button_state2)); //Sending the message to receiver
delay (200);
}
Receiver
//LIBRARIES
#include "RTClib.h"
#include <Wire.h>
#include <LCD.h>
#include <LiquidCrystal_I2C.h>
#include <FastLED.h>
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
//PIN/VARIABLE DEFINITIONS
#define ledPin 4
#define ledNum 50
#define hue 175
#define BACKLIGHT_PIN 3
//VARIABLES
float temp;
CRGB leds[ledNum];
LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);
RTC_DS3231 rtc;
RF24 radio(9, 10);
const byte address[6] = "00001";
boolean button_state = 0;
boolean button_state2 = 0;
char daysOfTheWeek[7][12] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
void setup ()
{
Serial.begin(9600);
FastLED.addLeds<WS2812, ledPin, GRB> (leds, ledNum);
radio.begin();
radio.openReadingPipe(0, address);
radio.setPALevel(RF24_PA_MAX);
radio.startListening();
lcd.begin(20, 4);
lcd.setBacklightPin(BACKLIGHT_PIN, POSITIVE);
lcd.setBacklight(HIGH);
if (! rtc.begin())
{
Serial.println("Couldn't find RTC");
while (1);
}
if (rtc.lostPower())
{
Serial.println("RTC lost power, lets set the time!");
// following line sets the RTC to the date & time this sketch was compiled
rtc.adjust(DateTime(2019, 12, 2, 15, 0, 10));
// This line sets the RTC with an explicit date & time, for example to set
// January 21, 2014 at 3am you would call:
// rtc.adjust(DateTime(2014, 1, 21, 3, 0, 0));
}
}
void loop ()
{
temp = rtc.getTemperature();
DateTime now = rtc.now();
if (radio.available())
{
temp = rtc.getTemperature();
char text[32] = "";
radio.read(&text, sizeof(text));
radio.read(&button_state, sizeof(button_state));
radio.read(&button_state2, sizeof(button_state2));
button_state = HIGH;
if (button_state == HIGH)
{
{
temp = rtc.getTemperature();
char text[32] = "";
radio.read(&text, sizeof(text));
radio.read(&button_state, sizeof(button_state));
radio.read(&button_state2, sizeof(button_state2));
}
// LEDs CHANGE COLORS BASED ON TEMP
if (temp <= 26.00)
{
for (int i = 0; i < 50; i++)
{
temp = rtc.getTemperature();
leds[i] = CRGB(0, 0, hue);
FastLED.show();
delay(10);
}
lcd.setCursor(2, 4);
lcd.print("Status: COLD" );
}
if ((temp > 26.00) && (temp < 29.00))
{
for (int i = 0; i < 50; i++)
{
temp = rtc.getTemperature();
leds[i] = CRGB(0, hue, 0);
FastLED.show();
delay(10);
}
lcd.setCursor(2, 4);
lcd.print("Status: GOOD" );
}
if (temp >= 29.00)
{
for (int i = 0; i < 50; i++)
{
temp = rtc.getTemperature();
leds[i] = CRGB(hue, 0, 0);
FastLED.show();
delay(10);
}
lcd.setCursor(2, 4);
lcd.print("Status: HOT!" );
}
Serial.println("loop 1");
}
if (button_state2 == HIGH)
{
temp = rtc.getTemperature();
char text[32] = "";
radio.read(&text, sizeof(text));
radio.read(&button_state, sizeof(button_state));
radio.read(&button_state2, sizeof(button_state2));
for (int i = 0; i < 50; i++)
{
temp = rtc.getTemperature();
leds[i] = CRGB(0, 0, 0);
FastLED.show();
delay(10);
}
Serial.println("loop 2");
}
//TIME(HOUR, MIN, SEC) LCD DISPLAY
lcd.setCursor(2, 0); //column & row
lcd.print("Time: ");
lcd.print(now.hour(), DEC);
lcd.print(':');
lcd.print(now.minute(), DEC);
lcd.print(':');
lcd.print(now.second(), DEC);
//DATE(MONTH, DAY, YEAR) LCD DISPLAY
lcd.setCursor(2, 1);
lcd.print(daysOfTheWeek[now.dayOfTheWeek()]);
lcd.print(" ");
lcd.print(now.month(), DEC);
lcd.print("/");
lcd.print(now.day(), DEC);
lcd.print("/");
lcd.print(now.year(), DEC);
//TEMP(TEMP) LCD DISPLAY
lcd.setCursor(2, 2);
lcd.print("Temp: ");
lcd.print(rtc.getTemperature());
lcd.print(" C");
delay(200);
}
}
Transmitter.ino (1.5 KB)
Receiver.ino (4.04 KB)
