Robots Atlas>ROBOTS ATLAS

Arduino for Beginners · Digital Inputs and Outputs

if/else conditionals driven by an input

Digital Inputs and Outputs

Introduction

A circuit truly reacts to the world only once it can decide: if the button is pressed, light the LED — otherwise turn it off. This lesson shows how the if (condition) { } statement driven by digitalRead() makes such decisions, how the else branch and the else if chain handle the remaining cases, and how a condition is really a logical expression evaluating to true or false. We dissect the comparison digitalRead(pin) == HIGH and == LOW, the famous trap of == (comparison) versus = (assignment), the comparison operators (==, !=) and the logical operators && (and), || (or), ! (negation) for combining two-button conditions, plus the shortcut if(digitalRead(pin)) that relies on HIGH being true being 1, and LOW being false being 0.