Monday, February 7, 2011

Event-Based Library for Arduino

After I worked in my first Arduino programs, maybe because I worked for a good time with languages like Java and C#, I have found that is a job a little outdated. Tasks like to handle push of buttons or turn a LED on smoothly through PWM, although of being easy to understand, require a reasonable volume of code to become consistent, especially when is necessary that various components work simultaneously.

Versão em português desse artigo: Event-Based Library - Biblioteca de eventos para Arduino.

Because I suffer with this problem that I decided to turn all my difficulties in Arduino programming in a collection of classes that assist the programmer. Currently this collection includes the following components:

AnalogEvent

Event handler for analog ports that can be used to read potentiometers or others sensors. Implements the event onChange.

In this example we configure the analog pin 1 to read values from a potentiometer with an hysteresis value of 3:

#include <AnalogEvent.h>

void setup() {
  AnalogEvent.addAnalogPort(1,        //potentiometer pin
                            onChange, //onChange event function
                            3);       //hysteresis
  
  Serial.begin(9600);
}

void loop() {
  AnalogEvent.loop();
}

void onChange(AnalogPortInformation* Sender) {
  Serial.print("Analog (pin:");
  Serial.print(Sender->pin);
  Serial.print(") changed to: ");
  Serial.print(Sender->value);
  Serial.println("!");
}

ButtonEvent

Event handler for buttons (tactile switches) on digital ports. Implements the events onUp, onDown, onHold and onDouble.

In this example we configure the pin 12 as a button with all events enabled:

#include <ButtonEvent.h>

void setup() {
  ButtonEvent.addButton(12,       //button pin
                        onDown,   //onDown event function
                        onUp,     //onUp event function
                        onHold,   //onHold event function
                        1000,     //hold time in milliseconds
                        onDouble, //double event function
                        200);     //double time interval

  Serial.begin(9600);
}

void loop() {
  ButtonEvent.loop();
}

void onDown(ButtonInformation* Sender) {
  Serial.print("Button (pin:");
  Serial.print(Sender->pin);
  Serial.println(") down!");
}

void onUp(ButtonInformation* Sender) {
  Serial.print("Button (pin:");
  Serial.print(Sender->pin);
  Serial.println(") up!");
}

void onHold(ButtonInformation* Sender) {
  Serial.print("Button (pin:");
  Serial.print(Sender->pin);
  Serial.print(") hold for ");
  Serial.print(Sender->holdMillis);
  Serial.println("ms!");
}

void onDouble(ButtonInformation* Sender) {
  Serial.print("Button (pin:");
  Serial.print(Sender->pin);
  Serial.print(") double click in ");
  Serial.print(Sender->doubleMillis);
  Serial.println("ms!");
}

LedControl

Simple LED functions with asynchronous capabilities. Permits turning on, off and dimmer.

In this example two LEDs blinking together in different manners. The LED in pin 9 is configured to 500ms of fade in/out with 1s of interval, and the LED at pin 8 is a simple blinker with 200ms of interval:

#include <LedControl.h>

void setup() {
  LedControl.startBlink(9,1000,500);
  LedControl.startBlink(8,200);
}

void loop() {
  LedControl.loop();
}

This project has its own repository with more information about the classes, usage instructions and download options. The project name is ebl-arduino, that means Event-Based Library for Arduino, and your page could be accessed through the URL http://code.google.com/p/ebl-arduino/.

No comments:

Post a Comment