Skip to content

Instantly share code, notes, and snippets.

@bhagman
Created July 23, 2014 15:06
Show Gist options
  • Save bhagman/0baae0c8f37ee37f0f61 to your computer and use it in GitHub Desktop.
Save bhagman/0baae0c8f37ee37f0f61 to your computer and use it in GitHub Desktop.
// 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