Last active
December 22, 2015 07:59
-
-
Save vittee/4bb7db965d549738217d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// struct for pin | |
typedef struct { | |
uint8 pin; | |
uint8 state; | |
long lastUpdated; | |
long interval; | |
} pin_info_t; | |
// initialize pin info | |
pin_info_t pin9 = {9, LOW, 0, 1000}; | |
pin_info_t pin10 = {10, LOW, 0, 500}; | |
pin_info_t pin11 = {11, LOW, 0, 100}; | |
void setup() { | |
for (uint8 pin = 9; pin <= 11; pin++) { | |
pinMode(pin, OUTPUT); | |
} | |
} | |
void loop() { | |
/** | |
* this is an inline code and would result in duplicated code eventually when compiled | |
* we can also define this as a function but a function call will cause a stack manipulation and a JUMP/RETURN instructions | |
* which result in slower execution. | |
*/ | |
#define PIN_UPDATE(x) \ | |
if ((timeNow - x.lastUpdated) > x.interval) { \ | |
x.lastUpdated = millis(); \ // you may use "timeNow" instead, if accuracy is not a case. | |
digitalWrite(x.pin, x.state); \ // simply write state via digitalWrite() because HIGH/true are constants defined as 0x01, also LOW/false are 0x00 | |
x.state = !x.state; \ | |
} | |
// the above inline code refers to this symbol. | |
long timeNow = millis(); | |
PIN_UPDATE(pin9); | |
PIN_UPDATE(pin10); | |
PIN_UPDATE(pin11); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment