A large part of learning Arduino for most people, is getting started with coding. These lessons offer explorations of some of the core concepts of reading and writing Arduino code.
Arduino Uno (but most any type of Arduino or clone will do)
USB cable (to attach the Arduino to the computer)
**Any collaboration on writing or editing the main documents of this lesson would be appreciated. Direct message Corey Rice to engage in improving these primary documents.
Background information and springboard for following exercises
This document was written in an effort to get students started programming with Arduino. In my high school level Engineering course, later work is contingent upon being able to read and write code for Arduino.
Students should read this document to get oriented with the Arduino IDE and understand some of the overarching concepts. Toward the end of the document, there is some supporting information that leads students through the corresponding exercises. Each of those exercises have their own separate document, which can be assigned to students via Google Classroom, Schoology, or whatever your local Learning Management System is used at the institution where you teach. Nontraditional learning institutions may want to simply point self-motivated students to the document linked above, and let students go at their own pace. Same for any adult who wants to learn.
The corresponding exercises are a series of investigations of (mostly) separated topics, that students can work on at their own pace. These exercises lend themselves to student-driven individualized learning, and full student engagement. Ideally, a classroom teacher could assign these exercises and then let the students work individually. The teacher would then spend the class time answering questions as students come upon them, rather than delivering whole-group lectures on these computer science concepts. It is critical that students be encouraged to try experimenting and tinkering with the code in order to determine how it works.
Since students will advance at their own pace, the time for any student to complete all of these assignments can vary quite a bit. However, it is reasonable to expect a student to finish these assignments faster as they progress through them. About two hours per exercise is a moderate expectation for high school students who are reasonably engaged and responding to every item. *Students will need to know how to take a screenshot on the computer they are using, if they will turn in these exercises as written.
Getting oriented while working on something fairly familiar
Students will complete the first exercise, which requires them to get oriented in using the Arduino with their computer and the IDE. It also has them get acquainted with the format of the upcoming exercises and the processes required while they still work on concepts that should be largely familiar to them. (The hope is that the math itself is not the stumbling block for students, and they focus on mastering the Arduino/ Computer Science tasks in this exercise.)
The basics of 'if()' and 'else' as handled by Arduino
The concepts covered in this exercise may be the least familiar to many students new to computer science. For that reason, it may take some longer than they expect and more effort. This is the time for the teacher to take on the role of cheerleader and encourage students to keep trying to experiment with the code on the Arduino and Serial Monitor until they reach an understanding of how these concepts work.
The simplest conditional loop
While loops are common in physical computing, and so they deserve to be ‘near the front’ whenever working with Arduino. These exercises also teach the core concepts of looping functions, which is needed to fully understand the ‘void loop()’ that prominently appears in Arduino programming. This exercise will likely take much less time than the Boolean logic exercise that preceded it.
Making and using lists of data
Lists of information are a part of how humans understand things. Computer Science handles lists with arrays (among other things). Once mastered, arrays are incredibly valuable ways to control complex things or multi-step actions.
This concept will likely have students re-investigating the syntax needed to control things. Since arrays have their own syntactic rules for setting and getting values, this is to be expected.
An increment based loop, with a planned exit
For loops are a great way to control things in programs and, paired with the power of arrays, they can provide some of the most elegant solutions to the problems of physical computing.
At this point students may have hit their stride with these exercises, or be in serious need of encouragement. Ideally, students should be able to start seeing larger patterns in programs and be asking more abstract questions about what is going on in a given example program.
The power to simplify programs by writing reusable functions
Any time something needs to be done repeatedly or segmented into pieces, functions can save the day. This also serves as a reason to further investigate the ‘void setup()’ and ‘void loop()’ functions of Arduino. As well as a primer for more advanced concepts like Object Oriented Programming, Variable Scope, and Arduino Libraries.
This concept is more abstract than the previous exercises, and the teacher should expect students to start asking questions about what is going on with the code. Progress may slow.
Not all variables are global, and that's a good thing
Variables are one of the first concepts you need for these exercises, but they have some advanced concepts attached to them. Understanding the difference between global variables and local variables is a key concept to implementing User Defined Functions and more advanced program structures.
Some style choices can be made while writing code, that do more than make it look good
As you get better at writing code, there are some good moves you can make to improve the code you write. These things include: well placed comments, clever naming of variables, writing code the minimum number of times, and consolidating values to a minimum of variables.
"A Classy Solution" --Adafruit.com
After a person learns how to write code that follows in chronological order, they will eventually want to consolidates their code for maintainability and expand-ability. OOP lays the foundation for all Arduino Libraries and many other programming languages.
If everything students learned before this exercise was learning to add and subtract, learning OOP is analogous to learning how to multiply and divide. [Functional programming is analogous the derivative and integral.]
These are the most advanced concepts covered by these exercises. Expect students to struggle, and have many questions. If you are running these exercises as a unit with a time limit, not all students may make it through this exercise.
Many nice people have already written code, and shared it with you
The good news is that this exercise is much easier than the one that preceded it. All a student needs here is to see how to implement some OOP, not how to write it from nothing.