problem with timer1

hi

i have wrote this . i expect to have 200hz pulse wave but the wave form is like the picture below .

where is my mistake ?

#include "TimerOne.h"
int n=0;
void setup() {
pinMode(12, OUTPUT);
Timer1.initialize(1000);
 waveon();
}

void loop() {
 

}

//......................................pulse generator {pin 12} ( to drive mosphet)
void waveon() {
  digitalWrite(12, LOW);
  Timer1.setPeriod(200);  
  Timer1.attachInterrupt(waveoff);
}
//............................................................
void waveoff() {
  digitalWrite(12, HIGH);
  ++n;
  if (n = 1 ) {
    Timer1.setPeriod(25);
    Timer1.attachInterrupt(n1);
  }
  if (n = 2 ) {
    Timer1.setPeriod(35);
    Timer1.attachInterrupt(n2);

  }
  if (n = 3 ) {
    Timer1.setPeriod(45);
    Timer1.attachInterrupt(n3);

  }
  if (n = 4 ) {
    Timer1.setPeriod(55);
    Timer1.attachInterrupt(n4);

  }
  if (n = 5 ) {
    Timer1.setPeriod(65);
    Timer1.attachInterrupt(n5);

  }
  if (n = 6 ) {
    Timer1.setPeriod(75);
    Timer1.attachInterrupt(n6);

  }

  
}






void n1() {
  Timer1.setPeriod(4775);
  //
  Timer1.attachInterrupt(waveon);
}
void n2() {
  Timer1.setPeriod(4765);
  //
  Timer1.attachInterrupt(waveon);
}
void n3() {
  Timer1.setPeriod(4755);
  //
  Timer1.attachInterrupt(waveon);
}
void n4() {
  Timer1.setPeriod(4745);
  //
  Timer1.attachInterrupt(waveon);
}
void n5() {
  Timer1.setPeriod(4735);
  //
  Timer1.attachInterrupt(waveon);
}
void n6() {
  Timer1.setPeriod(4725);
 // 
  Timer1.attachInterrupt(waveon);
  n=0;
}
[/td]
[/tr]
[/table]

![New Bitmap Image (3).jpg|700x608](upload://mDmhdmn8ISGnQ5cuzlYxzbborWY.jpeg)

Delta_G:

if (n = 1 ) {

Follow this link and see #3.

:slight_smile:

thank you .
i have solved it but the problem didnt solved yet.
what should i do ?

nobody have a solution ?

payam2000:
i have solved it but the problem didnt solved yet.
what should i do ?

Post the latest version of your program.

And please post your program using the code button </> so it looks like this. See How to use the Forum. It makes it much easier for people to help you

...R

here my latest code as you said , the problem is it do not work ! out put wave is like the picture below (12hz) instead of 200 hz ! and not stable
thank you for your help

#include "TimerOne.h"
int n=0;
void setup() {
pinMode(9, OUTPUT);
Timer1.initialize(1000);
 waveon();
}

void loop() {
 

}

//......................................pulse generator {pin 12} ( to drive mosphet)
void waveon() {
  digitalWrite(9, LOW);
  Timer1.setPeriod(200);  
  Timer1.attachInterrupt(waveoff);
}
//............................................................
void waveoff() {
  digitalWrite(9, HIGH);
  ++n;
  if (n==1) {
    Timer1.setPeriod(25);
    Timer1.attachInterrupt(n1);
  }
  if (n==2) {
    Timer1.setPeriod(35);
    Timer1.attachInterrupt(n2);

  }
  if (n==3) {
    Timer1.setPeriod(45);
    Timer1.attachInterrupt(n3);

  }
  if (n==4) {
    Timer1.setPeriod(55);
    Timer1.attachInterrupt(n4);

  }
  if (n==5) {
    Timer1.setPeriod(65);
    Timer1.attachInterrupt(n5);

  }
  if (n==6) {
    Timer1.setPeriod(75);
    Timer1.attachInterrupt(n6);

  }

  
}






void n1() {
  Timer1.setPeriod(4775);
  //
  Timer1.attachInterrupt(waveon);
}
void n2() {
  Timer1.setPeriod(4765);
  //
  Timer1.attachInterrupt(waveon);
}
void n3() {
  Timer1.setPeriod(4755);
  //
  Timer1.attachInterrupt(waveon);
}
void n4() {
  Timer1.setPeriod(4745);
  //
  Timer1.attachInterrupt(waveon);
}
void n5() {
  Timer1.setPeriod(4735);
  //
  Timer1.attachInterrupt(waveon);
}
void n6() {
  Timer1.setPeriod(4725);
 // 
  Timer1.attachInterrupt(waveon);
  n=0;
}

Image from Reply #5 so we don't have to download it. See this Image Guide

...R

IMHO something like this is a great deal simpler

void loop() {
    if (micros() - prevPulseMicros >= intervalMicros) {
        prevPulseMicros += intervalMicros;
        if (pulseState == HIGH) {
            digitalWrite(pulsePin, LOW);
            pulseState = LOW;
            intervalMicros = pulseLowMicros;
        }
        else {
            digitalWrite(pulsePin, HIGH);
            pulseState = HIGH;
            intervalMicros = pulseHIGHMicros;
        }
    }
}

just make sure that pulseHighMicros + pulseLowMicros = 5000

...R

thank you

but i have to read out put of an opamp after 25us , 35us ,....65us after pulse is high !
and i want to use timer 1 ! i will use other timers for other things
exp:

void n6() {
  Timer1.setPeriod(4725);
 // here i want to use analogread 
  Timer1.attachInterrupt(waveon);
  n=0;
}

best regards

Sorry, I am not familiar with that Timer1 library.

You say you want to take readings at 10 µsec intervals - but analogRead() takes 104µsecs to complete.

It would be a good idea if you describe the project you are trying to implement. At the moment this has all the hallmarks of an XY Problem

...R