Hello,
what i have in my circuit is 2 push buttons, the first for starting a timer of 3 seconds which then when the timer is done it sends a 1 to the NRF24L01 which turn a relay on for a specific time which is 3 seconds then turn it off by sending 0 to NRF24L01. The second button is reset button which if the timer did not finish, it prevents the 1 from being sent through the NRF24L01. My connection are good I tested them separately and they are working well and my NRF24L01 code is good it sends data that i want, my only problem is how to stop the timer from being executed if the 1 has not been sent means if the relay is not on but if the relay is on and i clicked reset, I don't want this reset to stop the relay which is working to finish it's time which is 3 seconds on.
here is my code so far:
Moderators note : used code tags
what i have in my circuit is 2 push buttons, the first for starting a timer of 3 seconds which then when the timer is done it sends a 1 to the NRF24L01 which turn a relay on for a specific time which is 3 seconds then turn it off by sending 0 to NRF24L01. The second button is reset button which if the timer did not finish, it prevents the 1 from being sent through the NRF24L01. My connection are good I tested them separately and they are working well and my NRF24L01 code is good it sends data that i want, my only problem is how to stop the timer from being executed if the 1 has not been sent means if the relay is not on but if the relay is on and i clicked reset, I don't want this reset to stop the relay which is working to finish it's time which is 3 seconds on.
here is my code so far:
C:
#include <SPI.h>
#include <RF24.h>
#include <RF24Network.h>
RF24 radio(9, 10); // nRF24L01 (CE,CSN)
RF24Network network(radio); // Include the radio in the network
const uint16_t this_node = 00; // Address of this node in Octal format ( 04,031, etc)
const uint16_t node01 = 01;
int relayButtonPin = A2;
int resetButton = 8;
boolean lastRelay5_pb = LOW;
boolean currentRelay5_pb = LOW;
boolean relay5PushBOn = false;
int relay5NumClicks = 0;
boolean relay5Working = false;
int relay5Data;
boolean okToPrint = true;
boolean lastReset = LOW;
boolean currentReset = LOW;
boolean resetPushBOn = false;
int resetNumClicks = 0;
boolean resetWorking = false;
unsigned long interval = 3000;
unsigned long startClickTime;
unsigned long startResetTime;
void setup()
{
// put your setup code here, to run once:
Serial.begin(9600);
pinMode(relayButtonPin, INPUT);
SPI.begin();
radio.begin();
network.begin(90, this_node); //(channel, node address)
radio.setDataRate(RF24_2MBPS);
}
void loop()
{
// This is for the Relay 5 Push button connected to A2 ---------------------------------------
currentRelay5_pb = debounce(lastRelay5_pb, relayButtonPin);
if (lastRelay5_pb == LOW && currentRelay5_pb == HIGH)
{
relay5PushBOn = !relay5PushBOn;
relay5NumClicks++;
okToPrint = true;
}
lastRelay5_pb = currentRelay5_pb;
if(relay5Working == false && relay5NumClicks >= 2){
relay5Working = true;
startClickTime = millis();
relay5Data = 1;
}
if(relay5Working == true){
relay5NumClicks = 0;
}
// This is for reset button
currentReset = debounce(lastReset, resetButton);
if (lastReset == LOW && currentReset == HIGH)
{
resetPushBOn = !resetPushBOn;
resetNumClicks++;
}
lastReset = currentReset;
if(resetWorking == false && resetNumClicks >= 1){
resetWorking = true;
okToPrint = false;
startResetTime = millis();
relay5Data = 0;
}
if(resetWorking == true){
resetNumClicks = 0;
}
Serial.print("Print: ");
Serial.println(relay5Data);
if(millis() - startResetTime > interval){
okToPrint = true;
}
if (millis() - startClickTime > interval){
if (okToPrint)
{
Serial.print("ok send: ");
Serial.println(relay5Data);
}
}
bool ok1 = network.write(header1, &relay5Data, sizeof(relay5Data)); // Send the data
}
// Debouncing function
boolean debounce(boolean last, int switchPin)
{
boolean current = digitalRead(switchPin);
if (last != current)
{
delay(5);
current = digitalRead(switchPin);
}
return current;
}
Last edited by a moderator: