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); } }
Good work, Yufei! Next time, try embedding your vimeo videos in your blog post (look at Sherry’s post last week for example: https://makingstudio.blog/2018/09/18/week-2-homework-2/). As for your code, can you think of any way to avoid repeating the analogWrite and brightness adjustment portions? Perhaps the button could be made to control fadeAmount?
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~