Skip to content

Commit c2b9c2e

Browse files
committed
Unify Ticker examples.
1 parent 8abd8f9 commit c2b9c2e

File tree

4 files changed

+144
-27
lines changed

4 files changed

+144
-27
lines changed

libraries/Ticker/examples/Arguments/Arguments.ino

-27
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
Basic Ticker usage
3+
4+
Ticker is an object that will call a given function with a certain period.
5+
Each Ticker calls one function. You can have as many Tickers as you like,
6+
memory being the only limitation.
7+
8+
A function may be attached to a ticker and detached from the ticker.
9+
There are two variants of the attach function: attach and attach_ms.
10+
The first one takes period in seconds, the second one in milliseconds.
11+
12+
The built-in LED will be blinking.
13+
*/
14+
15+
#include <Ticker.h>
16+
17+
Ticker flipper;
18+
19+
int count = 0;
20+
21+
void flip() {
22+
int state = digitalRead(LED_BUILTIN); // get the current state of GPIO1 pin
23+
digitalWrite(LED_BUILTIN, !state); // set pin to the opposite state
24+
25+
++count;
26+
// when the counter reaches a certain value, start blinking like crazy
27+
if (count == 20) {
28+
flipper.attach(0.1, flip);
29+
}
30+
// when the counter reaches yet another value, stop blinking
31+
else if (count == 120) {
32+
flipper.detach();
33+
}
34+
}
35+
36+
void setup() {
37+
pinMode(LED_BUILTIN, OUTPUT);
38+
digitalWrite(LED_BUILTIN, LOW);
39+
40+
// flip the pin every 0.3s
41+
flipper.attach(0.3, flip);
42+
}
43+
44+
void loop() {
45+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
#include <Arduino.h>
2+
#include <Ticker.h>
3+
4+
#define LED1 2
5+
#define LED2 4
6+
#define LED3 12
7+
#define LED4 14
8+
#define LED5 15
9+
10+
11+
class ExampleClass {
12+
public:
13+
ExampleClass(int pin, int duration) : _pin(pin), _duration(duration) {
14+
pinMode(_pin, OUTPUT);
15+
_myTicker.attach_ms(_duration, std::bind(&ExampleClass::classBlink, this));
16+
}
17+
~ExampleClass() {};
18+
19+
int _pin, _duration;
20+
Ticker _myTicker;
21+
22+
void classBlink() {
23+
digitalWrite(_pin, !digitalRead(_pin));
24+
}
25+
};
26+
27+
void staticBlink() {
28+
digitalWrite(LED2, !digitalRead(LED2));
29+
}
30+
31+
void scheduledBlink() {
32+
digitalWrite(LED3, !digitalRead(LED2));
33+
}
34+
35+
void parameterBlink(int p) {
36+
digitalWrite(p, !digitalRead(p));
37+
}
38+
39+
Ticker staticTicker;
40+
Ticker scheduledTicker;
41+
Ticker parameterTicker;
42+
Ticker lambdaTicker;
43+
44+
ExampleClass example(LED1, 100);
45+
46+
47+
void setup() {
48+
pinMode(LED2, OUTPUT);
49+
staticTicker.attach_ms(100, staticBlink);
50+
51+
pinMode(LED3, OUTPUT);
52+
scheduledTicker.attach_ms_scheduled(100, scheduledBlink);
53+
54+
pinMode(LED4, OUTPUT);
55+
parameterTicker.attach_ms(100, std::bind(parameterBlink, LED4));
56+
57+
pinMode(LED5, OUTPUT);
58+
lambdaTicker.attach_ms(100, []() {
59+
digitalWrite(LED5, !digitalRead(LED5));
60+
});
61+
}
62+
63+
void loop() {
64+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
Passing paramters to Ticker callbacks
3+
4+
Apart from void(void) functions, the Ticker library supports
5+
functions taking one argument. This argument's size has to be less or
6+
equal to 4 bytes (so char, short, int, float, void*, char* types will do).
7+
8+
This sample runs two tickers that both call one callback function,
9+
but with different arguments.
10+
11+
The built-in LED will be pulsing.
12+
*/
13+
14+
#include <Ticker.h>
15+
16+
Ticker tickerSetHigh;
17+
Ticker tickerSetLow;
18+
19+
void setPin(int state) {
20+
digitalWrite(LED_BUILTIN, state);
21+
}
22+
23+
void setup() {
24+
pinMode(LED_BUILTIN, OUTPUT);
25+
digitalWrite(1, LOW);
26+
27+
// every 25 ms, call setPin(0)
28+
tickerSetLow.attach_ms(25, setPin, 0);
29+
30+
// every 26 ms, call setPin(1)
31+
tickerSetHigh.attach_ms(26, setPin, 1);
32+
}
33+
34+
void loop() {
35+
}

0 commit comments

Comments
 (0)