/*
BlinkMachine.ino
This is a sample sketch to show how to use the OneButtonLibrary to detect double-click events on a button.
Copyright (c) by Matthias Hertel, http://www.mathertel.de
This work is licensed under a BSD style license. See http://www.mathertel.de/License.aspx
More information on: http://www.mathertel.de/Arduino
The library internals are explained at
http://www.mathertel.de/Arduino/OneButtonLibrary.aspx
Setup a test circuit:
* Connect a pushbutton to pin A1 (Uno) or D3 (ESP8266) and ground.
* The pin 13 (UNO) or D4 (ESP8266) is used for output attach a led and resistor to ground
or see the built-in led on the standard arduino board.
The Sketch shows how to setup the library and bind a "machine" that can blink the LED slow or fast.
A click on the button turns the led on.
A doubleclick on the button changes the blink rate from ON to SLOW to FAST and back.
In the loop function the button.tick function has to be called as often as you like.
State-Diagram
start
| +-------\
V V |
-------- ------ |
| OFF |<--click-+->| ON | |
-------- | ------ |
^ | | |
| | d-click |
longpress | | |
| V |
| ------ |
+- | SLOW | |
| ------ |
| | |
| d-click |
| | |
| V d-click
| ------ |
+--| FAST |---/
------
*/
// 06.10.2012 created by Matthias Hertel
// 26.03.2017 state diagram added, minor changes
#include "OneButton.h"
#include "OneButtonTiny.h" // This example also works with reduced OneButtonTiny class saving.
// The actions I ca do...
typedef enum {
ACTION_OFF, // set LED "OFF".
ACTION_ON, // set LED "ON"
ACTION_SLOW, // blink LED "SLOW"
ACTION_SLOW2, // blink LED "SLOW"
ACTION_FAST, // blink LED "FAST"
ACTION_FAST2 // blink LED "FAST"
} MyActions;
typedef enum {
ACTION1_OFF, // set LED "OFF".
ACTION1_ON, // set LED "ON"
ACTION1_SLOW, // blink LED "SLOW"
ACTION1_SLOW2, // blink LED "SLOW"
ACTION1_FAST, // blink LED "FAST"
ACTION1_FAST2 // blink LED "FAST"
} MyActions1;
#define PIN_INPUT1 12
#define PIN_INPUT2 14
#define PIN_LED 27
#define PIN_LED2 26
#define RELEASE 4
// Setup a new OneButton on pin PIN_INPUT1.
// OneButton button(PIN_INPUT1, true);
// Setup a new OneButton on pin PIN_INPUT2.
OneButton button1(PIN_INPUT1, true);
// Setup a new OneButton on pin PIN_INPUT2.
OneButton button2(PIN_INPUT2, true);
MyActions nextAction = ACTION_OFF; // no action when starting
MyActions1 nextAction1 = ACTION1_OFF; // no action when starting
const int m1Foward = 25;
const int m1Backward = 33;
const int m2Foward = 32;
const int m2Backward = 35;
// setup code here, to run once.
void setup() {
// enable the standard led on pin 13.
pinMode(PIN_LED, OUTPUT); // sets the digital pin as output
pinMode(PIN_LED2, OUTPUT); // sets the digital pin as output
pinMode(m1Foward, OUTPUT); // sets the digital pin as output
pinMode(m1Backward, OUTPUT); // sets the digital pin as output
pinMode(m2Foward, OUTPUT); // sets the digital pin as output
pinMode(m2Backward, OUTPUT); // sets the digital pin as output
pinMode(RELEASE, OUTPUT); // sets the digital pin as output
// link the button 1 functions.
button1.attachClick(click1);
button1.attachDoubleClick(doubleclick1);
button1.attachLongPressStop(longPressStop1);
// link the button 2 functions.
button2.attachClick(click2);
button2.attachDoubleClick(doubleclick2);
button2.attachLongPressStop(longPressStop2);
// set 80 msec. debouncing time. Default is 50 msec.
button1.setDebounceMs(80);
digitalWrite(m1Foward, LOW);
digitalWrite(m1Backward, LOW);
digitalWrite(m2Foward, LOW);
digitalWrite(m2Backward, LOW);
} // setup
// main code here, to run repeatedly:
void loop() {
unsigned long now = millis();
// keep watching the push button:
button1.tick();
button2.tick();
nextActionR();
nextActionS();
}
//------------------------------------------------------------------------------------------------------------
// You can implement other code in here or just wait a while
void nextActionR() {
unsigned long now = millis();
if (nextAction == ACTION_OFF) {
digitalWrite(PIN_LED, LOW);
} else if (nextAction == ACTION_ON) {
digitalWrite(PIN_LED, HIGH);
} else if (nextAction == ACTION_SLOW) {
if (now % 4000 < 1000) {
digitalWrite(m1Foward, LOW);
} else {
digitalWrite(m1Foward, HIGH);
} // if
} else if (nextAction == ACTION_SLOW2) {
digitalWrite(m1Foward, LOW);
digitalWrite(m1Foward, HIGH);
} // if
else if (nextAction == ACTION_FAST) {
if (now % 3000 < 500) {
digitalWrite(m1Foward, LOW);
} else {
digitalWrite(m1Foward, HIGH);
}
} else if (nextAction == ACTION_FAST2) {
if (now % 2000 < 500) {
digitalWrite(m1Foward, LOW);
} else {
digitalWrite(m1Foward, HIGH);
}
} // if
} // loop
//------------------------------------------------------------------------------------------------------------
void nextActionS() {
unsigned long now = millis();
if (nextAction1 == ACTION1_OFF) {
digitalWrite(PIN_LED2, LOW);
} else if (nextAction1 == ACTION1_ON) {
digitalWrite(PIN_LED2, HIGH);
} else if (nextAction1 == ACTION1_SLOW) {
if (now % 4000 < 4000) {
digitalWrite(m2Foward, LOW);
digitalWrite(RELEASE, HIGH);
} else {
digitalWrite(m2Foward, HIGH);
digitalWrite(RELEASE, LOW);
}
} else if (nextAction1 == ACTION1_SLOW2) {
if (now % 6000 < 2000) {
digitalWrite(m2Foward, LOW);
digitalWrite(RELEASE, HIGH);
} else {
digitalWrite(m2Foward, HIGH);
digitalWrite(RELEASE, LOW);
}
} else if (nextAction1 == ACTION1_FAST) {
if (now % 3000 < 500) {
digitalWrite(m2Foward, LOW);
digitalWrite(RELEASE, HIGH);
} else {
digitalWrite(m2Foward, HIGH);
digitalWrite(RELEASE, LOW);
}
} else if (nextAction1 == ACTION1_FAST2) {
if (now % 2000 < 500) {
digitalWrite(m2Foward, LOW);
digitalWrite(RELEASE, HIGH);
} else {
digitalWrite(m2Foward, HIGH);
digitalWrite(RELEASE, LOW);
}
} // if
} // loop
//------------------------------------------------------------------------------------------------------------
// this function will be called when the button was pressed 1 time and them some time has passed.
void click1() {
if (nextAction == ACTION_OFF)
nextAction = ACTION_ON;
else
nextAction = ACTION_OFF;
} // myClickFunction
// this function will be called when the button was pressed 2 times in a short timeframe.
void doubleclick1() {
if (nextAction == ACTION_ON) {
nextAction = ACTION_SLOW;
} else if (nextAction == ACTION_SLOW) {
nextAction = ACTION_SLOW2;
} else if (nextAction == ACTION_SLOW2) {
nextAction = ACTION_FAST;
} else if (nextAction == ACTION_FAST) {
nextAction = ACTION_FAST2;
} else if (nextAction == ACTION_FAST2) {
nextAction = ACTION_ON;
} // if
} // myDoubleClickFunction
void longPressStop1() {
nextAction = ACTION_OFF;
digitalWrite(m1Foward, LOW);
} // myLongPressFunction
//------------------------------------------------------------------------------------------------------------
void click2() {
if (nextAction1 == ACTION1_OFF)
nextAction1 = ACTION1_ON;
else
nextAction1 = ACTION1_OFF;
} // myClickFunction
void doubleclick2() {
if (nextAction1 == ACTION1_ON) {
nextAction1 = ACTION1_SLOW;
} else if (nextAction1 == ACTION1_SLOW) {
nextAction1 = ACTION1_SLOW2;
} else if (nextAction1 == ACTION1_SLOW2) {
nextAction1 = ACTION1_FAST;
} else if (nextAction1 == ACTION1_FAST) {
nextAction1 = ACTION1_FAST2;
} else if (nextAction1 == ACTION1_FAST2) {
nextAction1 = ACTION1_ON;
} // if
} // myDoubleClickFunction
void longPressStop2() {
nextAction1 = ACTION1_OFF;
digitalWrite(m2Foward, LOW);
} // myLongPressFunction
// End