I want to write a while loop that gets activated and runs only once when I press a push button connected to a digital pin; something like "wait for push button and when activated do the rest".
This should work in a way that if I release the push button in the middle of while loop, the while loop should continue till it finishes naturally. The while loop has a condition like this:
while(analogRead(linsense) <= maxdistance)
{
// do something
}
I want to write a while loop that gets activated and runs only once when I press a push button connected to a digital pin; something like "wait for push button and when activated do the rest".
A while loop says to do something over and over, until a condition is met. It is not intended for a one-time operation. An if statement is.
You probably want to consider doing something only when the transition from released to pressed occurs, not just if the switch is pressed. If you hold the switch down, should the task, whatever it is, be repeated until the switch is released, or should it be done just once?
If it is to be done just once, you need to keep track of the previous state of the switch, so that you can detect the transition (the current state does not match the previous state). The current state then defines whether the transition was pressed-to-released or released-to-pressed.
If it is to be done just once, you need to keep track of the previous state of the switch, so that you can detect the transition (the current state does not match the previous state). The current state then defines whether the transition was pressed-to-released or released-to-pressed.
This is exactly what I want to do except only on the released-to-pressed state. I need help on how to do it.
This is exactly what I want to do except only on the released-to-pressed state. I need help on how to do it.
int currState = HIGH;
int prevState = HIGH;
void loop()
{
currState = digitalRead(somePin);
if(currState != prevState)
{
// A transition occurred
if(currState == LOW)
{
// Switch is now pressed
}
else
{
// Switch is now released
}
}
prevState = currState;
}
If you are using pullup resistors, this code should give you an idea. If you are using pulldown resistors, change all LOWs to HIGHs, and all HIGHs to LOWs, and ask yourself why you are using external hardware, when the Arduino has built in (pullup) resistors.
const int buttonPin = 2; // the number of the pushbutton pin
const int ledPin = 13; // the number of the LED pin
// variables will change:
int buttonState = 0; // variable for reading the pushbutton status
void setup() {
// initialize the LED pin as an output:
pinMode(ledPin, OUTPUT);
// initialize the pushbutton pin as an input:
pinMode(buttonPin, INPUT);
}
void loop(){
// read the state of the pushbutton value:
buttonState = digitalRead(buttonPin);
// check if the pushbutton is pressed.
// if it is, the buttonState is HIGH:
if (buttonState == HIGH) {
// turn LED on:
digitalWrite(ledPin, HIGH);
}
else {
// turn LED off:
digitalWrite(ledPin, LOW);
}
}
here is the code of how to read a button state, u need to turn on the loop and off presing a button? like pres 1 times do the loop press it again stop the loop?
here the while loop "do something" if the button is presed and stop if u realease the button
but if u whant to do the while loop once
int state = 0;
buttonState = digitalRead(buttonPin);
while(buttonState == HIGH && state == 0)
{
int = 1;
// do something
}
while(buttonState == LOW && state == 1)
{
int = 0;
// do something or nothing xD
}
something like this? xD u can use "if" instead of "while"
That won't work because there is no way for buttonState to change once in the loop. In other words you have to read buttonState each time through the loop to detect the change.
buttonState = digitalRead(buttonPin);
while(buttonState == HIGH)
{
// do something
buttonState = digitalRead(buttonPin); // read the button
}
or something like this
while(digitalRead(buttonPin) == HIGH)
{
// do something
}
I think you guys are missing the question.
I have a set of instruction that needs to run ONLY ONCE when I have a RISING EDGE on the push button input. I dont care about if the button is on or off. The TRANSITION from low to high (or high to low with pullup resistor) matters.
I already got the answer from PaulS... Thank you Paul.
That won't work because there is no way for buttonState to change once in the loop. In other words you have to read buttonState each time through the loop to detect the change.
buttonState = digitalRead(buttonPin);
while(buttonState == HIGH)
{
// do something
buttonState = digitalRead(buttonPin); // read the button
}
or something like this
while(digitalRead(buttonPin) == HIGH)
{
// do something
}
I have a similiar question. I want to control a led with an button. If i use the "if/else" function it works fine.
Here is the "if/else" code:
void loop() {
int status_taster01 = digitalRead(taster01);
if (status_taster01 == 1){
digitalWrite(blau, HIGH);
}
else {
digitalWrite(blau, LOW);
}
}
the other code with the "while" function doesn't work fine:
Ahh okay. I think now I understand it. Only because the condition for the while-loop is not true the led dont go off from herself. The while-loop is false but I still need to turn off the led after that.
i have a LCD keypad on top of my arduino uno and i want to be able to push the SELECT button on the LCD keypad shield to generate random numbers to be able to simulate rolling the dice on a board game, the problem i'm having is whenever i upload the code to my arduino, it is generating the numbers by itself, plz help and whenever I upload the code to the Arduino I get,
Arduino: 1.8.5 (Windows Store 1.8.10.0) (Windows 10), Board: "Arduino/Genuino Uno"
In file included from C:\Users\taran\Desktop\arduino\demo_simple\demo_simple.ino:3:0:
C:\Users\taran\Desktop\arduino\demo_simple\demo_simple.ino: In function 'void loop()':
C:\Users\taran\Documents\Arduino\libraries\DFR_LCD_Keypad-master/DFR_LCD_Keypad.h:35:35: error: expected '(' before numeric constant
#define DFR_LCD_KEYPAD_KEY_SELECT 4
^
C:\Users\taran\Documents\Arduino\libraries\DFR_LCD_Keypad-master/DFR_LCD_Keypad.h:55:20: note: in expansion of macro 'DFR_LCD_KEYPAD_KEY_SELECT'
#define KEY_SELECT DFR_LCD_KEYPAD_KEY_SELECT
^
C:\Users\taran\Desktop\arduino\demo_simple\demo_simple.ino:31:10: note: in expansion of macro 'KEY_SELECT'
if KEY_SELECT:{
^
demo_simple:50: error: expected '}' at end of input