Created
July 23, 2014 15:06
-
-
Save bhagman/0baae0c8f37ee37f0f61 to your computer and use it in GitHub Desktop.
This file contains 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
// Low frequency output on an arbitrary pin | |
// | |
// Brett Hagman ([email protected]) | |
// (or [email protected]) | |
const int lfPin = 6; | |
// Number of milliseconds between flipping the pin state | |
// 30 Hz = twice every 1/30 second | |
// Frequency = f | |
// time between flips = (1/f)/2 in seconds | |
// = 1/(2*f) | |
// = 1/(2*f) * 1000000 in microseconds | |
// = 1000000/(2*f) | |
// = 500000/x microseconds | |
#define FlipTime(x) (500000L / x) | |
#define myFrequency 30 | |
void setup() | |
{ | |
pinMode(lfPin, OUTPUT); | |
} | |
void loop() | |
{ | |
static uint32_t lastFlip = 0; | |
static bool state = false; | |
if ((micros() - lastFlip) > FlipTime(myFrequency)) | |
{ | |
state = !state; | |
digitalWrite(lfPin, state); | |
lastFlip = micros(); | |
} | |
// You can do whatever you want here, but remember that it can't | |
// take up too much time (i.e. less than FlipTime(x)) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment