theoretical code help

I am beginning work on a project and needed some help to get me over one of these mental blocks im having (sort of like - damn, i should know this, but since i dont, i wont be able to think about anything else till i figure it out)

anyway, the question is -

i need my loop function to wait for the pressing of, 1 of 6 buttons, then, once of them is pressed, do something accordingly (this is easy enough)… but while doing it, disregard the buttons (that is, if a button is pressed while the “do something accordingly” action is occurring, the pressing of the button will be disregarded by the code).

the way i thought of doing this was something like

wait for button press → do something accordingly → while doing this action, set button output pin (or multiple pins, since i will have many buttons) to 5v (i would be using internal pull up resistors), making the buttons unusable (there wont be any change in value recorded by the arduino since either way a 5v input will be detected) → finished doing action → back to beginning of loop → set button pins back to gnd, making them usable once again…

would this work? anyone have a better way?

would this work? anyone have a better way?

I think your over thinking the problem making it more complex then it really is. Your buttons will only do things when your code reads them and takes action on them. So if you read and detect a specific button pressed you then go do something else, then the fact that another button might be pressed won’t do anything at all because your code is off doing something else, not checking buttons.

That make sense?

Lefty

i see your point, but the way i was structuring the code in my mind was having a bunch of parallel if statements, each leading to their own functions, which meant that if one “if” suddently became true and began doing something, the code would still continue looping, and thus if another “if” became true, it would do something else… i think…

here, let me diagram it

void loop(){

if (digitalRead(pin1, LOW)){

x = 1;

}

if (digitalRead(pin2, LOW)){

x = 2;

}



if (x == 1){

do this stuff

}

if (x == 2){

do this other stuff

}

this would be without the idea i had for overcoming re-reading button presses… in which case i would add “digitalWrite(button1_outPin, HIGH);” and “digitalWrite(button2_outPin, HIGH);” to the “do this stuff” and “do this other stuff”, respectively, as well as “digitalWrite(button1_outPin, LOW);” to the opening of the loop

If you need to do some “…” things after you read the buttons but before you “do this stuff” you could:

void loop(){
if (digitalRead(pin1, LOW)){

  • functionThatDoes_…()*
  • do this stuff*
    }
    if (digitalRead(pin2, LOW)){
  • functionThatDoes_…()*
  • do this other stuff*
    }

That was close to retrolefty’s suggestion, as I understood it.

the other thing is that i need to have “do this stuff” actually completely finish - that is, press the button once, do a full routine - otherwise i could just cut the routine off at any point by pressing any other button (and in the code down now, the routine only occurs while the button is pressed down)…

you know what, il test this out, hopefully in a couple of weeks (damn final exams), and i think with a little of “real” tinkering I can get this done… perhaps i am poorly describing the scenario, which is why i can’t see why your code would work, and you don’t quite get what i mean either…

but thanks for the input anyway guys :wink: