Transient buttons control non-transient behaviour of LEDs
Imagine a system where you have two transient non-exclusive buttons and two LEDs.
When you press button 1, the LED 1 is HIGH and LED 2 is LOW (and stays that way).
When you press button 2, the LED 1 is LOW and LED 2 is HIGH (also stays that way).
Which C++ code allow me, to keep the LED 1 turned on indefinitely (until button 2 is pressed)?
Basically, my problem is, that the buttons are transient and the behaviour of LEDs shouldn't be.
Here is code, that would work for pernament exclusive buttons, but doenst work for transient non-exclusive buttons.
if (digitalRead(button1) == HIGH)
{
led1 = HIGH;
}
else
{
led2 = LOW;
}
if (digitalRead(button2) == HIGH)
{
led2 = HIGH;
}
else
{
led2 = LOW;
}
c++ esp32
migrated from stackoverflow.com Nov 25 '18 at 1:26
This question came from our site for professional and enthusiast programmers.
add a comment |
Imagine a system where you have two transient non-exclusive buttons and two LEDs.
When you press button 1, the LED 1 is HIGH and LED 2 is LOW (and stays that way).
When you press button 2, the LED 1 is LOW and LED 2 is HIGH (also stays that way).
Which C++ code allow me, to keep the LED 1 turned on indefinitely (until button 2 is pressed)?
Basically, my problem is, that the buttons are transient and the behaviour of LEDs shouldn't be.
Here is code, that would work for pernament exclusive buttons, but doenst work for transient non-exclusive buttons.
if (digitalRead(button1) == HIGH)
{
led1 = HIGH;
}
else
{
led2 = LOW;
}
if (digitalRead(button2) == HIGH)
{
led2 = HIGH;
}
else
{
led2 = LOW;
}
c++ esp32
migrated from stackoverflow.com Nov 25 '18 at 1:26
This question came from our site for professional and enthusiast programmers.
AIqbalRaj Choices are in the infinite space of all possible programs.
– GIA_96
Nov 23 '18 at 10:11
can you share the code that you tried and where did you get stuck?
– AIqbalRaj
Nov 23 '18 at 10:55
I edited my original post.
– GIA_96
Nov 23 '18 at 11:02
Is the answer just: "remove else branches and add reset for other led to the same branches"? And for that "attack" this isn't homework resolving facility. Without your code it was just too broad.
– KIIV
Nov 23 '18 at 12:12
add a comment |
Imagine a system where you have two transient non-exclusive buttons and two LEDs.
When you press button 1, the LED 1 is HIGH and LED 2 is LOW (and stays that way).
When you press button 2, the LED 1 is LOW and LED 2 is HIGH (also stays that way).
Which C++ code allow me, to keep the LED 1 turned on indefinitely (until button 2 is pressed)?
Basically, my problem is, that the buttons are transient and the behaviour of LEDs shouldn't be.
Here is code, that would work for pernament exclusive buttons, but doenst work for transient non-exclusive buttons.
if (digitalRead(button1) == HIGH)
{
led1 = HIGH;
}
else
{
led2 = LOW;
}
if (digitalRead(button2) == HIGH)
{
led2 = HIGH;
}
else
{
led2 = LOW;
}
c++ esp32
Imagine a system where you have two transient non-exclusive buttons and two LEDs.
When you press button 1, the LED 1 is HIGH and LED 2 is LOW (and stays that way).
When you press button 2, the LED 1 is LOW and LED 2 is HIGH (also stays that way).
Which C++ code allow me, to keep the LED 1 turned on indefinitely (until button 2 is pressed)?
Basically, my problem is, that the buttons are transient and the behaviour of LEDs shouldn't be.
Here is code, that would work for pernament exclusive buttons, but doenst work for transient non-exclusive buttons.
if (digitalRead(button1) == HIGH)
{
led1 = HIGH;
}
else
{
led2 = LOW;
}
if (digitalRead(button2) == HIGH)
{
led2 = HIGH;
}
else
{
led2 = LOW;
}
c++ esp32
c++ esp32
asked Nov 23 '18 at 9:59
GIA_96GIA_96
41
41
migrated from stackoverflow.com Nov 25 '18 at 1:26
This question came from our site for professional and enthusiast programmers.
migrated from stackoverflow.com Nov 25 '18 at 1:26
This question came from our site for professional and enthusiast programmers.
AIqbalRaj Choices are in the infinite space of all possible programs.
– GIA_96
Nov 23 '18 at 10:11
can you share the code that you tried and where did you get stuck?
– AIqbalRaj
Nov 23 '18 at 10:55
I edited my original post.
– GIA_96
Nov 23 '18 at 11:02
Is the answer just: "remove else branches and add reset for other led to the same branches"? And for that "attack" this isn't homework resolving facility. Without your code it was just too broad.
– KIIV
Nov 23 '18 at 12:12
add a comment |
AIqbalRaj Choices are in the infinite space of all possible programs.
– GIA_96
Nov 23 '18 at 10:11
can you share the code that you tried and where did you get stuck?
– AIqbalRaj
Nov 23 '18 at 10:55
I edited my original post.
– GIA_96
Nov 23 '18 at 11:02
Is the answer just: "remove else branches and add reset for other led to the same branches"? And for that "attack" this isn't homework resolving facility. Without your code it was just too broad.
– KIIV
Nov 23 '18 at 12:12
AIqbalRaj Choices are in the infinite space of all possible programs.
– GIA_96
Nov 23 '18 at 10:11
AIqbalRaj Choices are in the infinite space of all possible programs.
– GIA_96
Nov 23 '18 at 10:11
can you share the code that you tried and where did you get stuck?
– AIqbalRaj
Nov 23 '18 at 10:55
can you share the code that you tried and where did you get stuck?
– AIqbalRaj
Nov 23 '18 at 10:55
I edited my original post.
– GIA_96
Nov 23 '18 at 11:02
I edited my original post.
– GIA_96
Nov 23 '18 at 11:02
Is the answer just: "remove else branches and add reset for other led to the same branches"? And for that "attack" this isn't homework resolving facility. Without your code it was just too broad.
– KIIV
Nov 23 '18 at 12:12
Is the answer just: "remove else branches and add reset for other led to the same branches"? And for that "attack" this isn't homework resolving facility. Without your code it was just too broad.
– KIIV
Nov 23 '18 at 12:12
add a comment |
2 Answers
2
active
oldest
votes
Something like this:
if (digitalRead(button1) == HIGH)
{
led1 = HIGH;
led2 = LOW;
}
if (digitalRead(button2) == HIGH)
{
led2 = HIGH;
led1 = LOW;
}
Hello Mike, thanks. It is correct answer for my question, but I failed to mention, this was just small piece of the program and in that case, I might be better off using interrupts. If I am correct, the loop has to do full cycle, before it reaches button1 again. Thanks, anyway
– GIA_96
Nov 23 '18 at 13:21
add a comment |
Don't react to the button by changing the LED; change a state-variable in memory, instead. That saves the information of which button was most recently pressed. Then, each time through your loop, set your LED's according to the contents of that persistent variable. Remember that it will have to be a static variable (global or static local), as an automatic one will disappear and reappear after each exit and re-entry to loop(), and you cannot count on its contents when that happens.
Update:
How do I prevent overwriting the state variable with different input ?
You don't prevent it - you must over-write it. The purpose of the state variable is to remember which button was last pressed. I had in mind something like the following, where lastButton
saves the number of the most recent button:
void setup(){
pinmode(LED1PIN, OUTPUT);
pinmode(LED2PIN, OUTPUT);
pinmode(BUTN1PIN, INPUT_PULLUP); // you might not want the pullup
pinmode(BUTN2PIN, INPUT_PULLUP); // depending on your wiring.
}
void loop(){
static uint8_t lastButton = 0;
// Save the number of the last button pressed.
if (digitalRead(button1) == HIGH)
lastButton = 1;
else if (digitalRead(button2) == HIGH)
lastButton = 2;
// Set the LEDs according to most recent button
// Note that at program-start, when no button has
// been pressed yet, this code leaves both LED's off.
if (lastButton == 1)
{
digitalWrite(LED1PIN, HIGH);
digitalWrite(LED2PIN, LOW);
}
else if(lastButton == 2)
digitalWrite(LED1PIN, LOW);
digitalWrite(LED2PIN, HIGH);
}
}
Thanks, but how do I prevent the corruption of the variable, by pressing another button ? Lets say, I make code in which, something is gonna happen after b1 (state-variable) is true. That is gonna send me into while loop, but when you as user press Button2, b1 gets reseted to false and the loop will be exited ?. How do I prevent overwriting the state variable with different input ?
– GIA_96
Nov 28 '18 at 15:59
I might make my question more intelligible and make another thread. I am not sure, if its clear enough.
– GIA_96
Nov 28 '18 at 16:04
add a comment |
StackExchange.ifUsing("editor", function () {
return StackExchange.using("schematics", function () {
StackExchange.schematics.init();
});
}, "cicuitlab");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "540"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2farduino.stackexchange.com%2fquestions%2f58158%2ftransient-buttons-control-non-transient-behaviour-of-leds%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
Something like this:
if (digitalRead(button1) == HIGH)
{
led1 = HIGH;
led2 = LOW;
}
if (digitalRead(button2) == HIGH)
{
led2 = HIGH;
led1 = LOW;
}
Hello Mike, thanks. It is correct answer for my question, but I failed to mention, this was just small piece of the program and in that case, I might be better off using interrupts. If I am correct, the loop has to do full cycle, before it reaches button1 again. Thanks, anyway
– GIA_96
Nov 23 '18 at 13:21
add a comment |
Something like this:
if (digitalRead(button1) == HIGH)
{
led1 = HIGH;
led2 = LOW;
}
if (digitalRead(button2) == HIGH)
{
led2 = HIGH;
led1 = LOW;
}
Hello Mike, thanks. It is correct answer for my question, but I failed to mention, this was just small piece of the program and in that case, I might be better off using interrupts. If I am correct, the loop has to do full cycle, before it reaches button1 again. Thanks, anyway
– GIA_96
Nov 23 '18 at 13:21
add a comment |
Something like this:
if (digitalRead(button1) == HIGH)
{
led1 = HIGH;
led2 = LOW;
}
if (digitalRead(button2) == HIGH)
{
led2 = HIGH;
led1 = LOW;
}
Something like this:
if (digitalRead(button1) == HIGH)
{
led1 = HIGH;
led2 = LOW;
}
if (digitalRead(button2) == HIGH)
{
led2 = HIGH;
led1 = LOW;
}
answered Nov 23 '18 at 12:42
MikeMike
1162
1162
Hello Mike, thanks. It is correct answer for my question, but I failed to mention, this was just small piece of the program and in that case, I might be better off using interrupts. If I am correct, the loop has to do full cycle, before it reaches button1 again. Thanks, anyway
– GIA_96
Nov 23 '18 at 13:21
add a comment |
Hello Mike, thanks. It is correct answer for my question, but I failed to mention, this was just small piece of the program and in that case, I might be better off using interrupts. If I am correct, the loop has to do full cycle, before it reaches button1 again. Thanks, anyway
– GIA_96
Nov 23 '18 at 13:21
Hello Mike, thanks. It is correct answer for my question, but I failed to mention, this was just small piece of the program and in that case, I might be better off using interrupts. If I am correct, the loop has to do full cycle, before it reaches button1 again. Thanks, anyway
– GIA_96
Nov 23 '18 at 13:21
Hello Mike, thanks. It is correct answer for my question, but I failed to mention, this was just small piece of the program and in that case, I might be better off using interrupts. If I am correct, the loop has to do full cycle, before it reaches button1 again. Thanks, anyway
– GIA_96
Nov 23 '18 at 13:21
add a comment |
Don't react to the button by changing the LED; change a state-variable in memory, instead. That saves the information of which button was most recently pressed. Then, each time through your loop, set your LED's according to the contents of that persistent variable. Remember that it will have to be a static variable (global or static local), as an automatic one will disappear and reappear after each exit and re-entry to loop(), and you cannot count on its contents when that happens.
Update:
How do I prevent overwriting the state variable with different input ?
You don't prevent it - you must over-write it. The purpose of the state variable is to remember which button was last pressed. I had in mind something like the following, where lastButton
saves the number of the most recent button:
void setup(){
pinmode(LED1PIN, OUTPUT);
pinmode(LED2PIN, OUTPUT);
pinmode(BUTN1PIN, INPUT_PULLUP); // you might not want the pullup
pinmode(BUTN2PIN, INPUT_PULLUP); // depending on your wiring.
}
void loop(){
static uint8_t lastButton = 0;
// Save the number of the last button pressed.
if (digitalRead(button1) == HIGH)
lastButton = 1;
else if (digitalRead(button2) == HIGH)
lastButton = 2;
// Set the LEDs according to most recent button
// Note that at program-start, when no button has
// been pressed yet, this code leaves both LED's off.
if (lastButton == 1)
{
digitalWrite(LED1PIN, HIGH);
digitalWrite(LED2PIN, LOW);
}
else if(lastButton == 2)
digitalWrite(LED1PIN, LOW);
digitalWrite(LED2PIN, HIGH);
}
}
Thanks, but how do I prevent the corruption of the variable, by pressing another button ? Lets say, I make code in which, something is gonna happen after b1 (state-variable) is true. That is gonna send me into while loop, but when you as user press Button2, b1 gets reseted to false and the loop will be exited ?. How do I prevent overwriting the state variable with different input ?
– GIA_96
Nov 28 '18 at 15:59
I might make my question more intelligible and make another thread. I am not sure, if its clear enough.
– GIA_96
Nov 28 '18 at 16:04
add a comment |
Don't react to the button by changing the LED; change a state-variable in memory, instead. That saves the information of which button was most recently pressed. Then, each time through your loop, set your LED's according to the contents of that persistent variable. Remember that it will have to be a static variable (global or static local), as an automatic one will disappear and reappear after each exit and re-entry to loop(), and you cannot count on its contents when that happens.
Update:
How do I prevent overwriting the state variable with different input ?
You don't prevent it - you must over-write it. The purpose of the state variable is to remember which button was last pressed. I had in mind something like the following, where lastButton
saves the number of the most recent button:
void setup(){
pinmode(LED1PIN, OUTPUT);
pinmode(LED2PIN, OUTPUT);
pinmode(BUTN1PIN, INPUT_PULLUP); // you might not want the pullup
pinmode(BUTN2PIN, INPUT_PULLUP); // depending on your wiring.
}
void loop(){
static uint8_t lastButton = 0;
// Save the number of the last button pressed.
if (digitalRead(button1) == HIGH)
lastButton = 1;
else if (digitalRead(button2) == HIGH)
lastButton = 2;
// Set the LEDs according to most recent button
// Note that at program-start, when no button has
// been pressed yet, this code leaves both LED's off.
if (lastButton == 1)
{
digitalWrite(LED1PIN, HIGH);
digitalWrite(LED2PIN, LOW);
}
else if(lastButton == 2)
digitalWrite(LED1PIN, LOW);
digitalWrite(LED2PIN, HIGH);
}
}
Thanks, but how do I prevent the corruption of the variable, by pressing another button ? Lets say, I make code in which, something is gonna happen after b1 (state-variable) is true. That is gonna send me into while loop, but when you as user press Button2, b1 gets reseted to false and the loop will be exited ?. How do I prevent overwriting the state variable with different input ?
– GIA_96
Nov 28 '18 at 15:59
I might make my question more intelligible and make another thread. I am not sure, if its clear enough.
– GIA_96
Nov 28 '18 at 16:04
add a comment |
Don't react to the button by changing the LED; change a state-variable in memory, instead. That saves the information of which button was most recently pressed. Then, each time through your loop, set your LED's according to the contents of that persistent variable. Remember that it will have to be a static variable (global or static local), as an automatic one will disappear and reappear after each exit and re-entry to loop(), and you cannot count on its contents when that happens.
Update:
How do I prevent overwriting the state variable with different input ?
You don't prevent it - you must over-write it. The purpose of the state variable is to remember which button was last pressed. I had in mind something like the following, where lastButton
saves the number of the most recent button:
void setup(){
pinmode(LED1PIN, OUTPUT);
pinmode(LED2PIN, OUTPUT);
pinmode(BUTN1PIN, INPUT_PULLUP); // you might not want the pullup
pinmode(BUTN2PIN, INPUT_PULLUP); // depending on your wiring.
}
void loop(){
static uint8_t lastButton = 0;
// Save the number of the last button pressed.
if (digitalRead(button1) == HIGH)
lastButton = 1;
else if (digitalRead(button2) == HIGH)
lastButton = 2;
// Set the LEDs according to most recent button
// Note that at program-start, when no button has
// been pressed yet, this code leaves both LED's off.
if (lastButton == 1)
{
digitalWrite(LED1PIN, HIGH);
digitalWrite(LED2PIN, LOW);
}
else if(lastButton == 2)
digitalWrite(LED1PIN, LOW);
digitalWrite(LED2PIN, HIGH);
}
}
Don't react to the button by changing the LED; change a state-variable in memory, instead. That saves the information of which button was most recently pressed. Then, each time through your loop, set your LED's according to the contents of that persistent variable. Remember that it will have to be a static variable (global or static local), as an automatic one will disappear and reappear after each exit and re-entry to loop(), and you cannot count on its contents when that happens.
Update:
How do I prevent overwriting the state variable with different input ?
You don't prevent it - you must over-write it. The purpose of the state variable is to remember which button was last pressed. I had in mind something like the following, where lastButton
saves the number of the most recent button:
void setup(){
pinmode(LED1PIN, OUTPUT);
pinmode(LED2PIN, OUTPUT);
pinmode(BUTN1PIN, INPUT_PULLUP); // you might not want the pullup
pinmode(BUTN2PIN, INPUT_PULLUP); // depending on your wiring.
}
void loop(){
static uint8_t lastButton = 0;
// Save the number of the last button pressed.
if (digitalRead(button1) == HIGH)
lastButton = 1;
else if (digitalRead(button2) == HIGH)
lastButton = 2;
// Set the LEDs according to most recent button
// Note that at program-start, when no button has
// been pressed yet, this code leaves both LED's off.
if (lastButton == 1)
{
digitalWrite(LED1PIN, HIGH);
digitalWrite(LED2PIN, LOW);
}
else if(lastButton == 2)
digitalWrite(LED1PIN, LOW);
digitalWrite(LED2PIN, HIGH);
}
}
edited Nov 28 '18 at 20:57
answered Nov 25 '18 at 15:59
JRobertJRobert
10.2k21237
10.2k21237
Thanks, but how do I prevent the corruption of the variable, by pressing another button ? Lets say, I make code in which, something is gonna happen after b1 (state-variable) is true. That is gonna send me into while loop, but when you as user press Button2, b1 gets reseted to false and the loop will be exited ?. How do I prevent overwriting the state variable with different input ?
– GIA_96
Nov 28 '18 at 15:59
I might make my question more intelligible and make another thread. I am not sure, if its clear enough.
– GIA_96
Nov 28 '18 at 16:04
add a comment |
Thanks, but how do I prevent the corruption of the variable, by pressing another button ? Lets say, I make code in which, something is gonna happen after b1 (state-variable) is true. That is gonna send me into while loop, but when you as user press Button2, b1 gets reseted to false and the loop will be exited ?. How do I prevent overwriting the state variable with different input ?
– GIA_96
Nov 28 '18 at 15:59
I might make my question more intelligible and make another thread. I am not sure, if its clear enough.
– GIA_96
Nov 28 '18 at 16:04
Thanks, but how do I prevent the corruption of the variable, by pressing another button ? Lets say, I make code in which, something is gonna happen after b1 (state-variable) is true. That is gonna send me into while loop, but when you as user press Button2, b1 gets reseted to false and the loop will be exited ?. How do I prevent overwriting the state variable with different input ?
– GIA_96
Nov 28 '18 at 15:59
Thanks, but how do I prevent the corruption of the variable, by pressing another button ? Lets say, I make code in which, something is gonna happen after b1 (state-variable) is true. That is gonna send me into while loop, but when you as user press Button2, b1 gets reseted to false and the loop will be exited ?. How do I prevent overwriting the state variable with different input ?
– GIA_96
Nov 28 '18 at 15:59
I might make my question more intelligible and make another thread. I am not sure, if its clear enough.
– GIA_96
Nov 28 '18 at 16:04
I might make my question more intelligible and make another thread. I am not sure, if its clear enough.
– GIA_96
Nov 28 '18 at 16:04
add a comment |
Thanks for contributing an answer to Arduino Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2farduino.stackexchange.com%2fquestions%2f58158%2ftransient-buttons-control-non-transient-behaviour-of-leds%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
AIqbalRaj Choices are in the infinite space of all possible programs.
– GIA_96
Nov 23 '18 at 10:11
can you share the code that you tried and where did you get stuck?
– AIqbalRaj
Nov 23 '18 at 10:55
I edited my original post.
– GIA_96
Nov 23 '18 at 11:02
Is the answer just: "remove else branches and add reset for other led to the same branches"? And for that "attack" this isn't homework resolving facility. Without your code it was just too broad.
– KIIV
Nov 23 '18 at 12:12