Arduino Uno on/off signal with a varied duty cycle

I am having no luck. the highest output i can get is about 47KHz however I also lose my duty cycle accuracy. I need to read a analog input, when the input is negative i need to fire 1 output at 100KHz with a varied duty cycle. when the input is positive I need to fire a different output at 100KHz also with a varied duty cycle. I do not think that I can use the delay function because then I could miss the positive to negative transition on the input. Any help with this would be greatly appreciated. It is my Sr. project and I have spent countless hours trying different approaches to make this work.

This is my current code. It is a simple program just trying to get my desired output frequency.

int Power_to_Mos=3; int Duty_Cycle_1=.9; int convert=1000; int Freq=(1/100000)*convert;

void setup() {

// put your setup code here, to run once:



void loop() { // put your main code here, to run repeatedly: digitalWrite(Power_to_Mos,HIGH); delay(.000009); //Freq*Duty_Cycle_1 is eventually what will ne in the () digitalWrite(Power_to_Mos,LOW); delay(.000001); //Freq*(1-Duty_Cycle_1) is eventually what will be in the ()



AKA delay(0)

The goal is to delay 9 micro seconds which would be the on period for a .9 duty cycle with 100 KHz frequency.

You could look at delayMicroseconds, but it's not that accurate, and you will not achieve 100 kHz. (You forgot to account for the time for digitalWrite to execute, and the overhead of calling loop() ) You should be looking at using the timers.


You already posted this same question, and we told you how to do it over here, and you have clearly not read our responses

I have a special place for cross-posters.

Thread locked.