初始化提交

This commit is contained in:
王立帮
2024-07-20 22:09:06 +08:00
commit c247dd07a6
6876 changed files with 2743096 additions and 0 deletions

View File

@@ -0,0 +1,155 @@
/*
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 |
| | |
| 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"
// The actions I ca do...
typedef enum {
ACTION_OFF, // set LED "OFF".
ACTION_ON, // set LED "ON"
ACTION_SLOW, // blink LED "SLOW"
ACTION_FAST // blink LED "FAST"
}
MyActions;
#if defined(ARDUINO_AVR_UNO) || defined(ARDUINO_AVR_NANO_EVERY)
// Example for Arduino UNO with input button on pin 2 and builtin LED on pin 13
#define PIN_INPUT A1
#define PIN_LED 13
#elif defined(ESP8266)
// Example for NodeMCU with input button using FLASH button on D3 and using the led on -12 module (D4).
// This LED is lighting on output level LOW.
#define PIN_INPUT D3
#define PIN_LED D4
#endif
// Setup a new OneButton on pin A1.
OneButton button(PIN_INPUT, true);
MyActions nextAction = ACTION_OFF; // no action when starting
// 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
// link the myClickFunction function to be called on a click event.
button.attachClick(myClickFunction);
// link the doubleclick function to be called on a doubleclick event.
button.attachDoubleClick(myDoubleClickFunction);
// set 80 msec. debouncing time. Default is 50 msec.
button.setDebounceTicks(80);
} // setup
// main code here, to run repeatedly:
void loop() {
unsigned long now = millis();
// keep watching the push button:
button.tick();
// You can implement other code in here or just wait a while
if (nextAction == ACTION_OFF) {
// do nothing.
digitalWrite(PIN_LED, LOW);
} else if (nextAction == ACTION_ON) {
// turn LED on
digitalWrite(PIN_LED, HIGH);
} else if (nextAction == ACTION_SLOW) {
// do a slow blinking
if (now % 1000 < 500) {
digitalWrite(PIN_LED, LOW);
} else {
digitalWrite(PIN_LED, HIGH);
} // if
} else if (nextAction == ACTION_FAST) {
// do a fast blinking
if (now % 200 < 100) {
digitalWrite(PIN_LED, LOW);
} else {
digitalWrite(PIN_LED, HIGH);
} // if
} // if
} // loop
// this function will be called when the button was pressed 1 time and them some time has passed.
void myClickFunction() {
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 myDoubleClickFunction() {
if (nextAction == ACTION_ON) {
nextAction = ACTION_SLOW;
} else if (nextAction == ACTION_SLOW) {
nextAction = ACTION_FAST;
} else if (nextAction == ACTION_FAST) {
nextAction = ACTION_ON;
} // if
} // myDoubleClickFunction
// End

View File

@@ -0,0 +1,177 @@
/*
InterruptOneButton.ino - Example for the OneButtonLibrary library.
This is a sample sketch to show how to use the OneButtonLibrary
to detect double-click events on a button by using interrupts.
The library internals are explained at
http://www.mathertel.de/Arduino/OneButtonLibrary.aspx
Good article on Arduino UNO interrupts:
https://arduino.stackexchange.com/questions/30968/how-do-interrupts-work-on-the-arduino-uno-and-similar-boards
... and the processor datasheet.
Setup a test circuit:
* Connect a pushbutton to pin A1 (ButtonPin) and ground.
* The pin 13 (StatusPin) 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 the functions (singleClick, doubleClick) to the events.
In the loop function the button.tick function must be called as often as you like.
By using interrupts the internal state advances even when for longer time the button.tick is not called.
*/
// 03.03.2011 created by Matthias Hertel
// 01.12.2011 extension changed to work with the Arduino 1.0 environment
// 04.11.2017 Interrupt version created.
// 04.11.2017 Interrupt version using attachInterrupt.
#include "OneButton.h"
#if defined(ARDUINO_AVR_UNO) || defined(ARDUINO_AVR_NANO_EVERY)
// Example for Arduino UNO with input button on pin 2 and builtin LED on pin 13
// attachInterrupt only supports pin 2 and 3 on UNO.
// See https://www.arduino.cc/reference/en/language/functions/external-interrupts/attachinterrupt/
#define PIN_INPUT 2
#define PIN_LED 13
#elif defined(ESP8266)
// Example for NodeMCU with input button using FLASH button on D3 and using the led on -12 module (D4).
// This LED is lighting on output level LOW.
#define PIN_INPUT D3
#define PIN_LED D4
#endif
// Setup a new OneButton on pin PIN_INPUT
// The 2. parameter activeLOW is true, because external wiring sets the button to LOW when pressed.
OneButton button(PIN_INPUT, true);
// current LED state, staring with LOW (0)
int ledState = LOW;
// save the millis when a press has started.
unsigned long pressStartTime;
// In case the momentary button puts the input to HIGH when pressed:
// The 2. parameter activeLOW is false when the external wiring sets the button to HIGH when pressed.
// The 3. parameter can be used to disable the PullUp .
// OneButton button(PIN_INPUT, false, false);
// This function is called from the interrupt when the signal on the PIN_INPUT has changed.
// do not use Serial in here.
#if defined(ARDUINO_AVR_UNO) || defined (ARDUINO_AVR_NANO_EVERY)
void checkTicks()
{
// include all buttons here to be checked
button.tick(); // just call tick() to check the state.
}
#elif defined(ESP8266)
ICACHE_RAM_ATTR void checkTicks()
{
// include all buttons here to be checked
button.tick(); // just call tick() to check the state.
}
#endif
// this function will be called when the button was pressed 1 time only.
void singleClick()
{
Serial.println("singleClick() detected.");
} // singleClick
// this function will be called when the button was pressed 2 times in a short timeframe.
void doubleClick()
{
Serial.println("doubleClick() detected.");
ledState = !ledState; // reverse the LED
digitalWrite(PIN_LED, ledState);
} // doubleClick
// this function will be called when the button was pressed 2 times in a short timeframe.
void multiClick()
{
Serial.print("multiClick(");
Serial.print(button.getNumberClicks());
Serial.println(") detected.");
ledState = !ledState; // reverse the LED
digitalWrite(PIN_LED, ledState);
} // multiClick
// this function will be called when the button was pressed 2 times in a short timeframe.
void pressStart()
{
Serial.println("pressStart()");
pressStartTime = millis() - 1000; // as set in setPressTicks()
} // pressStart()
// this function will be called when the button was pressed 2 times in a short timeframe.
void pressStop()
{
Serial.print("pressStop(");
Serial.print(millis() - pressStartTime);
Serial.println(") detected.");
} // pressStop()
// setup code here, to run once:
void setup()
{
Serial.begin(115200);
Serial.println("One Button Example with interrupts.");
// enable the led output.
pinMode(PIN_LED, OUTPUT); // sets the digital pin as output
digitalWrite(PIN_LED, ledState);
// setup interrupt routine
// when not registering to the interrupt the sketch also works when the tick is called frequently.
attachInterrupt(digitalPinToInterrupt(PIN_INPUT), checkTicks, CHANGE);
// link the xxxclick functions to be called on xxxclick event.
button.attachClick(singleClick);
button.attachDoubleClick(doubleClick);
button.attachMultiClick(multiClick);
button.setPressTicks(1000); // that is the time when LongPressStart is called
button.attachLongPressStart(pressStart);
button.attachLongPressStop(pressStop);
// A1-Option for UNO:
// it is possible to use e.g. A1 but then some direct register modifications and an ISR has to be used:
// You may have to modify the next 2 lines if using another pin than A1
// PCICR |= (1 << PCIE1); // This enables Pin Change Interrupt 1 that covers the Analog input pins or Port C.
// PCMSK1 |= (1 << PCINT9); // This enables the interrupt for pin 1 of Port C: This is A1.
} // setup
// A1-Option for UNO:
// The Interrupt Service Routine for Pin Change Interrupt 1
// This routine will only be called on any signal change on A1: exactly where we need to check.
// ISR(PCINT1_vect)
// {
// // keep watching the push button:
// button.tick(); // just call tick() to check the state.
// }
// main code here, to run repeatedly:
void loop()
{
// keep watching the push button, even when no interrupt happens:
button.tick();
// You can implement other code in here or just wait a while
delay(10);
} // loop
// End

