Third Week Arduino

Digital Output

 


The Serial Monitor

 


Two Buttons Control One LED (on and off)

// constants won't change. They're used here to set pin numbers:
const int buttonPinBottom = 6; // the number of the pushbutton pin
const int buttonPinTop = 7;
const int ledPin = 8;

// variables will change int means integer, var means variable
int buttonStateBottom = 0; // variables for reading the pushbutton status
int buttonStateTop = 0;

void setup() {
// initialize the LED pin as an output:
pinMode(ledPin, OUTPUT);
// initialize the pushbutton pin as an input:
pinMode(buttonPinBottom, INPUT_PULLUP);
pinMode(buttonPinTop, INPUT_PULLUP);
}

void loop() {
// read the state of the pushbutton values:
buttonStateBottom = digitalRead(buttonPinBottom);

// check if the pushbutton 6 is pressed. If it is, the buttonState is LOW:
if (buttonStateBottom == LOW) {
// turn LED on:
digitalWrite(ledPin, HIGH);
}

buttonStateTop = digitalRead(buttonPinTop);

// check if the pushbutton 7 is pressed. If it is, the buttonState is LOW:
if (buttonStateTop == LOW) {
// turn LED off:
digitalWrite(ledPin, LOW);
}
}