Thursday, October 28, 2021

Tips for Beginners: How to Eat an Elephant

That brand new Arduino of yours isn't going to code itself, is it? If you're a beginner at programming, or even just at Arduino, here's a tip that I know will help you out.

How to Eat an Elephant

If you want to make some cool project, take it a small step at a time. In my robotics classes kids that followed this approach finished faster than the ones who tried to program everything at once. 

Example

Let's say you have a project where you want to use a temperature sensor, an Arduino, and an OLED display to show the temperature. When you press a button, the display changes between Celsius and Fahrenheit.

Don't try to code all that in one shot. Instead, add tiny bits of functionality incrementally and get each new thing working before adding the next. Here is how I would approach such a project.

Step 1

First, make sure you can blink an LED. Why? You're making sure your new board works properly and that you can flash a new program to it.  In addition, the LED can be a debug signal for you in the next step.

Step 2

Next, read the state of a button. Wire up the button to one of your digital inputs with an external pull-up resistor or use INPUT_PULLUP. With a pull-up resistor, the button is pressed when the digital signal is low, and released when the signal is high. Change your blink code so that when the button is pressed, you light up the LED. When it isn't pressed, turn off the LED.

You already know the LED works so if there's a problem, you have fewer things to troubleshoot.

And that's why we started with blinking the LED.

Step 3

We'll work on the OLED display later. We want to work toward displaying the sensor reading so let's use Serial for now. Print a generic "hello world" every second using Serial.println().

In the next steps we can build on that to print out our sensor reading over Serial. Make sure you can see the message in the Arduino IDE Serial Monitor. Once this is working we can work on the sensor

Step 4

Our next step is to read the value from the sensor with analogRead() and print the result using Serial.println() and see if the value makes sense. Is it bouncing all over the place or always stuck at minimum or maximum? If so, troubleshoot. If it seems to hold more or less steady and changes according to temperature (try touching the sensor to raise the temperature), you're probably good to go.

Step 5

Figure out how to convert the value you read previously into Celsius and print that with Serial. You'll need to refer to a datasheet or tutorial for that particular sensor type. 

Step 6

Repeat step 5 but with Celsius.

Step 7

Work on displaying a generic "hello world" on your OLED display. Then try displaying a generic value. It doesn't have to look pretty just yet.

Step 8

Once you have that figured out, display the temperature in C. 

Step 9

Then figure out how to switch between C and F when the button is pressed. 

Step 10

Make the OLED display look prettier. Again break it down. Maybe learn how to display large numbers. Then work on displaying a thermometer graphic. Then work on displaying a bar graph within the thermometer. And so on.

So that's how you eat an elephant. The little bites make sure you don't have too many errors and bugs to deal with at once. It seems like every time I try to do too much at once, I'm the one getting bitten--in the butt.

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.