While, then, if

I’m relatively new to programming the Arduino but I’m working on a project where I’m looking at using some switches, probably 8, and having a button that while certain switches are high and certain switches are low, when the button is pressed a servo is triggered but only after the switches are returned to low. Is this possible and which statements should I use?

Hello mpboyer3496,

Welcome to the Arduino fora. I and the other people here want to help you but to do that we need certain bits of information. Without the correct information it is difficult or impossible to give you the help and advice you need. To help us to help you please read General guidance andHow to use this forum and provide the information asked for. Being new here you might think this is asking too much or having rules for the sake of rules, that is not the case. If you don’t follow the guidelines all that happens is there is a long exchange of message while we try to get you to tell us what we need in order to help you, this is frustrating for you and frustrating for us.

Common mistakes for people posting here the first time:
Code problems:
Not posting any code while expecting us to work out what is wrong with your code; we are not telepathic and can only find problems in code we can see.
Posting a snippet of code in the belief that the problem is in the code snippet. It is almost always the case that the problem is not where you think it is but elsewhere in the code, hence we need all the code.
Not correctly formatting code. Before posting code use Tools / auto format at the top of the IDE, or use control T. Once you’ve done that use edit / copy for forum.
Posting code without code tags (</>), these are explained in the instructions linked to above. Using code tags makes the code easier to read. Not using code tags means some of the code gets interpreted as HTML with the result that it is displayed with smiley faces and other stuff that should not be there.
Not being clear about what is expected from code or what happened instead. Code ALWAYS works, that is the nature of code. Whether it does what you expect is a different thing altogether. We need to know what you expected the code to do and what happened instead.
Asking for complete code; we are not here to write code for you, we are here to help you if you get stuck writing code yourself. If you really want someone to write code for you please click on ‘report to moderator’ and ask them to move this to ‘Gigs and Collaborations’ and indicate how much you are willing to pay.

Schematics:
The language of electronics is a schematic or circuit diagram. Long descriptions of what is connected to what are generally useless. A schematic is a drawing of what connects to what, please make one and photograph it. We don’t mind if it’s hand drawn, scruffy and does not use the correct symbols. We do mind if you don’t at least try to post a schematic. Please read How to post an image

General:
Posting links to other sites where code or photos or schematics are hosted. Most of us will not follow such links, partly because of the risk that they hold malware or other unwanted content, partly to maintain everyting on this site for the benefit of future users looking for an answer to a similar question and partly because we feel that if you want our help you should provide everything we need on this site not expect us to go hunting elsewhere for it.

Questions:
Not being clear about what is being asked, for example not asking a question at all or asking a vague question like ‘please help’ or some such thing.

About us:
Those of us answering questions have a wide variety of backgrounds and electronics knowledge, some though working in electronics, some through electronics as a hobby, some both. Most of us are not trained as teachers so probably miss the finer points of how to teach and explain things effectively. We are not, with the odd exception, employees or representatives of Arduino.
Please also remember we are volunteers doing this for free in our spare time and are more inclined to help people who make it easy for us to provide help by providing the information we ask for.

About you:
We only know about you what you tell us; we need to know your level of experience with electronics or microcontrollers, if we don’t know then we can’t tailor our answer to your level of knowledge. We also don’t know if you have problems with English or language or communications in general unless you tell us. We can be sympathetic about problems we know about, but if you don’t tell us we don’t know.

Thank you.

Start with an Arduino UNO. For each switch or button, pick an I/O pin. Pins 0 and 1 are special (Serial port) so best to start with 2. Set the pin to be an input with the internal pull-up resistor enabled:

// Give each pin number a name so the sketch is easier to understand
const byte ButtonPin = 2;

void setup()
{
  pinMode(ButtonPin, INPUT_PULLUP);
  pinMode(LED_BUILTIN, OUTPUT); // The LED marked 'L' on the UNO
  digitalWrite(LED_BUILTIN, LOW);  // Turn the LED off
}

Now the pin will read as HIGH unless you connect it to Ground. Put your button or switch between the pin and Ground.

Now your sketch can use digitalRead(pin) to check if the switch/button is open (HIGH) pr closed (LOW).