View File

@@ -0,0 +1,83 @@
/*
This is a sample sketch to show how to use the OneButtonLibrary
to detect double-click events on a button.
The library internals are explained at
http://www.mathertel.de/Arduino/OneButtonLibrary.aspx
Setup a test circuit:
* Connect a pushbutton to pin A1 (ButtonPin) and ground.
* The pin 13 (StatusPin) 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 the functions (singleClick, doubleClick) to the events.
In the loop function the button.tick function must be called as often as you like.
*/
// 03.03.2011 created by Matthias Hertel
// 01.12.2011 extension changed to work with the Arduino 1.0 environment
#include "OneButton.h"
#if defined(ARDUINO_AVR_UNO) || defined(ARDUINO_AVR_NANO_EVERY)
// Example for Arduino UNO with input button on pin 2 and builtin LED on pin 13
#define PIN_INPUT 2
#define PIN_LED 13
#else if defined(ESP8266)
// Example for NodeMCU with input button using FLASH button on D3 and using the led on -12 module (D4).
// This LED is lighting on output level LOW.
#define PIN_INPUT D3
#define PIN_LED D4
#endif
// Setup a new OneButton on pin PIN_INPUT
// The 2. parameter activeLOW is true, because external wiring sets the button to LOW when pressed.
OneButton button(PIN_INPUT, true);
// In case the momentary button puts the input to HIGH when pressed:
// The 2. parameter activeLOW is false when the external wiring sets the button to HIGH when pressed.
// The 3. parameter can be used to disable the PullUp .
// OneButton button(PIN_INPUT, false, false);
// current LED state, staring with LOW (0)
int ledState = LOW;
// setup code here, to run once:
void setup()
{
Serial.begin(115200);
Serial.println("One Button Example with polling.");
// enable the standard led on pin 13.
pinMode(PIN_LED, OUTPUT); // sets the digital pin as output
// enable the standard led on pin 13.
digitalWrite(PIN_LED, ledState);
// link the doubleclick function to be called on a doubleclick event.
button.attachDoubleClick(doubleClick);
} // setup
// main code here, to run repeatedly:
void loop()
{
// keep watching the push button:
button.tick();
// You can implement other code in here or just wait a while
delay(10);
} // loop
// this function will be called when the button was pressed 2 times in a short timeframe.
void doubleClick()
{
Serial.println("x2");
ledState = !ledState; // reverse the LED
digitalWrite(PIN_LED, ledState);
} // doubleClick
// End

