We have introduced two new concepts with this example program: functions that return the result of their work, and the if statement.

The if statement is possibly the most important instruction in a programming language, because it allows a computer (and remember, the Arduino is a small computer) to make decisions. After the if keyword, you have to write a “question” inside parentheses, and if the “answer”, or result, is true, the first block of code will be executed; otherwise, the block of code after else will be executed.

Notice that the == symbol is very different from the = symbol. The former is used when two entities are compared, and returns true or false; the latter assigns a value to a constant or a variable. Make sure that you use the correct one, because it is very easy to make that mistake and use just = , in which case your program will never work. We know, because after years of programming, we still make that mistake.

It’s important to realise that the switch is not connected directly to the LED. Your Arduino sketch inspects the switch, and then makes a decision as to whether to turn the LED on or off. The connection between the switch and the LED is really happening in your sketch.

Holding your finger on the button for as long as you need light is not practical. Although it would make you think about how much energy you’re wasting when you walk away from a lamp that you left on, we need to figure out how to make the on button “stick”.


Comments

Leave a Reply

Your email address will not be published. Required fields are marked *