Project: Electronics Bash - Arduino #10 - Write Better Code


In this stream, we'll be focusing on useful coding structures and practices, including:

  • Arrays are your friend
  • Using delta-time avoids delay()
  • Switching program modes

This is the space where I'll be posting code samples and circuit layouts sometime in advance of the stream, for those who want to follow along more directly. You can expect the code and formatting here to change up to a couple hours before the stream as the plan for the evening comes together.

Batteries

None of the code related to battery usage will be terribly complicated - we'll be mostly making use of the Analog Read function and the Serial monitor.

Build Together - Sensor Monitor

The following pieces of code will make use of this circuit

Next week, we'll be looking at some fundamental coding patterns using some simple components that everyone should have in their starter kit, plus one additional sensor or switch. Below is a bit of starter code to test root components (LEDs, buttons, potentiometer) as a place to start

LED Testing

Try this code after installing the 5 LEDs and their resistors, to make sure they all work:

Click here to view code on Github

LEDs and Inputs

Once the LEDs, buttons, and potentiometer are installed, this bit of code tests them all. The 5 LEDs should chase a a speed determined by the position of the potentiometer, and the buttons should set the direction of the chase:

Click here to view code on Github

Final Fire Alarm Code

This is the code we wrote over the course of the evening's stream - it includes mapping sensor readings to individual threshhold levels, triggering an alarm state after being at a high level for a certain amount of time, and multiple conditions for exiting the alarm state back to the normal sensing state.

Click here to view code on Github