At GAA training, can a lamp come on only when you press a button and it's dark?
At an evening GAA training session, you want a small indicator to light when you press a button, provided it's dark enough. If you press the same button in daylight, it should stay off. How can one program make that decision using both inputs?
Your digital button has two states: pressed and released. You stored an analogue reading of three hundred and fifty in dim light and four hundred and sixty in brighter light. On this setup, the lower reading means less light. Choose four hundred, between the two readings, as your threshold.
Start with the button released. The indicator stays off whether the reading is below four hundred or at least four hundred. Now press the button. A reading below four hundred turns the indicator on. A reading of four hundred or more leaves it off.
Your finger is off the enable button, and the light reading is three hundred and eighty. The threshold is four hundred. Will the indicator turn on or stay off? Pick the answer that gives the right reason.
Import the button and pins. Set the threshold to four hundred. Each time round the loop, store button A's state, then the analogue reading from pin zero. The and condition needs both tests to pass. Write one to pin two to light the LED, or zero to turn it off.
Trace one pass through the loop. You press the button, so enabled is true. The program stores three hundred and ninety as the light reading. That's below four hundred, so the light test is true. True and true gives true. The program converts true to one and writes it to pin two.
Keep the button pressed and change the stored light reading to exactly four hundred. Trace the same condition, especially the less-than comparison. Which value does the program write to pin two? Pick an answer.
Both versions read the button and store the light reading. On the left, only the button decides the output, so pressing it lights the indicator even at four hundred and ten. On the right, that same reading fails the light test, so the indicator stays off.
For an exam question that asks you to complete a trace table, write the output for each row. With the button released, all three outputs are zero. With it pressed, only three hundred and ninety-nine gives one. Test four hundred itself as well as the values on either side.