HW

A. Lab: Digital Input and Output with an Arduino

1. Setting up: In this lab, I type the code in Arduino to turn a twinkle LED.

IMG_4627.JPG

https://youtu.be/6KI74OW5rMc

2. What Are The Input and Output Pins?

When I press the switch, LED will trun off the light, otherwise, it will turn on.

https://youtu.be/NWxVcHZOdWQ

void setup() {
  // confihure the input and the output pins
  pinMode(2, OUTPUT); // the LED
  pinMode(3, INPUT); // the switch

}

void loop() {
  // read the state of the pushbutton
  int buttonState = digitalRead(3);

  // write to the LED depending on the state of the button
  if (buttonState == HIGH) {
    // turn the LED on
    digitalWrite (2, LOW);
  } else {
    // turn the LED off
    digitalWrite(2, HIGH);
  }
}

3. Add Digital Outputs (LEDs)

https://youtu.be/BCurqsvcgq4

void setup() {
  pinMode(2,INPUT);
  pinMode(3,OUTPUT);
  pinMode(4,OUTPUT);
}

void loop() {
  // put your main code here, to run repeatedly:
if (digitalRead(2)==HIGH){
  digitalWrite(3,HIGH);
  digitalWrite(4,LOW);
}
else{
 digitalWrite(3,LOW);
  digitalWrite(4,HIGH); 
}
}

B. Lab: Analog In with an Arduino