Hi, Im new here and am looking for some guidance on a project I'm trying out.
I have a state changing push button to control an output (pin13).
I have a POT for time delay.
once the button is pressed pin 13 goes high, waits for the time delay then pin 13 goes low,
the problem I'm having ;
when the button is pushed ( PIN 13 HIGH, waits for TIMEDELAY , PIN 13 LOW ) ,
while PIN 13 is HIGH if button is pressed send PIN 13 LOW, set TIMEDELAY 0
1. Hardware setup (Fig-1) using Button, Led, Pot1, and UNO
Figure-1:
2. You want that when Button of Fig-1 is pressed, the Led should turn ON. The On-period is determined by the voltage level at A0-pin. If you press Button again when the Led is still ON, the MCU/program will ignore the Button action.
Sketch (tested on UNO)
#define Led 13
void setup()
{
Serial.begin(9600);
pinMode(2, INPUT_PULLUP);
pinMode(Led, OUTPUT);
analogReference(DEFAULT);
}
void loop()
{
unsigned int timeDelay = map(analogRead(A0), 0, 1023, 0, 5000);//(5000/5)*3.3 =3 sec
bool n = digitalRead(2);
if (n == LOW) //Button is closed
{
digitalWrite(Led, HIGH);
delay(timeDelay);
digitalWrite(Led, LOW);
}
}
3. Note that a mechanical swicth like Button of Fig-1 makes a lot of make-and-break (called bouncing) before settling at the final closed position. In such case, we can apply debouncing on the Button using Debounce.h Library. Debouncing does not stop bouncing; rather, it refers to waiting of about 50 ms (as is set in the Debounce.h Library) until the Button settles at the final closed position.
Sketch: (tested on UNO)
#include<Debounce.h>
Debounce Button(2);
#define Led 13
void setup()
{
Serial.begin(9600);
pinMode(2, INPUT_PULLUP);
pinMode(Led, OUTPUT);
analogReference(DEFAULT);
}
void loop()
{
unsigned int timeDelay = map(analogRead(A0), 0, 1023, 0, 5000);//(5000/5)*3.3 =3 sec
bool n = !Button.read(); //redas Button when it is settled at closed position
//Serial.println(n);
if (n == LOW) //Button is closed
{
digitalWrite(Led, HIGH);
delay(timeDelay);
digitalWrite(Led, LOW);
}
}
//https://forum.arduino.cc/t/start-stop-with-delay/908129?
#define PUSHED LOW
#define notPUSHED HIGH
#define LEDon HIGH
#define LEDoff LOW
const byte potPin = A0;
const byte ledPin = 13; //Pin13---[220R]---[A->|-K]---GND
const byte buttonPin = 2; //+5V---[50k internal PU resistor]---[Pin2]---[switch]---GND
byte ledState;
byte buttonStatenew;
byte buttonStateold = notPUSHED;
unsigned int potVal;
unsigned int offTime;
unsigned int offTimeLocked;
unsigned long ledMillis;
unsigned long switchMillis;
//*********************************************************
void setup()
{
Serial.begin(9600);
pinMode (ledPin, OUTPUT);
pinMode (buttonPin, INPUT_PULLUP);
} //END of setup()
//*********************************************************
void loop()
{
//******************************
//is it time to check the switches ?
if (millis() - switchMillis >= 50)
{
//restart the TIMER
switchMillis = millis();
checkSwitches();
}
//******************************
potVal = analogRead(potPin);
offTime = map(potVal, 0, 1023, 1, 60); //set offtime to 1-60
offTime = offTime * 1000; //set offTime to seconds
//******************************
//when the LED is on, is it time to turn it OFF ?
if (ledState == 1 && millis() - ledMillis >= offTimeLocked)
{
//disables TIMER checking
ledState = 0;
digitalWrite(ledPin, LEDoff);
}
//******************************
// Other non blocking code goes here
//******************************
} //END of loop()
//*********************************************************
void checkSwitches()
{
buttonStatenew = digitalRead(buttonPin);
//**********************************
//was there a change in switch state ?
if (buttonStateold != buttonStatenew)
{
//update to the new state
buttonStateold = buttonStatenew;
//*************
//when the LED is OFF, was the switch pushed ?
if (ledState == LEDoff && buttonStatenew == PUSHED)
{
digitalWrite(ledPin, LEDon);
//lock in the delay time
offTimeLocked = offTime;
Serial.print("Delay in ms will be = ");
Serial.println(offTimeLocked);
//enable TIMER checking
ledState = LEDon;
//restart the TIMER
ledMillis = millis();
}
//*************
//when the LED is ON, was the switch pushed ?
else if (ledState == LEDon && buttonStatenew == PUSHED)
{
//cancels the LED timing and turns off the LED
digitalWrite(ledPin, LEDoff);
//disable TIMER checking
ledState = LEDoff;
}
}
} //END of checkSwitches()
“ The OP was asking for a system where the Led will turn on when Button is pressed, and the Led will remain ON until the adjustable tmeDelay is exhausted. The program will ignore the closure of the Button during this timeDelay.
Then should the solution be not straight forward few lines of codes?”
They can remove this section if need be:
//*************
//when the LED is ON, was the switch pushed ?
. . .
You are right.
Sometimes addtional buttons and LEDs suddendly arose.
In this case an Arduino an a sketch written in C++ is more practical solution, isn´t it?