tone() vs. analogWrite() for Lasertag IR

Status
Not open for further replies.

Blitter

Member
I am currently working on a Lasertag project, using two Teensies (one for IR/Game management, and one for sound)
Project Video: https://www.flickr.com/photos/95492142@N00/20878723950
Populated PCB: https://www.flickr.com/photos/95492142@N00/20527739753

Currently, I am using a HEXFET on the Teensy 3.1's pin, to drive an IR LED (TSAL6100) @ 56Khz in SIRC encoding (600us pulses)
The IR is too strong for indoors, so I'd like to have variable control over the power of the LED, without having to resort to a potentiometer.

In code, I'm presently using the tone & noTone functions where IROUTPIN = 20, FREQUENCY = 56000, IRpulse = 600, and pulselength = 1 or 2.
From what I understand, this gives me a square wave with no control over the duty cycle:

Code:
void sendPulse(int pulseLength)
{
    // Send Bit 
    tone(IROUTPIN,FREQUENCY);
    delayMicroseconds(IRpulse * pulseLength); // (1200us = 2, 600us = 1)

    // Send 600us Break
    noTone(IROUTPIN);
    delayMicroseconds(IRpulse * pulseLength);
}


Could I have control of the duty cycle and thus the IR power, if I use analogWrite(), after it is setup with:

Code:
void setup() 
{
    analogWriteFrequency(IROUTPIN , FREQUENCY );
}

Are there any "gotchas" when doing it this way?
 
Last edited:
Using PWM will allow you to set the analogWriteFrequency (56kHz), the resolution analogWriteResolution (10bit) and the duty cycle with analogWrite (1 to 1023) independently. To stop the pulsing, it is enough to do an analogWrite of 0 to the pin.
 
Status
Not open for further replies.
Back
Top