Wen’s Arduino Homework 2

#1 Digital Input

#2 The Serial Monitor

#3 Analog Input

#4 Two buttons control one LED (on and off)

Code:

// constants won’t change. They’re used here to set pin numbers:
const int onbutton = 2; // the number of the pushbutton pin
const int offbutton = 3;
const int LED = 13; // the number of the LED pin

// variables will change:
int whatisit = 0; // variable for reading the pushbutton status

void setup() {
// initialize the LED pin as an output:
pinMode(LED, OUTPUT);
// initialize the pushbutton pin as an input:
pinMode(onbutton, INPUT);
pinMode(offbutton, INPUT);
}

void loop() {
// read the state of the pushbutton value:
whatisit = digitalRead(onbutton);
if (whatisit == 0) {
// turn LED on:
digitalWrite(LED, HIGH);
}

whatisit = digitalRead(offbutton);
if (whatisit == 0) {
digitalWrite(LED, LOW);}
}