Hey guys!
I just started playing with arduino, and i have to the solve the following problem. If i press a button, an output have to stay high for one second, then go off, no matter how long i press the button, and must not to go high again until i release the button, and press again. Right now it's working with a delay, but it's not really good. Any help would be appreciated.
Thank you!
Hi, try the following:
if(digitalRead(button, HIGH){
digitalWrite(LED, HIGH);
delay(1000);
digitalWrite(LED,LOW);
while(digitalRead(button, HIGH);
}
It says too many arguments ito function 'int digitalRead (uint8_t)
int LED = 13;
int buttonPin = 12;
void setup() {
pinMode(buttonPin, INPUT_PULLUP);
pinMode(LED, OUTPUT);
}
void loop(){
int buttonValue = digitalRead(buttonPin);
if(digitalRead(buttonPin, LOW )
digitalWrite(LED, HIGH);
delay(1000);
digitalWrite(LED,LOW);
while(digitalRead(button, LOW);
}
else {
// Otherwise, turn the LED off
digitalWrite(LED, LOW);
}
Sorry my fault...
it has to be:
if(digitalRead(button)){
digitalWrite(LED, HIGH);
delay(1000);
digitalWrite(LED,LOW);
while(digitalRead(button));
}
Don't use delay() as it blocks the Arduino from doing other things.
The best way to do something like this is to check when the button becomes pressed and start a timer. Something like this ...
prevButtonState = buttonState; // save the previous state
buttonState = digitaRead(buttonPin);
if (buttonState != prevButtonState and buttonState = LOW) { // assumes LOW when pressed
ledStartTime = millis();
digitalWrite(ledpin, HIGH);
}
if (millis() - ledStartMIlls() > ledOnInterval) {
digitalWrite(ledPin, LOW);
}
...R
Linze99:
Sorry my fault...it has to be:
if(digitalRead(button)){
digitalWrite(LED, HIGH);
delay(1000);
digitalWrite(LED,LOW);
while(digitalRead(button));
}
Yeah, i tried that but still...
Fercsi4646:
If i press a button, an output have to stay high for one second, then go off, no matter how long i press the button, and must not to go high again until i release the button, and press again.
Referencing IDE -> file/examples/digital/stateChangeDetection
Use this to generate a signal which is active for only one pass through the code when it changes.
You can verify you've done it right by using the change to increment a counter and displaying the count value on the serial monitor.
Use the fact of the change (some variable, like buttonWasPressed) to reset a timer.
As long as the timer accumulated value is less than what you've set as a preset/timed out value, make the output high. Else make the output low.