There are two common techniques used to make a microcontroller (like the Arduino) respond to changes in its inputs. One is polling, the other is interrupts.
In a polling setup, the mircocontoller explicitly examines all its input sources during its main loop to see what state each input is in, and then it responds accordingly.
In an interrupt scheme, the microcontroller does essentially nothing in its main loop, but it is directed to do something specific when an input source jostles it into action.
Of the two, polling is probably easier to get started with. We won't learn how to work with interrupts in this section, but it's good to know that you can use both techniques in the same program.
The following examples use polling to determine the state of a switch. If the switch is pressed, the Arduino will turn an LED on. If it is not pressed, it will turn the LED off.
The following example requires pulldown resistors on the input switch.
/* LightSwitch Turn an LED on and off. Requires pulldown resistor on input. */ int pushButtonPin = 2; // connect the push button to digital pin 2 int ledPin = 13; // connect the LED to pin 13 int buttonState; // stores current button state void setup() { pinMode(pushButtonPin, INPUT); // make the pushbutton's pin an input pinMode(ledPin, OUTPUT); // make LED's pin an output } void loop() { buttonState = digitalRead(pushButtonPin); // read the input pin // set LED state accordingly if (buttonState == HIGH) // if the button is pushed digitalWrite(ledPin, HIGH); // turn the LED on else // otherwise digitalWrite(ledPin, LOW); // turn the LED off //delay(1); // delay between reads for stability (?? I don't remember why this got in here.) }
Notice the use of an if-else
statement. The if-else
statement is an example of control flow.
A more compact version of the above that eliminates the if-else statement:
/* LightSwitch2 Turn an LED on and off. Requires pulldown resistor on input. */ int pushButtonPin = 2; // connect the push button to digital pin 2 int ledPin = 13; // connect the LED to pin 13 int buttonState; // stores current button state void setup() { pinMode(pushButtonPin, INPUT); // make the pushbutton's pin an input pinMode(ledPin, OUTPUT); // make LED's pin an output } void loop() { buttonState = digitalRead(pushButtonPin); // read the input pin digitalWrite(ledPin, buttonState); // turn the LED on or off //delay(1); // delay between reads for stability (?? I don't remember why this got in here.) }
And an even more compact version:
/* LightSwitch3 Turn an LED on and off. Requires pulldown resistor on input. */ int pushButtonPin = 2; // connect the push button to digital pin 2 int ledPin = 13; // connect the LED to pin 13 int buttonState; // stores current button state void setup() { pinMode(pushButtonPin, INPUT); // make the pushbutton's pin an input pinMode(ledPin, OUTPUT); // make LED's pin an output } void loop() { digitalWrite(ledPin, digitalRead(pushButtonPin)); // read the input pin and turn the LED on or off //delay(1); // delay between reads for stability (?? I don't remember why this got in here.) }
Using extenal resistors as part of switch state detection is so common that the microchip that is at the heart of the Arduino has built-in pullup resistors that can be turned on manually. Internal pullup resistors can be enabled with:
digitalWrite(pushButtonPin, HIGH);
Here are the first two versions above but modified to use internal pullup resistors.
/* LightSwitchPullup Turn an LED on and off. Internal pullup version. */ int pushButtonPin = 2; // connect the push button to digital pin 2 int ledPin = 13; // connect the LED to pin 13 int buttonState; // stores current button state void setup() { pinMode(pushButtonPin, INPUT); // make the pushbutton's pin an input digitalWrite(pushButtonPin, HIGH); // turn on internal pullup resistors pinMode(ledPin, OUTPUT); // make LED's pin an output } void loop() { buttonState = digitalRead(pushButtonPin); // read the input pin // set LED state accordingly // becasue we are using pullup resistors, the logic is inverted; // in other words, pressed produces LOW, un-pressed produces HIGH. if (buttonState == LOW) // if the button is pushed digitalWrite(ledPin, HIGH); // turn the LED on else // otherwise digitalWrite(ledPin, LOW); // turn the LED off }
/* LightSwitchPullup2 Turn an LED on and off. Internal pullup version. */ int pushButtonPin = 2; // connect the push button to digital pin 2 int ledPin = 13; // connect the LED to pin 13 int buttonState; // stores current button state void setup() { pinMode(pushButtonPin, INPUT); // make the pushbutton's pin an input digitalWrite(pushButtonPin, HIGH); // turn on internal pullup resistors pinMode(ledPin, OUTPUT); // make LED's pin an output } void loop() { buttonState = digitalRead(pushButtonPin); // read the input pin // set LED state accordingly // becasue we are using pullup resistors, the logic is inverted; // in other words, pressed produces LOW, un-pressed produces HIGH. digitalWrite(ledPin, !buttonState); // turn the LED on or off }