Skip to content

Commit 37a0fff

Browse files
Adding new function
- adding a new function for blink in a period
1 parent e503169 commit 37a0fff

File tree

5 files changed

+45
-6
lines changed

5 files changed

+45
-6
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ Available Examples
2020
* 04.SingleBlinkChangeFrequency
2121
* 05.MultipleBlinkWithoutDelay
2222
* 06.MultipleBlinkWithOffset
23+
* 07.BlinkInPeriod
2324

2425
How To Install the Library
2526
----------------------------
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/*
2+
* Created by ArduinoGetStarted.com
3+
*
4+
* This example code is in the public domain
5+
*
6+
* Tutorial page: https://arduinogetstarted.com/tutorials/arduino-output-library
7+
*
8+
* This example blinks LED:
9+
* + blink in 10 seconds and then turn off
10+
* + without using delay() function. This is a non-blocking example
11+
*/
12+
13+
#include <ezOutput.h> // ezOutput library
14+
15+
ezOutput led(7); // create ezOutput object that attach to pin 7;
16+
17+
18+
void setup() {
19+
led.blink(200, 200, 0, 10000); // 200 milliseconds ON, 200 milliseconds OFF, start immidiately, blink during 10000 milliseconds
20+
}
21+
22+
void loop() {
23+
led.loop(); // MUST call the led.loop() function in loop()
24+
}

library.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name=ezOutput
2-
version=1.0.0
2+
version=1.0.1
33
author=ArduinoGetStarted.com
44
maintainer=ArduinoGetStarted.com (ArduinoGetStarted@gmail.com)
55
sentence=Output library for Arduino

src/ezOutput.cpp

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ ezOutput::ezOutput(int pin) {
3838

3939
_highTime = 0;
4040
_lowTime = 0;
41+
_blinkTimes = -1;
4142
_lastBlinkTime = 0;
4243

4344
pinMode(_outputPin, OUTPUT);
@@ -61,19 +62,24 @@ void ezOutput::toggle(void) {
6162
digitalWrite(_outputPin, _outputState);
6263
}
6364

64-
void ezOutput::blink(unsigned long lowTime, unsigned long highTime, unsigned long startTime) {
65-
_highTime = highTime;
66-
_lowTime = lowTime;
67-
_startTime = startTime;
65+
void ezOutput::blink(unsigned long lowTime, unsigned long highTime, unsigned long startTime, long blinkTimes) {
66+
_highTime = highTime;
67+
_lowTime = lowTime;
68+
_startTime = startTime;
69+
_blinkTimes = blinkTimes;
6870

6971
if(_blinkState == BLINK_STATE_DISABLE) {
7072
_blinkState = BLINK_STATE_DELAY;
7173
_lastBlinkTime = millis();
7274
}
7375
}
7476

77+
void ezOutput::blink(unsigned long lowTime, unsigned long highTime, unsigned long startTime) {
78+
blink(lowTime, highTime, startTime, -1);
79+
}
80+
7581
void ezOutput::blink(unsigned long lowTime, unsigned long highTime) {
76-
blink(lowTime, highTime, 0);
82+
blink(lowTime, highTime, 0, -1);
7783
}
7884

7985
int ezOutput::getState(void) {
@@ -83,6 +89,9 @@ int ezOutput::getState(void) {
8389
void ezOutput::loop(void) {
8490
bool isBlink = false;
8591

92+
if(_blinkTimes == 0)
93+
_blinkState = BLINK_STATE_DISABLE;
94+
8695
switch(_blinkState) {
8796
case BLINK_STATE_DISABLE:
8897
return;
@@ -112,6 +121,9 @@ void ezOutput::loop(void) {
112121
_outputState = (_outputState == LOW) ? HIGH : LOW;
113122
digitalWrite(_outputPin, _outputState);
114123
_lastBlinkTime = millis();
124+
125+
if(_blinkTimes > 0)
126+
_blinkTimes--;
115127
}
116128
}
117129

src/ezOutput.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ class ezOutput
4848
unsigned long _highTime;
4949
unsigned long _lowTime;
5050
unsigned long _startTime;
51+
unsigned long _blinkTimes;
5152
unsigned long _lastBlinkTime; // the last time the output pin was blinked
5253

5354
public:
@@ -57,6 +58,7 @@ class ezOutput
5758
void toggle(void);
5859
void blink(unsigned long lowTime, unsigned long highTime);
5960
void blink(unsigned long lowTime, unsigned long highTime, unsigned long startTime);
61+
void blink(unsigned long lowTime, unsigned long highTime, unsigned long startTime, long blinkTimes);
6062
int getState(void);
6163
void loop(void);
6264
};

0 commit comments

Comments
 (0)