Hello, I'm using PWM_DynamicFreq.ino for generating 2 different frequency

Ori1

Member
Hello, I'm using PWM_DynamicFreq.ino for generating 2 different frequency, What I'm trying to do is changing the delay between them around 1 to 10 ms.
I tried to do it with simple delay but it's not working for me. Can someone give any example for it?

Buy the way I'm using Teensy4.1
"
void loop()
{
delay(1000);
frequency = 20000.0f;
Serial.print(F("Change PWM Freq to "));
Serial.println(frequency);
PWM_Instance->setPWM(pinToUse, frequency, 20.0f);
printPWMInfo(PWM_Instance);
delay(1000);
frequency = 1000.0f;
Serial.print(F("Change PWM Freq to "));
Serial.println(frequency);
PWM_Instance->setPWM(pinToUse, frequency, 50.0f);
printPWMInfo(PWM_Instance);
}"
 

Attachments

  • PWM_DynamicFreq.ino
    2.2 KB · Views: 6
  • PWM1.png
    PWM1.png
    22.1 KB · Views: 8
If it works okay with a 1-second delay, but not with 10-ms delay, have you tried gradually reducing the value to see where it stops working? There is probably some limit, and you probably can't get too close to a 1-ms delay if you are using 1000 Hz.
 
I will try to explain better. The delay I need is between the 2 signals, to get a "0", for 1 to 10 milliseconds.
Its not working with 1 second delay between.
Please see the result that show that first frequency is ended and the second one begin immediately.
 
The delay I need is between the 2 signals, to get a "0", for 1 to 10 milliseconds.
If "0" means "no signal" for 1 to 10 ms, here is an example that seems to work. It repeats this cycle:
  • for 10 ms, frequency=1000(Hz) and duty cycle=50(%)
  • for 5 ms, frequency=1000 and duty cycle=0 (no signal)
  • for 10 ms, frequency=500 and duty cycle=50
Code:
#include "Teensy_PWM.h"

#define pinToUse              5

Teensy_PWM* PWM_Instance;
float frequency;
float duty_cycle;

void setup()
{
  Serial.begin(115200);
  while (!Serial && millis() < 5000);
  frequency = 10000.0f;
  duty_cycle = 50;
  PWM_Instance = new Teensy_PWM(pinToUse, frequency, duty_cycle);
}

void loop()
{
  frequency = 1000;
  duty_cycle = 50;
  PWM_Instance->setPWM(pinToUse, frequency, duty_cycle);
  delay(10);
 
  duty_cycle = 0;
  PWM_Instance->setPWM(pinToUse, frequency, duty_cycle);
  delay(5);

  frequency = 500;
  duty_cycle = 50;
  PWM_Instance->setPWM(pinToUse, frequency, duty_cycle);
  delay(10);
}

1712253883968.png
 
Thanks, I thought that I tried it before and for some reason got an error , But now it work when I set Duty Cycle to 0.
I have another question regarding it, How I can get the delay time and also the time when there is frequency more accurate in nano seconds?
 
Back
Top