// Do something when the button gets pressed
void loop()
{
  if (digitalRead(ButtonPin) == LOW)
    digitalWrite(LED_BUILTIN, HIGH);  // Turn on the LED
}

Now just expand on that. :slight_smile:

Also, this site has tons of tutorials, examples, and basic documentation of all common features. Look under the "documentation" tab. :slight_smile:

The example sketches that ship with the IDE are designed to help a beginner get started.

I know my way around the Arduino to a point but what I don’t know is how to accomplish the question that I asked which I thought was pretty specific.

I'm relatively new to programming the Arduino but I'm working on a project where I'm looking at using some switches, probably 8, and having a button that while certain switches are high and certain switches are low, when the button is pressed a servo is triggered but only after the switches are returned to low. Is this possible and which statements should I use?

mpboyer3496:
I know my way around the Arduino to a point but what I don’t know is how to accomplish the question that I asked which I thought was pretty specific.

If you want help with your sketch, show us what you have so far. If you want someone else to do the programming from your specification, try the "Gigs and Collaboration" section of the forum.
I would recommend an array of pin numbers for the switches. I would also recommend a State Machine design for the various steps. For each step where you are waiting for something to happen, have a state. When that is the current state, check to see if the thing you were waiting for happened.

Short answers:

mpboyer3496:
Is this possible

Yes

mpboyer3496:
which statements should I use?

if

But just to clarify your ask? Seems you want to read the switches when the button is pressed, and if at that moment the switches are correctly set (say 10101010 for argument's sake), remember that fact and prime the system so to speak, so that later on when the switches are all 00000000, move the servo?

If that was a correct statement of the problem, then you have two states, say IDLE and PRIMED, start in IDLE.

In state IDLE (and remember loop() is looping away like mad) read the button. If the button is not pressed, do nothing. If it's pressed, read the switches. If the switches are wrong, do nothing. If they're correct, move to state PRIMED.

In state PRIMED, read the switches (and remember loop() is looping away like mad). If the switches are not all 0, do nothing. If they're all 0, do the thing with the servo, and (presumably) revert to state IDLE.

(You could actually get away with one [edit: forgot to say boolean] state variable, since it would have two values true and false, so could just have variable "primed". If it's false you're idling, if it's true, you're primed.)

I actually coded it with 3 states, IDLE, PRIMED and ACTIVE. You're welcome to the code if you want it.

You need to specify what happens in PRIMED if the button is pressed again. Should it ignore that press and stay PRIMED regardless of what may have happened to the switches meantime, or re-check the switches and revert to IDLE if they're wrong when that press occurs?

mpboyer3496:
I know my way around the Arduino to a point but what I don’t know is how to accomplish the question that I asked which I thought was pretty specific.

I thought your question was specific and meaningless. It was like 'I am working on a project to write a book in English with characters in the fishing community, is this possible and which words shall I use?'. The answers being 'yes' and 'ones from the English language'. This is precise and useless. The answers to your questions are 'yes' and 'any of the statements valid in C++, according to the needs of your program'

You will I hope realise my response is a standard reply I give to try to encourage better questions, it is therefore generic and not tailored to your particular question. If you have since got useful answers from other people who saw something in your question that I could not see then all well and good. If you are still in need of help then please follow the advice for asking a good question.

Good luck with your project.

To me it is more like I’m trying to write a book in a language that I barely speak and need to communicate something specific to that language but I don’t know if my English translates properly to that language.

mpboyer3496:
To me it is more like I’m trying to write a book in a language that I barely speak and need to communicate something specific to that language but I don’t know if my English translates properly to that language.

That is where the universal language of images, diagrams and code solves the problem! But where are they?...

I have a project that has two switches and several conditions that must be all be in the correct state for the next thing to happen.

I'm using a while loop with a few IF statements in it.

I was and still am a bit of a novice but read through plenty of tutorials on while and if and eventually figured it out.

I suggest just starting with one switch and get that working and then Expand on that. That's what I did and learned a lot by battling through it.

Here btw and fwiw is the state diagram for how I coded it as an exercise. Coding's almost trivial when you have a decent state diagram.

Each ST_xxx is a case in a switch...case.