Introduction
When starting an Arduino project, I like to ensure that I understand what I am doing. I have been coding for a while, and my courses at CSUB were mostly in C++. Therefore, when I am learning projects in the Arduino Projects Book and coding, I like to not just copy the code but describe what is happening and then attempt to code the solution myself.
Building The Circuit
I start by looking at the schematic view in the book and then the illustration. I try to insert the wires using the grid on the breadboard. They don’t always line up with the picture. For example, in the book the black, red, and white wires are in a different order than the ones that come on the servo provided in the kit. I always double-check that all my wires are in the correct place logically and not just based on the pictures.
The servo motor comes with female connectors, so I added these header pins.
Coding
After completing the circuit and making sure that it was wired correctly, I read through the coding section. I wrote down a quick and messy description of what is happening in the code. I would then use this to program it without looking at the book so that I could learn the syntax to program an Arduino. I only allowed myself to look at the Arduino documentation.
I began to code. After getting stuck for a bit on a section, I referred to the book and covered the rest of the code with a sticky note.
I heard somewhere that before, when screens were smaller and there was no autocomplete, short concise names were important. However, now this was not a necessity, self documenting code became a thing, and long descriptive names were beneficial. I use this as an excuse to have descriptive variable and funtion names. See how I made the variable names longer when writing them out myself.
#include <Servo.h>
Servo myServo;
const int potentiometerPin = A0;
int potentiometerValue;
int angle;
void setup() {
myServo.attach(9);
Serial.begin(9600);
}
void loop() {
int potentiometerValue = myServo.read();
Serial.println(potentiometerValue);
}
I was stuck for a little bit and then realized that my potentiometerValue
does not come from myServo
, so I needed to change that. I forgot and referred to the documentation, then changed myServo.read()
to analogRead()
. Again, I was not looking at the book or using AI.
I ended up with the following code. When I compared it to the original, I noticed that I had written const int instead of int const, which is something I now have engrained in me because of this mistake.
I heard from popular programming YouTuber ThePrimeagen that he doesn’t have all these algorithms memorized line by line. He just remembers the path to them so he can recreate them. This is the approach I take in learning many programming concepts. I memorized what map()
did here, and I was able to recall it later. It needed a value and then the upper and lower bounds for the “from” and the “to.” So, from 0 and 1023 to 0 and 179. These may seem like random numbers, but they are derived from the analog signal (1024) and 180 degrees.
#include <Servo.h>
Servo myServo;
const int potentiometerPin = A0;
int potentiometerValue;
int angle;
void setup() {
myServo.attach(potentiometerPin);
Serial.begin(9600);
}
void loop() {
int potentiometerValue = analogRead(potentiometerPin);
Serial.println(potentiometerValue);
angle = map(potentiometerValue, 0, 1023, 0, 179);
myServo.write(angle);
delay(15);
}
Troubleshooting & Success
When I finished and uploaded the code, nothing moved. I thought I had messed up the wiring somehow. I also checked my serial connection, and it was giving me weird jumps in numbers. I remembered that when I initially connected it again with the new circuit, it still had the old code stored on it, and the servo was jittering. So I loaded the previous program again. It did nothing, so I thought I had broken the motor. Then I loaded the current program again, and it started to work. That managed to refresh the code on the Arduino and it worked with the same code.
Learning About Capacitors
I understood the circuit but not the point of the capacitors in this case. They were new to me, so I researched them. I saw them explained in parallel to water and a storage tank: if flow is cut to the storage tank, the flow from the storage tank outwards still continues. I realized that their purpose here was to remove jitters, since servo motors may go from no power to needing a lot all of a sudden.
I learned that voltage allows us to see the difference between two points. Capacitance is measured in farads, and for circuit boards usually in microfarads. The voltage rating on the capacitor is the maximum voltage the capacitor can handle, and if it exceeds this it may explode.
Top comments (0)