Using switch to select a case with pushbutton — toggle
I am working on a toggle using switch case. Not sure if I'm doing it right. My intention should be fairly obvious through my code example.
I want to increment a counter with a pushbutton use the x++ function and some toggle code I have saved that should be usable in a switch(case) statement to produce desired outcomes at a particular case by toggling an output pin on upon case selection and off when case is no longer valid. Each case would have its own output pin with a specific case loop to execute when that case becomes valid. I can write the output loops once I get the toggle mode working. There are some missing pieces to the code. I have no formal coding experience and have only taught myself a little bit here and there. I've started with my Basic Stamp Homework Board and switched over from BS2 to Arduino. My goal is to compile the codes that I need for basic projects and save it. Then I can realistically be done with base-coding and focus on connecting hardware and fine-tuning the hardware:software.
Not quite sure how to use this switch function. Wondering if I need to have a FOR statement somewhere in there to increment my counter. Was thinking I could increment the counter each time the toggle1 value flipped or something to that effect. Another though would be to utilize switch(case) by modulo, toggling case values by pressing the button a specific number of times. 1 press would be case 1, 2 press -- case 2, 3 press -- case 3, etc... Case 0 could be 5 presses and be the last cycle using the ++x operator. Toggling case using pushbutton and ++ should be a very very simple matter. I have very little experience with programming. Would like some guidance. I hope that the gist of my idea is all in the code. Thank you.
const int btn = 22;
const int led = 3;
int selector1 = i;
int btnValue = digitalRead(btn);
int i = 0;
bool isPressed = false;
if (btnValue == LOW && isPressed == false){
i = i++;
if (i > 3){
i = 0;
}
}
{
switch(case){
case 1:
selector1 == 0;
do digitalWrite(led, HIGH);
Serial.println(digitalRead(23));
break;
case 2:
selector1 == 1;
do digitalWrite(13, HIGH);
Serial.println(digitalRead(13));
break;
case 3:
selector1 == 2;
do digitalWrite(4, HIGH);
Serial.println(digitalRead(4));
break;
case 4:
selector1 == 3
do digitalWrite(5, HIGH);
Serial.println("You've reached the last selection.");
Serial.println(digitalRead(5));
}
}
arduino switch-statement toggle increment gpio
add a comment |
I am working on a toggle using switch case. Not sure if I'm doing it right. My intention should be fairly obvious through my code example.
I want to increment a counter with a pushbutton use the x++ function and some toggle code I have saved that should be usable in a switch(case) statement to produce desired outcomes at a particular case by toggling an output pin on upon case selection and off when case is no longer valid. Each case would have its own output pin with a specific case loop to execute when that case becomes valid. I can write the output loops once I get the toggle mode working. There are some missing pieces to the code. I have no formal coding experience and have only taught myself a little bit here and there. I've started with my Basic Stamp Homework Board and switched over from BS2 to Arduino. My goal is to compile the codes that I need for basic projects and save it. Then I can realistically be done with base-coding and focus on connecting hardware and fine-tuning the hardware:software.
Not quite sure how to use this switch function. Wondering if I need to have a FOR statement somewhere in there to increment my counter. Was thinking I could increment the counter each time the toggle1 value flipped or something to that effect. Another though would be to utilize switch(case) by modulo, toggling case values by pressing the button a specific number of times. 1 press would be case 1, 2 press -- case 2, 3 press -- case 3, etc... Case 0 could be 5 presses and be the last cycle using the ++x operator. Toggling case using pushbutton and ++ should be a very very simple matter. I have very little experience with programming. Would like some guidance. I hope that the gist of my idea is all in the code. Thank you.
const int btn = 22;
const int led = 3;
int selector1 = i;
int btnValue = digitalRead(btn);
int i = 0;
bool isPressed = false;
if (btnValue == LOW && isPressed == false){
i = i++;
if (i > 3){
i = 0;
}
}
{
switch(case){
case 1:
selector1 == 0;
do digitalWrite(led, HIGH);
Serial.println(digitalRead(23));
break;
case 2:
selector1 == 1;
do digitalWrite(13, HIGH);
Serial.println(digitalRead(13));
break;
case 3:
selector1 == 2;
do digitalWrite(4, HIGH);
Serial.println(digitalRead(4));
break;
case 4:
selector1 == 3
do digitalWrite(5, HIGH);
Serial.println("You've reached the last selection.");
Serial.println(digitalRead(5));
}
}
arduino switch-statement toggle increment gpio
hi, I've edited your question to improve code formatting and reduce the text a bit; still it's quite long, so I recommend you to try to shorten it. The most important things are: what are you trying to achieve, what you have tried (your current code, see also Minimal, Complete, and Verifiable example), how that failed. As a short note, you don't need an extra{...}
wrapper aroundswitch(...){...}
thing
– YakovL
Jan 2 at 0:43
add a comment |
I am working on a toggle using switch case. Not sure if I'm doing it right. My intention should be fairly obvious through my code example.
I want to increment a counter with a pushbutton use the x++ function and some toggle code I have saved that should be usable in a switch(case) statement to produce desired outcomes at a particular case by toggling an output pin on upon case selection and off when case is no longer valid. Each case would have its own output pin with a specific case loop to execute when that case becomes valid. I can write the output loops once I get the toggle mode working. There are some missing pieces to the code. I have no formal coding experience and have only taught myself a little bit here and there. I've started with my Basic Stamp Homework Board and switched over from BS2 to Arduino. My goal is to compile the codes that I need for basic projects and save it. Then I can realistically be done with base-coding and focus on connecting hardware and fine-tuning the hardware:software.
Not quite sure how to use this switch function. Wondering if I need to have a FOR statement somewhere in there to increment my counter. Was thinking I could increment the counter each time the toggle1 value flipped or something to that effect. Another though would be to utilize switch(case) by modulo, toggling case values by pressing the button a specific number of times. 1 press would be case 1, 2 press -- case 2, 3 press -- case 3, etc... Case 0 could be 5 presses and be the last cycle using the ++x operator. Toggling case using pushbutton and ++ should be a very very simple matter. I have very little experience with programming. Would like some guidance. I hope that the gist of my idea is all in the code. Thank you.
const int btn = 22;
const int led = 3;
int selector1 = i;
int btnValue = digitalRead(btn);
int i = 0;
bool isPressed = false;
if (btnValue == LOW && isPressed == false){
i = i++;
if (i > 3){
i = 0;
}
}
{
switch(case){
case 1:
selector1 == 0;
do digitalWrite(led, HIGH);
Serial.println(digitalRead(23));
break;
case 2:
selector1 == 1;
do digitalWrite(13, HIGH);
Serial.println(digitalRead(13));
break;
case 3:
selector1 == 2;
do digitalWrite(4, HIGH);
Serial.println(digitalRead(4));
break;
case 4:
selector1 == 3
do digitalWrite(5, HIGH);
Serial.println("You've reached the last selection.");
Serial.println(digitalRead(5));
}
}
arduino switch-statement toggle increment gpio
I am working on a toggle using switch case. Not sure if I'm doing it right. My intention should be fairly obvious through my code example.
I want to increment a counter with a pushbutton use the x++ function and some toggle code I have saved that should be usable in a switch(case) statement to produce desired outcomes at a particular case by toggling an output pin on upon case selection and off when case is no longer valid. Each case would have its own output pin with a specific case loop to execute when that case becomes valid. I can write the output loops once I get the toggle mode working. There are some missing pieces to the code. I have no formal coding experience and have only taught myself a little bit here and there. I've started with my Basic Stamp Homework Board and switched over from BS2 to Arduino. My goal is to compile the codes that I need for basic projects and save it. Then I can realistically be done with base-coding and focus on connecting hardware and fine-tuning the hardware:software.
Not quite sure how to use this switch function. Wondering if I need to have a FOR statement somewhere in there to increment my counter. Was thinking I could increment the counter each time the toggle1 value flipped or something to that effect. Another though would be to utilize switch(case) by modulo, toggling case values by pressing the button a specific number of times. 1 press would be case 1, 2 press -- case 2, 3 press -- case 3, etc... Case 0 could be 5 presses and be the last cycle using the ++x operator. Toggling case using pushbutton and ++ should be a very very simple matter. I have very little experience with programming. Would like some guidance. I hope that the gist of my idea is all in the code. Thank you.
const int btn = 22;
const int led = 3;
int selector1 = i;
int btnValue = digitalRead(btn);
int i = 0;
bool isPressed = false;
if (btnValue == LOW && isPressed == false){
i = i++;
if (i > 3){
i = 0;
}
}
{
switch(case){
case 1:
selector1 == 0;
do digitalWrite(led, HIGH);
Serial.println(digitalRead(23));
break;
case 2:
selector1 == 1;
do digitalWrite(13, HIGH);
Serial.println(digitalRead(13));
break;
case 3:
selector1 == 2;
do digitalWrite(4, HIGH);
Serial.println(digitalRead(4));
break;
case 4:
selector1 == 3
do digitalWrite(5, HIGH);
Serial.println("You've reached the last selection.");
Serial.println(digitalRead(5));
}
}
arduino switch-statement toggle increment gpio
arduino switch-statement toggle increment gpio
edited Jan 2 at 0:48
YakovL
3,072102642
3,072102642
asked Jan 1 at 21:15
Au42Au42
12
12
hi, I've edited your question to improve code formatting and reduce the text a bit; still it's quite long, so I recommend you to try to shorten it. The most important things are: what are you trying to achieve, what you have tried (your current code, see also Minimal, Complete, and Verifiable example), how that failed. As a short note, you don't need an extra{...}
wrapper aroundswitch(...){...}
thing
– YakovL
Jan 2 at 0:43
add a comment |
hi, I've edited your question to improve code formatting and reduce the text a bit; still it's quite long, so I recommend you to try to shorten it. The most important things are: what are you trying to achieve, what you have tried (your current code, see also Minimal, Complete, and Verifiable example), how that failed. As a short note, you don't need an extra{...}
wrapper aroundswitch(...){...}
thing
– YakovL
Jan 2 at 0:43
hi, I've edited your question to improve code formatting and reduce the text a bit; still it's quite long, so I recommend you to try to shorten it. The most important things are: what are you trying to achieve, what you have tried (your current code, see also Minimal, Complete, and Verifiable example), how that failed. As a short note, you don't need an extra
{...}
wrapper around switch(...){...}
thing– YakovL
Jan 2 at 0:43
hi, I've edited your question to improve code formatting and reduce the text a bit; still it's quite long, so I recommend you to try to shorten it. The most important things are: what are you trying to achieve, what you have tried (your current code, see also Minimal, Complete, and Verifiable example), how that failed. As a short note, you don't need an extra
{...}
wrapper around switch(...){...}
thing– YakovL
Jan 2 at 0:43
add a comment |
1 Answer
1
active
oldest
votes
What is your question exactly? Keep It Short and Simple ;) Also, have you tried google? This is the first hit using 'Arduino switch'. There are even examples (one two) build into the arduino IDE. Searching and trying will teach you better than asking. Really, try things out - a lot. When you run into something new, like a switch-statement, upload a very easy sketch to the arduino and just test it out.
With that said, the main issue here seems to be that your code is very incomplete. Is this meant as testcode, or did you just copy some stuff to convey your idea? Because this code makes it very hard to help you in a clear and specific way. You also reintroduced the button pushdown issue from your previous question (that is still open).
For your edification I'll list a couple or the issues and also provide corrected code below
i++;
suffices to increment i
switch(case)
should be switch(i)
remove 'do' from do digitalWrite()
selector1 == 1;
does nothing.
there is no main loop
int selector1 = i;
this is initiating a variable with a variable that doesn't exist yet. This is pass by value, not a reference to i, so in your current code selector1 will never change.
int btnValue = digitalRead(btn);
similarly, this is a one time assignment, it never changes
digitalWrite(13, HIGH);
This is odd, though to my surprise not impossible. What are you trying to do here? Because it makes no sense to me.
Serial.println(digitalRead(13));
Corrected code:
const int btn = 22;
const int led = 3;
int selector = 0;
boolean isPressed = false;
void setup() {
Serial.begin(9600);
pinMode(btn, INPUT_PULLUP);
pinMode(led, OUTPUT);
pinMode(13, OUTPUT);
pinMode(4, OUTPUT);
pinMode(5, OUTPUT);
}
void loop ()
{
if (digitalRead(btn) == LOW && isPressed == false ) //button is pressed AND this is the first digitalRead() that the button is pressed
{
isPressed = true; //set to true, so this code will not run again until button released
doSwitchStatement(); // a call to a separate function that performs the switch statement and subsequent evoked code
selector++; // this is done after the doSwitchStatement(), so case 0 will be executed on the first button press
if (selector > 3) {
selector = 0;
}
// selector = (selector+1) % 4; // does the same, without if-statement
} else if (digitalRead(btn) == HIGH)
{
isPressed = false; //button is released, variable reset
}
}
void doSwitchStatement() {
switch(selector) {
case 0:
digitalWrite(led, HIGH);
Serial.println("Case 0");
// add a call to doExtraStuff() if you like
break;
case 1:
digitalWrite(13, HIGH);
Serial.println("Case 1");
break;
case 2:
digitalWrite(4, HIGH);
Serial.println("Case 2");
break;
case 3:
do digitalWrite(5, HIGH);
Serial.println("You've reached the last selection.");
Serial.println("Case 3");
}
}
With each button press, first doSwitchStatement()
is executed, which sets an outputpin high and prints a message. After thatselector
is incremented and set to 0 if it reaches 4.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
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: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
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%2fstackoverflow.com%2fquestions%2f53999000%2fusing-switch-to-select-a-case-with-pushbutton-toggle%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
What is your question exactly? Keep It Short and Simple ;) Also, have you tried google? This is the first hit using 'Arduino switch'. There are even examples (one two) build into the arduino IDE. Searching and trying will teach you better than asking. Really, try things out - a lot. When you run into something new, like a switch-statement, upload a very easy sketch to the arduino and just test it out.
With that said, the main issue here seems to be that your code is very incomplete. Is this meant as testcode, or did you just copy some stuff to convey your idea? Because this code makes it very hard to help you in a clear and specific way. You also reintroduced the button pushdown issue from your previous question (that is still open).
For your edification I'll list a couple or the issues and also provide corrected code below
i++;
suffices to increment i
switch(case)
should be switch(i)
remove 'do' from do digitalWrite()
selector1 == 1;
does nothing.
there is no main loop
int selector1 = i;
this is initiating a variable with a variable that doesn't exist yet. This is pass by value, not a reference to i, so in your current code selector1 will never change.
int btnValue = digitalRead(btn);
similarly, this is a one time assignment, it never changes
digitalWrite(13, HIGH);
This is odd, though to my surprise not impossible. What are you trying to do here? Because it makes no sense to me.
Serial.println(digitalRead(13));
Corrected code:
const int btn = 22;
const int led = 3;
int selector = 0;
boolean isPressed = false;
void setup() {
Serial.begin(9600);
pinMode(btn, INPUT_PULLUP);
pinMode(led, OUTPUT);
pinMode(13, OUTPUT);
pinMode(4, OUTPUT);
pinMode(5, OUTPUT);
}
void loop ()
{
if (digitalRead(btn) == LOW && isPressed == false ) //button is pressed AND this is the first digitalRead() that the button is pressed
{
isPressed = true; //set to true, so this code will not run again until button released
doSwitchStatement(); // a call to a separate function that performs the switch statement and subsequent evoked code
selector++; // this is done after the doSwitchStatement(), so case 0 will be executed on the first button press
if (selector > 3) {
selector = 0;
}
// selector = (selector+1) % 4; // does the same, without if-statement
} else if (digitalRead(btn) == HIGH)
{
isPressed = false; //button is released, variable reset
}
}
void doSwitchStatement() {
switch(selector) {
case 0:
digitalWrite(led, HIGH);
Serial.println("Case 0");
// add a call to doExtraStuff() if you like
break;
case 1:
digitalWrite(13, HIGH);
Serial.println("Case 1");
break;
case 2:
digitalWrite(4, HIGH);
Serial.println("Case 2");
break;
case 3:
do digitalWrite(5, HIGH);
Serial.println("You've reached the last selection.");
Serial.println("Case 3");
}
}
With each button press, first doSwitchStatement()
is executed, which sets an outputpin high and prints a message. After thatselector
is incremented and set to 0 if it reaches 4.
add a comment |
What is your question exactly? Keep It Short and Simple ;) Also, have you tried google? This is the first hit using 'Arduino switch'. There are even examples (one two) build into the arduino IDE. Searching and trying will teach you better than asking. Really, try things out - a lot. When you run into something new, like a switch-statement, upload a very easy sketch to the arduino and just test it out.
With that said, the main issue here seems to be that your code is very incomplete. Is this meant as testcode, or did you just copy some stuff to convey your idea? Because this code makes it very hard to help you in a clear and specific way. You also reintroduced the button pushdown issue from your previous question (that is still open).
For your edification I'll list a couple or the issues and also provide corrected code below
i++;
suffices to increment i
switch(case)
should be switch(i)
remove 'do' from do digitalWrite()
selector1 == 1;
does nothing.
there is no main loop
int selector1 = i;
this is initiating a variable with a variable that doesn't exist yet. This is pass by value, not a reference to i, so in your current code selector1 will never change.
int btnValue = digitalRead(btn);
similarly, this is a one time assignment, it never changes
digitalWrite(13, HIGH);
This is odd, though to my surprise not impossible. What are you trying to do here? Because it makes no sense to me.
Serial.println(digitalRead(13));
Corrected code:
const int btn = 22;
const int led = 3;
int selector = 0;
boolean isPressed = false;
void setup() {
Serial.begin(9600);
pinMode(btn, INPUT_PULLUP);
pinMode(led, OUTPUT);
pinMode(13, OUTPUT);
pinMode(4, OUTPUT);
pinMode(5, OUTPUT);
}
void loop ()
{
if (digitalRead(btn) == LOW && isPressed == false ) //button is pressed AND this is the first digitalRead() that the button is pressed
{
isPressed = true; //set to true, so this code will not run again until button released
doSwitchStatement(); // a call to a separate function that performs the switch statement and subsequent evoked code
selector++; // this is done after the doSwitchStatement(), so case 0 will be executed on the first button press
if (selector > 3) {
selector = 0;
}
// selector = (selector+1) % 4; // does the same, without if-statement
} else if (digitalRead(btn) == HIGH)
{
isPressed = false; //button is released, variable reset
}
}
void doSwitchStatement() {
switch(selector) {
case 0:
digitalWrite(led, HIGH);
Serial.println("Case 0");
// add a call to doExtraStuff() if you like
break;
case 1:
digitalWrite(13, HIGH);
Serial.println("Case 1");
break;
case 2:
digitalWrite(4, HIGH);
Serial.println("Case 2");
break;
case 3:
do digitalWrite(5, HIGH);
Serial.println("You've reached the last selection.");
Serial.println("Case 3");
}
}
With each button press, first doSwitchStatement()
is executed, which sets an outputpin high and prints a message. After thatselector
is incremented and set to 0 if it reaches 4.
add a comment |
What is your question exactly? Keep It Short and Simple ;) Also, have you tried google? This is the first hit using 'Arduino switch'. There are even examples (one two) build into the arduino IDE. Searching and trying will teach you better than asking. Really, try things out - a lot. When you run into something new, like a switch-statement, upload a very easy sketch to the arduino and just test it out.
With that said, the main issue here seems to be that your code is very incomplete. Is this meant as testcode, or did you just copy some stuff to convey your idea? Because this code makes it very hard to help you in a clear and specific way. You also reintroduced the button pushdown issue from your previous question (that is still open).
For your edification I'll list a couple or the issues and also provide corrected code below
i++;
suffices to increment i
switch(case)
should be switch(i)
remove 'do' from do digitalWrite()
selector1 == 1;
does nothing.
there is no main loop
int selector1 = i;
this is initiating a variable with a variable that doesn't exist yet. This is pass by value, not a reference to i, so in your current code selector1 will never change.
int btnValue = digitalRead(btn);
similarly, this is a one time assignment, it never changes
digitalWrite(13, HIGH);
This is odd, though to my surprise not impossible. What are you trying to do here? Because it makes no sense to me.
Serial.println(digitalRead(13));
Corrected code:
const int btn = 22;
const int led = 3;
int selector = 0;
boolean isPressed = false;
void setup() {
Serial.begin(9600);
pinMode(btn, INPUT_PULLUP);
pinMode(led, OUTPUT);
pinMode(13, OUTPUT);
pinMode(4, OUTPUT);
pinMode(5, OUTPUT);
}
void loop ()
{
if (digitalRead(btn) == LOW && isPressed == false ) //button is pressed AND this is the first digitalRead() that the button is pressed
{
isPressed = true; //set to true, so this code will not run again until button released
doSwitchStatement(); // a call to a separate function that performs the switch statement and subsequent evoked code
selector++; // this is done after the doSwitchStatement(), so case 0 will be executed on the first button press
if (selector > 3) {
selector = 0;
}
// selector = (selector+1) % 4; // does the same, without if-statement
} else if (digitalRead(btn) == HIGH)
{
isPressed = false; //button is released, variable reset
}
}
void doSwitchStatement() {
switch(selector) {
case 0:
digitalWrite(led, HIGH);
Serial.println("Case 0");
// add a call to doExtraStuff() if you like
break;
case 1:
digitalWrite(13, HIGH);
Serial.println("Case 1");
break;
case 2:
digitalWrite(4, HIGH);
Serial.println("Case 2");
break;
case 3:
do digitalWrite(5, HIGH);
Serial.println("You've reached the last selection.");
Serial.println("Case 3");
}
}
With each button press, first doSwitchStatement()
is executed, which sets an outputpin high and prints a message. After thatselector
is incremented and set to 0 if it reaches 4.
What is your question exactly? Keep It Short and Simple ;) Also, have you tried google? This is the first hit using 'Arduino switch'. There are even examples (one two) build into the arduino IDE. Searching and trying will teach you better than asking. Really, try things out - a lot. When you run into something new, like a switch-statement, upload a very easy sketch to the arduino and just test it out.
With that said, the main issue here seems to be that your code is very incomplete. Is this meant as testcode, or did you just copy some stuff to convey your idea? Because this code makes it very hard to help you in a clear and specific way. You also reintroduced the button pushdown issue from your previous question (that is still open).
For your edification I'll list a couple or the issues and also provide corrected code below
i++;
suffices to increment i
switch(case)
should be switch(i)
remove 'do' from do digitalWrite()
selector1 == 1;
does nothing.
there is no main loop
int selector1 = i;
this is initiating a variable with a variable that doesn't exist yet. This is pass by value, not a reference to i, so in your current code selector1 will never change.
int btnValue = digitalRead(btn);
similarly, this is a one time assignment, it never changes
digitalWrite(13, HIGH);
This is odd, though to my surprise not impossible. What are you trying to do here? Because it makes no sense to me.
Serial.println(digitalRead(13));
Corrected code:
const int btn = 22;
const int led = 3;
int selector = 0;
boolean isPressed = false;
void setup() {
Serial.begin(9600);
pinMode(btn, INPUT_PULLUP);
pinMode(led, OUTPUT);
pinMode(13, OUTPUT);
pinMode(4, OUTPUT);
pinMode(5, OUTPUT);
}
void loop ()
{
if (digitalRead(btn) == LOW && isPressed == false ) //button is pressed AND this is the first digitalRead() that the button is pressed
{
isPressed = true; //set to true, so this code will not run again until button released
doSwitchStatement(); // a call to a separate function that performs the switch statement and subsequent evoked code
selector++; // this is done after the doSwitchStatement(), so case 0 will be executed on the first button press
if (selector > 3) {
selector = 0;
}
// selector = (selector+1) % 4; // does the same, without if-statement
} else if (digitalRead(btn) == HIGH)
{
isPressed = false; //button is released, variable reset
}
}
void doSwitchStatement() {
switch(selector) {
case 0:
digitalWrite(led, HIGH);
Serial.println("Case 0");
// add a call to doExtraStuff() if you like
break;
case 1:
digitalWrite(13, HIGH);
Serial.println("Case 1");
break;
case 2:
digitalWrite(4, HIGH);
Serial.println("Case 2");
break;
case 3:
do digitalWrite(5, HIGH);
Serial.println("You've reached the last selection.");
Serial.println("Case 3");
}
}
With each button press, first doSwitchStatement()
is executed, which sets an outputpin high and prints a message. After thatselector
is incremented and set to 0 if it reaches 4.
edited Jan 1 at 23:21
answered Jan 1 at 23:10
J.D.J.D.
1,217129
1,217129
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- 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%2fstackoverflow.com%2fquestions%2f53999000%2fusing-switch-to-select-a-case-with-pushbutton-toggle%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
hi, I've edited your question to improve code formatting and reduce the text a bit; still it's quite long, so I recommend you to try to shorten it. The most important things are: what are you trying to achieve, what you have tried (your current code, see also Minimal, Complete, and Verifiable example), how that failed. As a short note, you don't need an extra
{...}
wrapper aroundswitch(...){...}
thing– YakovL
Jan 2 at 0:43