Creating My First Arduino Library: SteppedSequence Journey
Written on
Chapter 1: Introduction to SteppedSequence
This is the narrative behind SteppedSequence, the inaugural Arduino library I published on GitHub. The journey began with a project that required orchestrating a series of actions involving servos and GPIO pins. During my research, I realized that there wasn't a comprehensive library available for implementing a stepped sequence in Arduino. Consequently, I decided to create my own solution.
While the library currently caters to my specific project, I aim to enhance it further to accommodate diverse applications.
Section 1.1: Library Structure Overview
My library is structured around several key components:
- SteppedSequence: This is the primary object, functioning as the sequence controller. Users can add steps and initiate the sequence by calling Start() and then Run() within the loop.
- Step: Each step in the sequence comes with its own set of conditions and actions. Actions are executed simultaneously once all conditions are satisfied.
- StepAction: This element can consist of one or more steps, with two types of actions currently available:
- StepActionGpio: This action manages a GPIO pin as an output. You can define the pin, its active state, and the duration.
- StepActionServo: This action controls a servo using the Servo.h library, specifying the servo pin, target angle, hold angle, and speed.
- StepCondition: In addition to actions, steps can also include conditions. Actions will only execute once all conditions are fulfilled. There are currently two types of conditions:
- StepConditionDigitalInput: This condition waits for a digital input, such as a button press or an external signal.
- StepConditionDelay: This condition monitors whether a specified duration has elapsed, effectively introducing a delay between steps.
Section 1.2: Example Implementation
Here is a simple example of how to implement the SteppedSequence library:
#include <SteppedSequence.h>
#define START_BUTTON_PIN 2
#define IS_START_BUTTON_PULLUP true
#define PRE_SEQUENCE_DELAY 1000
#define GREEN_LED_PIN 13
#define YELLOW_LED_PIN 12
#define RED_LED_PIN 11
#define GREEN_DURATION 5000
#define YELLOW_DURATION 2000
#define RED_DURATION 5000
SteppedSequence sequence;
Step Step1;
StepConditionDigitalInput Step1Condition(START_BUTTON_PIN, IS_START_BUTTON_PULLUP);
Step Step2;
StepConditionDelay Step2Condition(PRE_SEQUENCE_DELAY);
StepActionGpio Step2Action(GREEN_LED_PIN, HIGH, GREEN_DURATION);
Step Step3;
StepActionGpio Step3Action(YELLOW_LED_PIN, HIGH, YELLOW_DURATION);
Step Step4;
StepActionGpio Step4Action(RED_LED_PIN, HIGH, RED_DURATION);
void setup() {
Serial.begin(9600);
Step1.AddCondition(&Step1Condition);
Step2.AddCondition(&Step2Condition);
Step2.AddAction(&Step2Action);
Step3.AddAction(&Step3Action);
Step4.AddAction(&Step4Action);
sequence.AddStep(&Step1);
sequence.AddStep(&Step2);
sequence.AddStep(&Step3);
sequence.AddStep(&Step4);
sequence.BeginAll();
sequence.Start();
}
void loop() {
static uint16_t lastStep = -1;
sequence.Run();
uint16_t currentStep = sequence.GetCurrentStepIndex();
if (currentStep != lastStep) {
Serial.print("Running step: ");
Serial.println(currentStep);
lastStep = currentStep;
}
}
Chapter 2: Conclusion
Having recently published my first Arduino library, I am open to any suggestions or contributions from the community.
In this video, titled "Create an Arduino Library (Step by Step)," the process of developing an Arduino library is explained in detail, perfect for beginners.
The video "#71 How to create an Arduino Library - easy!" provides an accessible guide for those looking to dive into Arduino library creation.