Little Bird Curriculum / code littlebird.com.au
_02_Button.ino Arduino (C/C++) 17 lines · 6 views ✦ Open in English IDE Raw Download Embed view

Use the ThinkerShield's pushbutton (D7) to switch the D12 LED on and off — digitalRead and an if/else statement.

From the unit Crack the Code · activity PRP 2: Digital input — Button
int buttonPin = 7;        // the pushbutton is on pin D7 of the ThinkerShield
int ledPin = 12;          // the LED is on pin D12
int buttonState = 0;      // variable for reading the pushbutton's state

void setup() {
  pinMode(ledPin, OUTPUT);     // the LED pin is an output
  pinMode(buttonPin, INPUT);   // the button pin is an input
}

void loop() {
  buttonState = digitalRead(buttonPin);   // read the button
  if (buttonState == HIGH) {              // is it pressed?
    digitalWrite(ledPin, HIGH);           // yes -> LED on
  } else {
    digitalWrite(ledPin, LOW);            // no  -> LED off
  }
}

Embed this snippet

As an iframe:

Or drop it inline with a script tag (injects the highlighted block where included):

Shared from Little Bird Electronics curriculum.

Copied to clipboard