First see Part I and Part II.
See Video
In part III I added a second button that change the app state backward and updated the class MultiStateButton to handle the Previous button.
The source code is on github at fArduino
#include "fArduino.h"
const int SWITCH_PIN = 0; // should be 0
const int SWITCH_PIN_PREVIOUS = 2;
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, 400 };
const int _blinking1LevelIndex = 3;
const int _blinking2LevelIndex = 4;
Led _onBoardLed(LED_PIN); // The led representing the User Interface
MultiStateButton _5StatesButton(SWITCH_PIN, &_onBoardLed, ArraySize(LED_LIGTH_STEP_VALUES), &LED_LIGTH_STEP_VALUES[0]);
void setup() {
Board.SetPinMode(SWITCH_PIN, INPUT);
Board.SetPinMode(SWITCH_PIN_PREVIOUS, INPUT);
Board.SetPinMode(LED_PIN, OUTPUT);
_5StatesButton.SetPreviousButton(SWITCH_PIN_PREVIOUS);
}
void CheckToActivateBlinkingMode() {
if ((_5StatesButton.StateIndex == _blinking1LevelIndex) || (_5StatesButton.StateIndex == _blinking2LevelIndex)) {
_5StatesButton.LedInstance->SetBlinkMode(LED_BLINKING_RATES[_5StatesButton.StateIndex - 3]);
}
else {
if (_5StatesButton.LedInstance->IsBlinking()) {
_5StatesButton.LedInstance->SetBlinkModeOff();
}
}
}
/// See blog post about using 2 switch
// http://www.varesano.net/blog/fabio/serial-communication-arduino-and-processing-simple-examples-and-arduino-based-gamepad-int
void loopBoth() {
boolean buttonPressed = _5StatesButton.GetButtonStateDebounced();
boolean previousButtonPressed = false;
if (buttonPressed == true && _5StatesButton.NextButtonLastStateInLoop == false) {
_5StatesButton.NextState();
CheckToActivateBlinkingMode();
}
else {
previousButtonPressed = _5StatesButton.GetPreviousButtonStateDebounced();
if (previousButtonPressed == true && _5StatesButton.PreviousButtonLastStateInLoop == false) {
_5StatesButton.PreviousState();
CheckToActivateBlinkingMode();
}
}
_5StatesButton.PreviousButtonLastStateInLoop = previousButtonPressed;
_5StatesButton.NextButtonLastStateInLoop = buttonPressed;
_5StatesButton.UpdateUI();
}
void loop() {
loopBoth();
}

No comments:
Post a Comment