Week 3 Arduino Homework Exercises

Here are the videos of the Arduino exercises of this week. This week’s exercises are different from before and also harder. I was trying to figure out how buttons work. Also for the serial monitor which do no have a distinct output like LED, showing a different perspective of Arduino.

Exercise 1 : Digital Input

Exercise 2 : The serial monitor

For composing a circuit and Arduino sketch, I used Pressing button to change fading animation speed.

Pressing button changes fading animation speed

const int buttonPin = 2;     // the number of the pushbutton pin
const int ledPin =  9;      // the number of the LED pin
int brightness = 0;    // how bright the LED is
int fadeAmount = 5;    // how many points to fade the LED by
int buttonState = 0;         // variable for reading the pushbutton status

void setup() {
  // put your setup code here, to run once:
 pinMode(ledPin, OUTPUT);
  // initialize the pushbutton pin as an input:
  pinMode(buttonPin, INPUT);
}

void loop() {
  // put your main code here, to run repeatedly:
  buttonState = digitalRead(buttonPin);
  
  // check if the pushbutton is pressed. If it is, the buttonState is HIGH:
  if (buttonState == HIGH) {

  analogWrite(ledPin, brightness);
  
  // change the brightness for next time through the loop:
  brightness = brightness + fadeAmount;

  // reverse the direction of the fading at the ends of the fade:
  if (brightness <= 0 || brightness >= 255) {
    fadeAmount = -fadeAmount;
  }
  delay(10);
  } else {
  
  analogWrite(ledPin, brightness);
  
  // change the brightness for next time through the loop:
  brightness = brightness + fadeAmount;

  // reverse the direction of the fading at the ends of the fade:
  if (brightness <= 0 || brightness >= 255) {
    fadeAmount = -fadeAmount;
  }
    delay(50);
  }
}

2 thoughts on “Week 3 Arduino Homework Exercises”

    1. Thank you Becky! I will try to embed the videos next time. And for the code, actually I have tried two different code and they all have the same output. So I chose one of them to post~

      Like

Comments are closed.