Timer functions with variable duty cycle

Hello all. I would like to do something particular with my Arduino, and I can’t quite figure it out. I’m using it for a data acquisition system. I want it to output a 30 Hz pulse to trigger a camera. Once a frame is captured, I’m using Matlab to perform some machine vision algorithms to compute the distance between two objects in the frame. That value gets passed back to the Arduino, and then I would like it to oscillate a pin based on the distance value it receives. I feel like I’m close, but I can’t quite get the result I need. I need the Arduino to oscillate the pin with a given period, but never have the pin on for more than 5ms. So I need to adjust the duty cycle as the period changes. I’ve been working with the timer.h library, which is great, but it doesn’t seem to handle duty cycles at all. I’ve tried to ask this questions on blogs dealing with that library directly, but no one there has been responsive.

Here is my code. Thank for any help. I really appreciate it.

#include “Timer.h”
Timer t;
Timer t2;

int ledPin=13; //Yellow LED
int cameraPin = 12; //Red LED

int frameRate = 500; //in milli
int ledRate = 500;

void setup()
{
pinMode(ledPin,OUTPUT);
pinMode(cameraPin,OUTPUT);
Serial.begin(9600);
t.oscillate(cameraPin, frameRate, HIGH);
t2.oscillate(ledPin, ledRate, HIGH);
}

void loop()
{
t.update();

if (Serial.available()>=2) //check for data to read

{
ledRate=Serial.read();
ledRate+=(Serial.read()<<8);
t2.oscillate(ledPin, ledRate, HIGH);
}
else

{
t.update();
t2.update();

}
}

I love that cool smiley in your code.

If only there were a way to post code here without it being mangled. Perhaps there could be special tags to use before and after code to prevent the mangling and present the code in a scrolling window. They could be called code tags and it would be useful if they could be inserted in the message editor by clicking on a button with a special symbol such as #. Of course, users would need advice on how to post code properly and that advice would need to be in a sticky post like Read this before posting a programming question ... - Programming Questions - Arduino Forum

If only .......

Hmmm, strange?!? That smiley doesn’t seem to be in my script and the code seems to compile just fine? Weird. Regardless, I’ve replaced the code below to see if that looks better.

You can see that we want to send a pulse to a camera to trigger a frame every frameRate. Then when the distance between objects is returned, we can set the pulse rate of an LED with ledRate. The issue is that the LED can only be pulsed for 5 ms at a time. So to keep that true but still alter the period, we need to modify the duty cycle with each case. Is this duable in reasonable manner? Thanks again for the help XD XD XD

#include "Timer.h"
#include <LiquidCrystal.h>

LiquidCrystal lcd(9, 8, 7, 6, 5, 4);

Timer t;

int ledPin=13; //Yellow LED
int cameraPin = 12; //Red LED

int frameRate = 30; //in milli
int ledRate = 1000;

void setup()
{
  lcd.begin(20, 2);
  lcd.clear();
  lcd.print("Distance = " + String(ledRate));
  pinMode(ledPin,OUTPUT);
  pinMode(cameraPin,OUTPUT);
  Serial.begin(9600);
  t.oscillate(cameraPin, frameRate, HIGH);  
  t.oscillate(ledPin, ledRate, HIGH);
}

void loop()
{
  t.update();
  
 if (Serial.available()>=2) //check for data to read

  {
    ledRate=Serial.read();
    ledRate+=(Serial.read()<<8);
    t.oscillate(ledPin, ledRate, HIGH);
    t.update();
    lcd.clear();
    lcd.print("Distance = " + String(ledRate));
    
  }
  else

  {
    t.update();
  }
}