Using Arduino to generate two digital ouptut with different frequencies

Hello,

I am a newbie in arduino programming and I would like to use it to trigger a LED and a camera. For correcting for dark noise and stray light in the camera, I was thinking to take two pictures with LED completely OFF and two pictures with LED modulated ON and OFF at a high frequency. The high frequency modulation of the LED is mainly for avoiding heating since I am going to run it with quite a high current. A scheme of the signals I would like to send to both the camera (Dig 1) and the LED (Dig 2) is shown on the figure in attachment. Both trigger on the high level of the digital signals. I read about using port manipulation to get well synchronized and simultaneous output for the LED and camera, and I found this link http://www.jeremyblum.com/2010/09/05/driving-5-speakers-simultaneously-with-an-arduino/, which explain how to generate 5 digital signals with different frequencies using timers and interrupts. So I am well equipped to start BUT I do not know at all how to stop the generation of the LED signal as explained on my drawing. I guess I would have to use another interrupt, or make a kind of digital mask but I do not see how.
Any ideas on how to do that? or any link ?

Thanks in advance,

Gregory

synchro_LED_Camera.bmp (1.48 MB)

Assuming you have a Uno or similar you have 3 hardware timers, each of which is capable of outputting a wide range of frequencies with different length duty cycles. You can set the timers up to do these things and turn on an output pin (you can turn it on and off with pinMode). You wouldn't need interrupts or masks in this case.

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

The high frequency modulation of the LED is mainly for avoiding heating since I am going to run it with quite a high current.

That won't be any brighter than reducing the current to the same fraction as you pulse width modulation index, so don't bother adding the extra complication.

[quote author=Nick Gammon link=topic=187136.msg1385276#msg1385276 date=1378762913] Assuming you have a Uno or similar you have 3 hardware timers, each of which is capable of outputting a wide range of frequencies with different length duty cycles. You can set the timers up to do these things and turn on an output pin (you can turn it on and off with pinMode). You wouldn't need interrupts or masks in this case.

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

[/quote]

Hi,

Thanks for the answer. It seems that this is what i need. I have a mega 2560, but every thing should be quite similar except from some pin output?

Gregory

Slow speed, don't need interrupts for that. Just track # of 10mS intervals

void loop(){
currentMillis = millis();
if ( (currentMillis - previousMillis) >=10mS){  // defined as: unsigned long 10mS = 10; might get better results with micros() vs millis() and using = 10000UL
  previousMillis = previousMillis + 10mS;  
  PINC = PINC | 0b00010000;  // toggle PORTC bit 4 as 10mS pin - as an example
  50mSBurstCount = 50mSBurstCount +1;
   if (50mSBurstCount == 5){
    PINC = PINC | 0b00000100; // toggle PortC, bit 2 as digit 1
   50mSBurstCount = 0; // reset the count
   }

2000mSBurstCount = 2000mSBurstCount +1;
   if (2000mSBurstCount == 200){
   PINC = PINC | 0b00001000; // toggle PortC, bit 3 as digit 2
   2000mSBurstCount = 0;
   }
 }
// do other stuff while waiting for 10mS intervals
}

[quote author=Nick Gammon link=topic=187136.msg1385276#msg1385276 date=1378762913] Assuming you have a Uno or similar you have 3 hardware timers, each of which is capable of outputting a wide range of frequencies with different length duty cycles. You can set the timers up to do these things and turn on an output pin (you can turn it on and off with pinMode). You wouldn't need interrupts or masks in this case.

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

[/quote]

Hei Nick,

I have been looking at one of your code, the one entitled "Modulating a 38 kHz carrier with a 500 Hz signal", which corresponds exactly to what i want to do, but as you said it is written for an Arduino Uno. I am using the Arduino Mega, and it seems there is quite a mess on the numbering of the pin and timer attribution between the different arduino version. I manage to make a 38 khz signal and output it, but modulate it using another timer does not work. Mainly because i do not manage to find the right timer and pin number I guess. Would you have any document/website that could help in translating your code such that it work for a mega 2650?

thanks in advance.

Greg

I just work from the datasheet for the processor. Grab one for the Atmega328 and the Atmega2560 (should be on the Atmel site if you can find them ) otherwise go to DigiKey or somewhere like that. They’ll have a copy.

I think you’ll find it very similar. The 2560 has 16-bit timers for 1, 3, 4, and 5. So that means 0 and 2 are still 8 bit timers.

Probably the biggest challenge will be finding the output pins (eg. OC1A). Look for the reference schematic (from this site) to see which pins go to which headers on the Mega.

I just had a closer look. The problem with that sketch is it outputs a 500 Hz cycle on Timer 2 with a variable duty cycle and uses a pin change interrupt to detect when that toggles. However OC2B on the Mega doesn’t cause a pin-change interrupt.

It might need quite a bit of reworking because of that. Still, you could do the 500 Hz toggling “manually” with a slight loss of precision. For example, set up Timer 2 to simply count up for the “on” time, and cause an interrupt when it matches. Toggle the PWM output of 38 kHz, and then set up a different count for Timer 2 (the “off” cycle) and repeat.

Or do it in hardware, output the 38 kHz continually, and the 500 Hz on another pin, and run them through an AND gate.