Fast timing circuit

Hi,

I’m trying to make 2 LEDs alternately blink at a very fast rate - up to about 350 Hz. I also need to control the duty cycle of blinking. Unfortunately, I haven’t been able to do it yet with everything I’ve tried. I’m using the Timer library by Simon Monk (http://www.simonmonk.org), and my code is:

#include <Timer.h>
#include <Event.h>
#include <Print.h>

const int LEDpin1 = 5;		//Red LED 1
const int LEDpin2 = 11;		//Green LED 1

int LEDstate1 = LOW;
int LEDstate2 = LOW;

//long previousTime = 0;
bool last = true;						// Stores which LED is to be turned (updated every time led flashes)
float freq1 = 329;//Hertz = 1/second
float dc = 0.40;						//duty Cycle ON (100*dc)% of the time

Timer t1;		//On timer for 1

void setup()
{
	/* Pin setup */
	pinMode(LEDpin1, OUTPUT);
	pinMode(LEDpin2, OUTPUT);

	/* Timer setup */
	t1.every(1000.0/freq1,flashOn);
	t1.after(dc*1000.0/freq1, delaySetup);
	
}

void delaySetup()
{
	t1.every(1000.0/freq1,flashOff);
}

void loop()
{
	t1.update();
}


void flashOn()
{
	//digitalWrite(LEDpin1, HIGH);
	if (last)
	{
		// LED 1
		digitalWrite(LEDpin1, HIGH);
	}
	else
	{
		// LED 2
		digitalWrite(LEDpin2, HIGH);
	}
}

void flashOff()
{
	//digitalWrite(LEDpin1, LOW);

	if (last)
	{
		// LED 1
		digitalWrite(LEDpin1, LOW);
		last = false;
	}
	else
	{
		// LED 2
		digitalWrite(LEDpin2, LOW);
		last = true;
	}
}

I’m using a Timer to set a time to turn the LED on, and another to turn it off (after a delay). Other than that, I think the code is pretty simple. A frequency I have to use is 82.407 Hz. When I run the program, I get a flashing rate of 77.1 Hz. Any ideas on why this could be happening? And what can I do to try to flash at the correct frequency? Could using 1 timer for everything cause this?

Thank you very much for any help,
Dhruv

Is this something to do with guitars?

Yep. I need the LEDs to alternately flash at the rate of the string's vibration.

  int8_t every(unsigned long period, void (*callback)(void));
  int8_t every(unsigned long period, void (*callback)(void), int repeatCount);
  int8_t after(unsigned long duration, void (*callback)(void));
  int8_t oscillate(uint8_t pin, unsigned long period, uint8_t startingValue);
  int8_t oscillate(uint8_t pin, unsigned long period, uint8_t startingValue, int repeatCount);
  int8_t pulse(uint8_t pin, unsigned long period, uint8_t startingValue);

From the Timer class. Notice that there is not float or double in sight.

Thank you PaulS. So does this mean that I can’t get any more accuracy in timing than 1ms (since unsigned long period is an int)?

Dhruv

So does this mean that I can't get any more accuracy in timing than 1ms (since unsigned long period is an int)?

Using the Timer class? Yes. There are other timer libraries that might provide better resolution, and, of course you can diddle with the Arduino timers, too.

Once you blink the LEDs much more than 24 Hz, I’m pretty sure they will just look on all the time, and just appear to be brighter or darker depending on the duty cycle. Is that what you are after?

Sounds like he is going for a strobe effect (like the strobes you use to time car engines).

I have a sketch near the bottom of this page:

http://www.gammon.com.au/forum/?id=11504

It generates a 38 KHz pulse using a timer with variable duty cycle. To get to 82.407 Hz you would just need to fiddly with the constants and maybe the prescaler. You probably won't get precisely that figure, but fairly close.

Hi,

Thank you everyone for the responses. It appears that PaulS was correct in telling me the long in that timer class is the issue. I suppose I now have to learn how to use the bit-based timers built into Arduino.

Thank you for all the help. Dhruv