Sunday, December 14, 2014

Arduino/Trinket - Controlling a 5 state program with a button and a led

The Trinket is $6.95 micro controller from Adafruit that is compatible with the Arduino.

I am starting creating building blocks which I will use to create future projects based on the Trinket.

Let's say I have an application running on my micro controller which have 5 states.
I need to be able to see in which state the app is and switch to the next state
I will use the on board led to represent the 5 states as
  1. No light
  2. Dim light
  3. Bright light
  4. Led flashing quickly
  5. Led flashing slowly


See Video

Source Code (The source code has been greatly improved in Part II)

/*
    Implement a 5 state button. By clicking on the button the button activate one 
    of the 5 state. A led display the 5 states.
    States
        0 - No ligth
        1 - Dim light
        2 - Bright ligt
        3 - Led flashing quickly
        4 - Led flashing slowly

    based on video Tutorial 02 for Arduino: Buttons, PWM, and Functions
    https://www.youtube.com/watch?v=_LCCGFSMOr4

    http://arduino.cc/en/Reference/HomePage

    Torres Frederic 2014.12.12
*/

#include "fArduino.h"

const int SWITCH_PIN                  = 0;
const int LED_PIN                     = 1;  // Red LED on the Trinket
const int LED_LIGTH_STEP_VALUES[]     = { 0, 20, 200, 255, 255 }; // Define the led intensity, 255 we will blink
const int LED_BLINKING_RATES[]        = { 100, 300 }; 

int         _ledLevelIndex            = 0;
const int   _blinking1LevelIndex      = 3;
const int   _blinking2LevelIndex      = 4;
boolean     _blinkingOn               = false;

boolean _buttonLastStateInLoop        = false;

BoardClass Board;

void setup() {

    Board.SetPinMode(SWITCH_PIN, INPUT);
    Board.SetPinMode(LED_PIN, OUTPUT);
}

void loop() {

    boolean buttonPressed = Board.GetButtonStateDebounced(SWITCH_PIN, _buttonLastStateInLoop);
    int ledLevel          = LED_LIGTH_STEP_VALUES[_ledLevelIndex];

    if (buttonPressed == true && _buttonLastStateInLoop == false) {

        _ledLevelIndex += 1;
        ledLevel = LED_LIGTH_STEP_VALUES[_ledLevelIndex];
        if (_ledLevelIndex >= ArraySize(LED_LIGTH_STEP_VALUES))
            _ledLevelIndex = 0;
    }

    _buttonLastStateInLoop = buttonPressed;

    if ((_ledLevelIndex == _blinking1LevelIndex) || (_ledLevelIndex == _blinking2LevelIndex)) {

        _blinkingOn = !_blinkingOn;
        Board.LedOn(LED_PIN, _blinkingOn, LED_BLINKING_RATES[_ledLevelIndex-3]);
    }
    else {
        Board.LedSet(LED_PIN, ledLevel);
    }
}
Next step, I will create a dedicated class name MultiStateButton, which can 
  • Manage the state
  • Handle the user input
  • Handle the user interface AKA the LED
  • Support Next and Previous state button
  • To implement the flashing light I use for now a delay(), which block the loop and is not good. I need to use a time counter to flash the light.
I am also using a C++ class named Board containing re-usable code, mostly to be able to write clean and maintainable code.
See my next blog post

No comments:

Post a Comment