View File

@@ -0,0 +1,70 @@
/*
* SpecialInput.ino - Example for the OneButtonLibrary library.
* This is a sample sketch to show how to use the OneClick library on other input sources than standard digital pins.
*
* The library internals are explained at
* http://www.mathertel.de/Arduino/OneButtonLibrary.aspx
*
* Setup a test circuit:
* * Connect a pushbutton to pin 2 (ButtonPin) and ground.
*
* The sketch shows how to setup the library and bind the functions (singleClick, doubleClick) to the events.
* In the loop function the button.tick function must be called as often as you like.
*
* * 22.01.2021 created by Matthias Hertel
*/
#include "OneButton.h"
// This is an example on how to use the OneClick library on other input sources than standard digital pins.
// 1. do not use a pin in the initialization of the OneClick library.
// 2. pass the input state to the tick function.
// You can also find how to create an instance in setup and not by declaration.
// You can also find how to use inline callback functions.
#if defined(ARDUINO_AVR_UNO) || defined(ARDUINO_AVR_NANO_EVERY)
#define PIN_INPUT 2
#else if defined(ESP8266)
#define PIN_INPUT D3
#endif
// OneButton instance will be created in setup.
OneButton *button;
void fClicked(void *s)
{
Serial.print("Click:");
Serial.println((char *)s);
}
void setup()
{
Serial.begin(115200);
Serial.println("One Button Example with custom input.");
// create the OneButton instance without a pin.
button = new OneButton();
// Here is an example on how to use a parameter to the registered function:
button->attachClick(fClicked, "me");
// Here is an example on how to use an inline function:
button->attachDoubleClick([]() { Serial.println("DoubleClick"); });
// setup your own source of input:
pinMode(PIN_INPUT, INPUT_PULLUP);
} // setup()
void loop()
{
// read your own source of input:
bool isPressed = (digitalRead(PIN_INPUT) == LOW);
// call tick frequently with current push-state of the input
button->tick(isPressed);
} // loop()

