So I am trying to code a robot and I want it to have separate game modes that will be coded differently, i.e one will be using a random number generator, one would be using an optimal move ai code and one would be code that would be coded by the buttons on a remote. Is there a way to be able to switch what code is being run based on what button is pushed first? Would it just be a bunch of if statements like "If button A is pushed, run code A" Any tips would be appreciated, I have never actually coded before!
Would it just be a bunch of if statements like "If button A is pushed, run code A"
If buttons A, B, C... are a sliding or rotary multi-position switch, then yes, you are correct. But if the buttons are all momentary pushbuttons, then the logic should be "If button A was the last to be pushed, run code A". This means you need a variable to record which button was last pressed. When a button is pressed, you update that variable. When you choose which code to run, you check the variable instead of the buttons.
You will need to detect when a button becomes pressed rather than when it is pressed. Take a look at the StateChangeDetection example in the IDE to see how to do it
UKHeliBob:
You will need to detect when a button becomes pressed rather than when it is pressed. Take a look at the StateChangeDetection example in the IDE to see how to do it
Hi Bob, that would only be required if a single button was used to cycle round the modes. If there is a button for each mode, which is how I interpreted the OP, state change detection not required.