This is Arduino NANO & NRF24L01 is used remote controller's transmitter side code . This remote control has a single push button that is ordinarily grounded to earth (low), but when pressed, sends a HIGH signal to the RX receiver.
This working perfectly.
To prevent the push button switch from being denounced, I need to use Millis(). Please assist me in inserting the millis() method into this code because I don't want to utilize delay().
Here is my TX Code using millis() but its not working!
#include <SPI.h>
#include "nRF24L01.h"
#include "RF24.h"
#define CE_PIN 9
#define CSN_PIN 10
// NOTE: the "LL" at the end of the constant is "LongLong" type
const uint64_t pipe = 0xE8E8F0F0E1LL; // Define the transmit pipe
/*-----( Declare objects )-----*/
RF24 radio(CE_PIN, CSN_PIN); // Create a Radio
/*-----( Declare Variables )-----*/
int joystick[2]; // 10 element array holding Joystick reading and 4 buttons
int LadderSw = 6;
boolean TimePeriodIsOver;
unsigned long expireTime=0;
unsigned long TimePeriod=5000;
unsigned long currentMillis;
byte LastState=LOW;
void setup() {
Serial.begin(9600);
radio.begin();
radio.openWritingPipe(pipe);
pinMode(LadderSw, INPUT_PULLUP);
}
void loop() {
radio.stopListening();
if((millis()-expireTime) > TimePeriod){
joystick[0] = digitalRead(LadderSw);
if(joystick[0]!=LastState){
expireTime=millis();
LastState=joystick[0];
if(joystick[0]==HIGH)
{
radio.write( joystick, sizeof(joystick));
Serial.print(" Ladder Switch PRESSED ");
Serial.println(digitalRead(LadderSw));
}
}
}
//Serial.print ("-----");
//Serial.println ((currentMillis-expireTime));
}
so the wiring should be pin 6 <---> switch <---> GND
and the state is HIGH when released and LOW when pressed
To your question, there are countless button tutorials to deal with bouncing. here for the basics of wiring the button if you don't use the internal pullup
I have made one code for toggle the LED with using millis() function.
Push & hold the button for 2500msec LED will ON and Again push and Hold the Button for 2500msec to OFF the LED
Here is my code, which is working fine. Please let me know if the method I used to create this code is correct or not?
//Push&hold the button for 2500msec LED will ON and Again push and Hold the Button for 2500msec to OFF the LED////
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////
const byte BUTTON = 2; // our button pin
const byte LED = 13; // LED (built-in on Uno)
unsigned long TimeElapse = 2500; // wait to turn on LED
unsigned long LedChangeState; // Store the time after press the button
boolean ledState = false; //set the flag Only one time do the functio
boolean State = LOW;//led state
byte LastState = HIGH;//Button last position
byte ButtoncurrentState;//current button state
void setup() {
Serial.begin(9600);
pinMode(BUTTON, INPUT_PULLUP);
pinMode(LED, OUTPUT);
digitalWrite(LED, LOW);
}
void loop() {
unsigned long currentMillis = millis();
ButtoncurrentState = digitalRead(BUTTON);
if (ButtoncurrentState == LastState) {
LedChangeState = currentMillis;
ledState = false;
}
if (ButtoncurrentState == LOW && (unsigned long)(currentMillis - LedChangeState) >= TimeElapse && ledState == false) {
State = !State;
digitalWrite(LED, State);
ledState = true;
}
}
consider following which handles multiple buttons and LEDs using arrays
consider using an array to capture the timestamp that a button is pressed and whether it is still pressed after 2.5 sec before reporting it pressed in checkButtons (), when does the timestamp get resets and whether the button has been reported as pressed
// check multiple buttons and toggle LEDs
enum { Off = HIGH, On = LOW };
byte pinsLed [] = { 10, 11, 12 };
byte pinsBut [] = { A1, A2, A3 };
#define N_BUT sizeof(pinsBut)
byte butState [N_BUT];
// -----------------------------------------------------------------------------
int
chkButtons ()
{
for (unsigned n = 0; n < sizeof(pinsBut); n++) {
byte but = digitalRead (pinsBut [n]);
if (butState [n] != but) {
butState [n] = but;
delay (10); // debounce
if (On == but)
return n;
}
}
return -1;
}
// -----------------------------------------------------------------------------
void
loop ()
{
switch (chkButtons ()) {
case 2:
digitalWrite (pinsLed [2], ! digitalRead (pinsLed [2]));
break;
case 1:
digitalWrite (pinsLed [1], ! digitalRead (pinsLed [1]));
break;
case 0:
digitalWrite (pinsLed [0], ! digitalRead (pinsLed [0]));
break;
}
}
// -----------------------------------------------------------------------------
void
setup ()
{
Serial.begin (9600);
for (unsigned n = 0; n < sizeof(pinsBut); n++) {
pinMode (pinsBut [n], INPUT_PULLUP);
butState [n] = digitalRead (pinsBut [n]);
}
for (unsigned n = 0; n < sizeof(pinsLed); n++) {
digitalWrite (pinsLed [n], Off);
pinMode (pinsLed [n], OUTPUT);
}
}