View File

@@ -0,0 +1,142 @@
/*
This is a sample sketch to show how to use the OneButtonLibrary
to detect click events on 2 buttons in parallel.
The library internals are explained at
http://www.mathertel.de/Arduino/OneButtonLibrary.aspx
Setup a test circuit:
* Connect a pushbutton to pin A1 (ButtonPin) and ground.
* Connect a pushbutton to pin A2 (ButtonPin) and ground.
* The Serial interface is used for output the detected button events.
The Sketch shows how to setup the library and bind 2 buttons to their functions.
In the loop function the button1.tick and button2.tick functions have to be called as often as you like.
*/
// 01.03.2014 created by Matthias Hertel
// ... and working.
/* Sample output:
Starting TwoButtons...
Button 1 click.
Button 2 click.
Button 1 doubleclick.
Button 2 doubleclick.
Button 1 longPress start
Button 1 longPress...
Button 1 longPress...
Button 1 longPress...
Button 1 longPress stop
Button 2 longPress start
Button 2 longPress...
Button 2 longPress...
Button 2 longPress stop
*/
#include "OneButton.h"
// Setup a new OneButton on pin A1.
OneButton button1(A1, true);
// Setup a new OneButton on pin A2.
OneButton button2(A2, true);
// setup code here, to run once:
void setup() {
// Setup the Serial port. see http://arduino.cc/en/Serial/IfSerial
Serial.begin(115200);
while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo only
}
Serial.println("Starting TwoButtons...");
// link the button 1 functions.
button1.attachClick(click1);
button1.attachDoubleClick(doubleclick1);
button1.attachLongPressStart(longPressStart1);
button1.attachLongPressStop(longPressStop1);
button1.attachDuringLongPress(longPress1);
// link the button 2 functions.
button2.attachClick(click2);
button2.attachDoubleClick(doubleclick2);
button2.attachLongPressStart(longPressStart2);
button2.attachLongPressStop(longPressStop2);
button2.attachDuringLongPress(longPress2);
} // setup
// main code here, to run repeatedly:
void loop() {
// keep watching the push buttons:
button1.tick();
button2.tick();
// You can implement other code in here or just wait a while
delay(10);
} // loop
// ----- button 1 callback functions
// This function will be called when the button1 was pressed 1 time (and no 2. button press followed).
void click1() {
Serial.println("Button 1 click.");
} // click1
// This function will be called when the button1 was pressed 2 times in a short timeframe.
void doubleclick1() {
Serial.println("Button 1 doubleclick.");
} // doubleclick1
// This function will be called once, when the button1 is pressed for a long time.
void longPressStart1() {
Serial.println("Button 1 longPress start");
} // longPressStart1
// This function will be called often, while the button1 is pressed for a long time.
void longPress1() {
Serial.println("Button 1 longPress...");
} // longPress1
// This function will be called once, when the button1 is released after beeing pressed for a long time.
void longPressStop1() {
Serial.println("Button 1 longPress stop");
} // longPressStop1
// ... and the same for button 2:
void click2() {
Serial.println("Button 2 click.");
} // click2
void doubleclick2() {
Serial.println("Button 2 doubleclick.");
} // doubleclick2
void longPressStart2() {
Serial.println("Button 2 longPress start");
} // longPressStart2
void longPress2() {
Serial.println("Button 2 longPress...");
} // longPress2
void longPressStop2() {
Serial.println("Button 2 longPress stop");
} // longPressStop2
// End