

But, for more than 2 buttons, nested if statements are far easier to deal with. Hmmm, words are not helping all that much. Two more tests to confirm the possibility of being 1 or 3 or 2 or 4. In particular Ive Imagined a sketch with a 'switch case loop' for the function selection. Two more tests, and you know that it could be buttons 2 or 4. Hi, I would like to create a sketch that execute some different function (sensor measurements) depending on which key is pressed by user. So, two tests, and you know that the press could be in one of two buttons. When a case statement is found whose value. Similarly, the p.x value will be less than some value for the press to be in 1 or 3. In particular, a switch statement compares the value of a variable to the values specified in case statements.

If buttons 1 and 2 are on the same row, and buttons 3 and 4 are on the next row, and lined up, then you only need to test that p.x is greater than the left edge of one button to test that the press is in either button 1 or button 3. You don't actually need 16 if statements for 4 buttons. You can have 4 ugly, complex, redundant if statements, or you can have 16 simple if statements. Now, why might this be better? Suppose you have 4 buttons.
#Switch case inside switch case arduino code#
Instead you'd use code similar to the blink-without-delay example to progress from one state to another, where each state prints out message, and progresses to the next state, possibly even setting a different interval (there's a variable called that in the example) each time. It's far easier to debug code that involves multiple if tests if you use multiple if statements. gearbox1 is value read from Nextion screen gearbox1myNextion.getComponentValue('G1') What i am trying to achieve is, when case 10 (witch is checked once a second) run a 10 second loop turning output on and off. AWOL: Imagine your function 'firstcase' written without using 'delay'.
