Output is always HIGH ,except if input goes from HIGH to LOW, and within 2 seconds goes HIGH again.
If time is more than 2 seconds, output remains HIGH, and in every other case, just need to be low if input is HIGH within 2 seconds after switching from HIGH to LOW.
ralerale88:
Output is always HIGH ,except if input goes from HIGH to LOW, and within 2 seconds goes HIGH again.
Does that mean that the output only goes LOW after the input has reverted to HIGH?
If so then I think you need something like this pseudo code
if (input goes HIGH to LOW) {
highLowMillis = millis();
}
if (input goes LOW to HIGH)
if (millis() - highLowMillis) < 2000) {
make output HIGH
}
highLowMillis = 0;
}
Monitor the input for a HIGH to LOW transition. You do this by saving the previous value of the input and every time loop() is called check to see if the current value is LOW and the previous value is HIGH. If there is not a transition then there is nothing to do. If there is a transition then save the millis() time and perform step 2.
Check to see if the current millis() - previous millis() is greater than 2000. If it is not then check for a LOW to HIGH transition. If that transition occurs then set the output to LOW. If it is greater than 2000 then perform step 1 again.
You don't say how long the output should stay LOW.
I maked it this way but something is wrong :shock:
// this constant won't change:
const int buttonPin = 2; // the pin that the pushbutton is attached to
const int ledPin = 13; // the pin that the LED is attached to
// Variables will change:
int buttonState = 0; // current state of the button
int lastButtonState = 0; // previous state of the button
void setup() {
// initialize the button pin as a input:
pinMode(buttonPin, INPUT);
// initialize the LED as an output:
pinMode(ledPin, OUTPUT);
}
void loop()
{
static unsigned long timer = 0;
unsigned long interval = 2000;
// read the pushbutton input pin:
buttonState = digitalRead(buttonPin);
// compare the buttonState to its previous state
if (buttonState != lastButtonState)
{
if (buttonState == LOW && lastButtonState == HIGH)
{
timer = millis();
if (millis() - timer <= interval )
if (buttonState == HIGH && lastButtonState== LOW)
{
digitalWrite(ledPin, LOW);
}
}
else
{
digitalWrite(ledPin, HIGH);
}
}
lastButtonState = buttonState;
}
here is a version of your code with serial-output for analysing what is going on inside your code.
This will help a lot to learn from it.
It uses a macro. Macros are not C++-commands. Macros are pre-processed before compiling.
more how macros works inside the comments of the code.
There are two things important:
give each debgug-output-line a number for easy identifying which line in the serial-monitor shows what
remove the delay after analysing . The delay is just there to slow down serial-output so your eyes can keep up with the output. You may slow it down even more (delay(500)
Use the autoscroll ON/OFF feature of the serial monitor if you have 20 to 30 lines of output to read them.
The serial-output that happens and the serial-output that does not happen will guide you where to change your code
Add some more debuglines at places you want to analyse more closely
//#define debugTxtVar(abc, def) Serial.print("< "#abc" = "); Serial.print(def); Serial.println('>')
// using the #define "statement" to make the compiler "write" code
// #define can be used to make the compiler replace text that describes something with (almost) anything else
// easy example
// let's assume IO-pin number 4 shall be set to work as output
// the command for defining an IO-pin to be configured as output is
//
// pinMode(4,OUTPUT);
// the number "4" says nothing about he purpose of this IO-pin
// let's assume the IO-pin shall switch On/Off a buzzer
// you would have to add a comment to explain
// pinMode(4,OUTPUT); // IO-pin 4 is buzzer
// the compiler needs the number. To make the code easier to read and understand
// #define BuzzerPin 4
// So the command looks like this
// pinMode(BuzzerPin,OUTPUT);
// the descriptive word "BuzzerPin" gets replaced through the compiler by a "4"
// so what the compiler compiles is still 'pinMode(4,OUTPUT);'
// the #define can do much more than just replace a word by a single number
// it can even take parameters like in this example
#define debugTxtVar(myParameterText, variableName) \
Serial.print(#myParameterText " " #variableName"="); \
Serial.println(variableName);
// this constant won't change:
const int buttonPin = 2; // the pin that the pushbutton is attached to
const int ledPin = 13; // the pin that the LED is attached to
// Variables will change:
int buttonState = 0; // current state of the button
int lastButtonState = 0; // previous state of the button
int LoopCnt = 0;
void setup() {
// initialize the button pin as a input:
pinMode(buttonPin, INPUT);
// initialize the LED as an output:
pinMode(ledPin, OUTPUT);
}
void loop() {
LoopCnt++; // just counts up for every iteration of function loop
delay(200); // remove this delay after analysing.
//The delay is just to slow down serial output
static unsigned long timer = 0;
debugTxtVar("000: static unsigned long timer = 0;)", timer);
unsigned long interval = 2000;
// read the pushbutton input pin:
buttonState = digitalRead(buttonPin);
debugTxtVar("00: digitalRead(buttonPin)", buttonState);
debugTxtVar("00b: digitalRead(buttonPin)", lastButtonState);
// compare the buttonState to its previous state
if (buttonState != lastButtonState) {
debugTxtVar("01: buttonState != lastButtonState is true", buttonState);
if (buttonState == LOW && lastButtonState == HIGH) {
timer = millis();
debugTxtVar("02: (buttonState == LOW && lastButtonState == HIGH) is true", buttonState);
if (millis() - timer <= interval ) {
debugTxtVar("03: (millis() - timer <= interval is true )", interval);
if (buttonState == HIGH && lastButtonState == LOW) {
debugTxtVar("04: (buttonState == HIGH && lastButtonState == LOW) is true )", buttonState);
digitalWrite(ledPin, LOW);
}
}
else {
debugTxtVar("05: (buttonState == LOW && lastButtonState == HIGH) is false", buttonState);
debugTxtVar("06: (buttonState == LOW && lastButtonState == HIGH) is false", lastButtonState);
digitalWrite(ledPin, HIGH);
}
}
debugTxtVar("07:LoopCnt", LoopCnt);
lastButtonState = buttonState;
}
}
by the way: now that I have posted this debugging-code two times. I think this is a very good way of enabling newbees to learn on their own. Don't guess what your code is doing print the facts.
Thank you very much guys, especially StefanL38, i menaged to make approximate code, but how to menage my output to stay LOW (if conditions for going LOW are fulfilled) until expires 2 sec from the moment when input goes LOW.
This point in code is: if (buttonState == LOW && lastButtonState == HIGH)
//#define debugTxtVar(abc, def) Serial.print("< "#abc" = "); Serial.print(def); Serial.println('>')
// using the #define "statement" to make the compiler "write" code
// #define can be used to make the compiler replace text that describes something with (almost) anything else
// easy example
// let's assume IO-pin number 4 shall be set to work as output
// the command for defining an IO-pin to be configured as output is
//
// pinMode(4,OUTPUT);
// the number "4" says nothing about he purpose of this IO-pin
// let's assume the IO-pin shall switch On/Off a buzzer
// you would have to add a comment to explain
// pinMode(4,OUTPUT); // IO-pin 4 is buzzer
// the compiler needs the number. To make the code easier to read and understand
// #define BuzzerPin 4
// So the command looks like this
// pinMode(BuzzerPin,OUTPUT);
// the descriptive word "BuzzerPin" gets replaced through the compiler by a "4"
// so what the compiler compiles is still 'pinMode(4,OUTPUT);'
// the #define can do much more than just replace a word by a single number
// it can even take parameters like in this example
#define debugTxtVar(myParameterText, variableName) \
Serial.print(#myParameterText " " #variableName"="); \
Serial.println(variableName);
// this constant won't change:
const int buttonPin = 2; // the pin that the pushbutton is attached to
const int ledPin = 13; // the pin that the LED is attached to
// Variables will change:
int buttonState = 0; // current state of the button
int lastButtonState = 0; // previous state of the button
int LoopCnt = 0;
void setup() {
Serial.begin(9600);
// initialize the button pin as a input:
pinMode(buttonPin, INPUT);
// initialize the LED as an output:
pinMode(ledPin, OUTPUT);
}
void loop() {
LoopCnt++; // just counts up for every iteration of function loop
static unsigned long timer = 0;
debugTxtVar("Tajmer_0: static unsigned long timer = 0;)", timer);
unsigned long interval = 2000;
// read the pushbutton input pin:
buttonState = digitalRead(buttonPin);
debugTxtVar("Ulaz", buttonState);
debugTxtVar("Ulaz_predh", lastButtonState);
// compare the buttonState to its previous state
if (buttonState != lastButtonState) {
debugTxtVar("Ulaz_nije_kao_predh", buttonState);
}
if (buttonState == LOW && lastButtonState == HIGH)
{
timer = millis();
debugTxtVar("Ulaz_0_i_predh_1_pocinje_odbrojavanje", buttonState);
}
if (buttonState == HIGH && lastButtonState == LOW && millis() - timer <= interval)
{
debugTxtVar("Ulaz_1_i_predh_0_i_Vreme_manje_od_2sec)", buttonState);
digitalWrite(ledPin, LOW);
debugTxtVar("Ugasi_izlaz", ledPin);
}
else {
debugTxtVar("Ulaz_NIJE_0_i_predh1_i_Vreme_manje_od_2sec", buttonState);
debugTxtVar("Ulaz_NIJE_0_i_predh1_i_Vreme_manje_od_2sec", lastButtonState);
digitalWrite(ledPin, HIGH);
debugTxtVar("Upali_izlaz", ledPin);
}
debugTxtVar("Brojac ciklusa", LoopCnt);
lastButtonState = buttonState;
}
please post a hand-drawn timing diagram of the input-pin and the output-pin.
You have described something in the first post. But it is not clearly understandable.
A timing diagram is apicture and therefore a universal language that everybody understands
and it is no longer interpretable like words.
look at the debug-output what does the variables have for values
what debuglines get printed ?
Which debuglines get not printed?
at least you can analyse this and post what you have watched.
This is a part of the work that can be done by you and that should be done by you.
To put it in very general terms, your 'if' statements don't "nest" following 'if' statements that should be enclosed by them. Instead you have just put a series of 'if' that only control part of what they are supposed to.
I say this to avoid giving the direct answer, since the problem solution was fully explained in reply #5. Check the wording of that very closely. I feel that this must be a school exercise.