Hi, I'm Becky Stern,
and today I'm going to walk you through building your own unique Arduino project
for the first time.
After you've tried a bunch of the built-in downloadable examples,
as we've covered in previous episodes,
the next step in your Arduino journey is to combine Arduino
sample sketches together to make a working prototype of your idea.
Figuring out how to write the code for your project can be super intimidating,
but here's my process for breaking it down.
First, write out your project's primary purpose.
If it has numerous features, you'll have to decide which are absolutely
necessary versus those that would be nice but aren't needed at first.
You can always add your “nice to have” features later,
but as a beginner you should start with the simplest version.
Next list the project's inputs and outputs.
A pass key lock might have a keypad input,
some kind of an LED display output, and a solenoid output.
Internet projects might have a cloud service as an input output or both,
but if it's really your first time building an Arduino project,
I recommend saving the IoT stuff for projects two or three.
The project I'm building today is a webcam privacy device
that uses a button and a real-time clock for inputs,
and outputs to a servo motor.
The next step is to write an outline of your program's main loop.
This is called pseudocode and it should map out
the cause-and-effect relationships you want in your program.
For example, if the button is pressed, then move the servo to the open position
and if the timer is passed, then move the servo to its close position.
When it comes to choosing the electronic components for your project,
ask yourself if each of the inputs and outputs needs to be digital- “on or off,”
analog- “along a scale,” or something else,
like this servo motor that requires a code library to get it working.
Not all product documentation is created equal.
So my best advice to beginners is to shop for components that have good tutorials
and sample code such as those mentioned in the built-in examples, to start,
or from reputable library sample code.
Look up examples and tutorials for each component of your project
and build and run each one at a time before attempting to combine their code.
So for example,
in my webcam privacy device project,
I've got a momentary push button input to manually operate the device
a potentiometer to adjust the time it stays open
and a real-time clock to keep accurate track of that time.
Then there's the servo motor.
So I've got three inputs and one output.
So I'll start with the built in Arduino example DigitalReadSerial
and wire up my push button, then observe the serial output
to confirm my button is working and wired correctly.
It's easier to discover that something is wired incorrectly at this stage
When you're working with code that's known to work with the component at hand,
rather than trying to debug wiring and new code at the same time,
then when that's working,
I'll add the potentiometer to my circuit board and open up a different code example
AnalogReadsSerial and observe that serial output
to confirm my knob circuit.
As we've covered in a previous episode, basic digital and analog code can be used
with a variety of different sensors, many times with little to no modification.
So I could substitute the push for a PIR motion sensor, for example,
or the knob for a slider or a force-sensitive resistor.
Having that serial output can help figure out if the digital logic
needs to be reversed or the sensor range mapped to suit the output.
For the real time clock,
A library needs to be downloaded
to communicate with the device, and that library comes with example code
found in the Examples menu after you've installed the library.
Lastly, it's time to test the servo motor, which requires
a library that happens to come pre-installed with the Arduino software.
Now it's time to create a new sketch that will operate all the inputs
and the outputs and apply the logic that makes the features work.
This is a little bit tricky.
Sometimes there's an example that does something similar to what you want already,
and you can build on that, or you can start with
any one of the basic testers you ran in the last step.
The basic idea is to add together all of the required bits,
like library includes and setup stuff,
In fact, it’s a smart thing to paste your pseudocode right into your program as comments.
That way you can always see in plain language what you are trying to do.
Comments also make it easier for somebody to pick up where you left off later,
even if that somebody is you.
You'll want to compile your new program after every important change,
just like testing the hardware one component at a time,
compiling often will catch and isolate errors
as you make them, like a misplaced semicolon or a curly brace
If you keep making changes and try to fix all the errors later,
it'll be harder to isolate and fix each one.
I'll go into more error troubleshooting detail in a future episode.
When your program achieves important milestones,
save your code to a new version to preserve your last working state.
It's kind of like preserving your last save state at an important checkpoint in a video game.
You might end up changing some elements of your project
as you progress, and that's totally natural.
Once you get a good basic prototype working,
then you can start adding in the extra features you wrote down earlier.
This process of incrementally building up your project will make it come together more easily,
and more importantly, it'll be more fun and less frustrating.
And if you do get frustrated, take a break.
So if you're a student, don't wait until the last minute.
You'd be surprised what fresh eyes can do to improve your code.
I’ve put some links to resources in the description.
Leave your advice about translating your ideas
into a working prototype in the comments so we can all learn together.
Check out the playlist with the rest of the series and subscribe
to be sure you don't miss the next one.