Skip to content

Commit badc569

Browse files
committed
added doEvery()
1 parent 8035d6e commit badc569

File tree

4 files changed

+84
-19
lines changed

4 files changed

+84
-19
lines changed

README.md

Lines changed: 40 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,42 @@
11
# Utilities
22

3-
This is a collection of useful functions for the Arduino Framerwork, like:
4-
- printArray()
5-
- pinModeGroup()
6-
- digitalWriteGroup()
7-
- digitalToggle()
8-
- digitalToggleGroup()
9-
- splitString()
10-
11-
Upload the [example](/examples/basic/basic.ino) to see them in action 🔥
3+
This is a collection of useful functions for the Arduino Framerwork.
4+
5+
Upload the [example](/examples/basic/basic.ino) to see the usage 🔥
6+
7+
# Functions
8+
9+
#### doEvery()
10+
Run a task using without blocking code every given time
11+
12+
#### pinModeGroup()
13+
Change the state of a group of pins
14+
15+
#### digitalWriteGroup()
16+
Write the state of a group of pins
17+
18+
#### digitalToggle()
19+
Toggle the state of a pin
20+
21+
#### digitalToggleGroup()
22+
Toggle the state of a group of pins
23+
24+
#### echo()
25+
Echo between two serial ports, bi or mono directional
26+
27+
#### printArray()
28+
Print an array of any kind
29+
30+
#### splitString()
31+
Split a cstring into token and get one of them
32+
33+
# Macros
34+
35+
#### LEN
36+
Get the exact size of any array
37+
38+
#### TO_FAHRENHEIT
39+
Convert Celsius to Fahrenheit
40+
41+
#### TO_CELSIUS
42+
Convert Fahrenheit to Celsius

Utilities.h

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,25 @@
1010
#endif
1111

1212
// MACROS
13+
#define LEN(x) ((sizeof(x) / sizeof(0 [x])) / ((size_t)(!(sizeof(x) % sizeof(0 [x]))))) // complex but safe macro for the lenght
1314
#define TO_FAHRENHEIT(x) x * 1.8 + 32
1415
#define TO_CELSIUS(x) (x - 32) * 0.55
15-
#define LEN(x) ((sizeof(x) / sizeof(0 [x])) / ((size_t)(!(sizeof(x) % sizeof(0 [x]))))) // complex but safe macro for the lenght
16-
#define maybe 2 // true or false is not enough
16+
#define maybe 2 // true or false is not enough
17+
18+
// Run a task using without blocking code every given time
19+
template <class T1, class T2>
20+
bool doEvery(T1 *start_time, T2 interval)
21+
{
22+
if (millis() > *start_time + interval)
23+
{
24+
*start_time = millis();
25+
return true;
26+
}
27+
else
28+
{
29+
return false;
30+
}
31+
}
1732

1833
// Change the state of a group of pins
1934
void pinModeGroup(uint8_t pins[], size_t len, uint8_t state)

examples/basic/basic.ino

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ void setup()
55
Serial.begin(115200);
66
delay(1000);
77

8-
// Create a group of pin
9-
uint8_t group[] = {LED_BUILTIN, 12, 11, 10, 9, 8};
8+
// Create a group of pins
9+
byte group[] = {LED_BUILTIN, 12, 11, 10, 9, 8};
1010

1111
// Print that group
1212
printArray(group, LEN(group));
@@ -20,14 +20,14 @@ void setup()
2020
digitalWriteGroup(group, LEN(group), LOW);
2121

2222
// Toggle the state of a pin
23-
for (uint8_t n = 0; n < 5; n++)
23+
for (byte n = 0; n < 5; n++)
2424
{
2525
digitalToggle(LED_BUILTIN);
2626
delay(500);
2727
}
2828

2929
// Or toggle a group of pins
30-
for (uint8_t n = 0; n < 5; n++)
30+
for (byte n = 0; n < 5; n++)
3131
{
3232
digitalToggleGroup(group, LEN(group));
3333
delay(500);
@@ -44,9 +44,12 @@ void setup()
4444
Serial.println(substring); // this will output "test"
4545

4646
// A more complex array for printArray
47-
uint8_t array[] = {1, 0x56, 0b1011};
47+
int array[] = {1, 11, 89, 34, 9};
4848

49-
// Print the array as hexadecimal values with a ":" between the items and invert it (from the last to the first)
49+
// Print the array as decimal values with a ", " between the items and invert them (from the last to the first)
50+
printArray(array, LEN(array), ", ", DEC, true);
51+
52+
// Print the array as hexadecimal values with a ":" between the items and invert them (from the last to the first)
5053
printArray(array, LEN(array), ":", HEX, true);
5154

5255
// Print the array as binary values with a newline ("\n") between the items and write the index before every element
@@ -56,4 +59,19 @@ void setup()
5659
//echo(&Serial, &Serial2);
5760
}
5861

59-
void loop() {}
62+
unsigned long task1, task2;
63+
64+
void loop()
65+
{
66+
// This will be run every 3500 milliseconds (3.5 seconds)
67+
if (doEvery(&task1, 3500))
68+
{
69+
Serial.println("Hello, from task1");
70+
}
71+
72+
// This every 10 seconds
73+
if (doEvery(&task2, 10000))
74+
{
75+
Serial.println("Also from task2!");
76+
}
77+
}

keywords.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#######################################
1010
# Methods and Functions (KEYWORD2)
1111
#######################################
12+
doEvery KEYWORD2
1213
pinModeGroup KEYWORD2
1314
digitalWriteGroup KEYWORD2
1415
digitalToggle KEYWORD2
@@ -20,7 +21,7 @@ splitString KEYWORD2
2021
#######################################
2122
# Constants (LITERAL1)
2223
#######################################
24+
LEN LITERAL1
2325
TO_FAHRENHEIT LITERAL1
2426
TO_CELSIUS LITERAL1
25-
LEN LITERAL1
2627
maybe LITERAL1

0 commit comments

Comments
 (0)