Week 02: One wrong resistor made me question my entire life


This assignment and the experiments required some C++, which wasn't a big problem for me. I just needed to get used to the syntax again since I hadn't written C++ in a long time. The logic is basically the same.
However, I ran into a problem that I couldn't figure out at first: I had connected the wrong resistor. I spent quite a while trying to find out what was wrong, and as a result, the signal was really unstable. I'll explain more about what happened below.
During the experiments with 1/0 state switching, pressure sensors, peak detection, and potentiometers, I also started to have some questions about how microcontrollers work.
I didn't notice that I was using the wrong resistor—the 220Ω and 10kΩ ones got mixed up

In the example, the resistor marked in the red box is a 10kΩ resistor connected in series with the button. However, I used a 220Ω resistor instead. They honestly look very similar, so I didn't notice the difference at first.
Because of this, D2 was not being pulled to HIGH or LOW consistently, which caused the signals I printed to be different from what I expected. Using the formula to calculate the current through the two different resistors, I got 15mA with the 220Ω resistor and 0.33mA with the 10kΩ resistor. The difference is actually pretty significant.
After replacing the resistor with the correct 10kΩ one, everything worked as expected. However, this problem took me a long time to figure out because I kept overlooking the fact that the resistor connected in series with the button in the example was 10kΩ.
Getting my hands dirty
After solving the problem, I did'nt run into many issues with the smaller experiments. I just checked the circuit and resistor, figured out whether to use a digital (D) or analog (A) pin, set it as an input or output, and wrote the code. That was pretty much it.
Why do we need to set a digital (D) pin as an input or output, but not an analog (A) pin?

I asked GPT to help me put together a table, and learned that analog (A) pins are commonly used to measure changing values, such as those from potentiometers, pressure sensors, light sensors, and temperature sensors. Digital (D) pins are used when a value has one of two possible states, such as HIGH or LOW.
Why is Serial.begin(9600) often the first line in setup()?
'Serial.begin(9600)' tells the Arduino to start serial communication at a baud rate of 9600. What does 9600 mean? It is the baud rate, which you can think of as the agreed-upon communication speed between the Arduino and the computer.
First, the Arduino initializes the communication channel with the computer and configures the pins it will use to interact with the circuit. Then 'loop()' can repeatedly read the button state.