Due timer.h (simple timer example )

Hello everyone,
I am want to generate a pwm signal varying frequency using potentiometer through arduino due. I am using simple timer example from due timer.h. It works perfectly fine when these following two lines are in the void setup.
Timer3.attachInterrupt(myHandler);
Timer3.start(5000);
but when I put these two lines in loop it doesn't work. kindly help
note: I verified the output using oscilloscope.

#include <DueTimer.h>

int myLed = 13;

bool ledOn = false;
void myHandler(){
	ledOn = !ledOn;

	digitalWrite(myLed, ledOn); // Led on, off, on, off...
}

void setup(){
	pinMode(myLed, OUTPUT);

	Timer3.attachInterrupt(myHandler);
	Timer3.start(50000); // Calls every 50ms
}

void loop(){

	while(1){
		// I'm stuck in here! help me...
	}
	
}

In this code the lines in question are not in the loop. if you have problems with other code, why are you showing this one?
Show the code that does not work

#include <DueTimer.h>

int myLed = 13;

bool ledOn = false;
void myHandler(){
	ledOn = !ledOn;

	digitalWrite(myLed, ledOn); // Led on, off, on, off...
}

void setup(){
	pinMode(myLed, OUTPUT);

	
}

void loop(){

Timer3.attachInterrupt(myHandler);
  Timer3.start(5000); // Calls every 50ms
	
}

this is the code which is not working

The loop cycle runs much more often than your timer duration, the interrupt just doesn't have time to fire.
You should only setup the timer again when you receive a new potentiometer value. By the way, where is it, I don't see it in the code? :slight_smile:
If you don't have a timer duration change, don't put this lines in the loop

#include <DueTimer.h>

int myLed = 13;
int mypot=A0;
int pot_value=0;
int pot_map=0;

bool ledOn = false;
void myHandler(){
	ledOn = !ledOn;

	digitalWrite(myLed, ledOn); // Led on, off, on, off...
}

void setup(){
  
	pinMode(myLed, OUTPUT);
  pinMode(mypot,INPUT);

	
}

void loop(){
  
  pot_value=analogRead(mypot);
 pot_map=map(pot_value,0,1023,0,5000);
  
  Timer3.attachInterrupt(myHandler);
  Timer3.start(pot_map); // Calls every 50ms
	
}

this is the code I am using 

This code has the same problem as the previous one - you are restarting the timer more often than the interrupt fires.

Change the timer setting ONLY if the new pot value differs from actual used in timer.
So you need to save actual value to variable and then compare it with new one.

1 Like

thank you for your feedback. But I am not getting what you are saying being a beginner.

void loop(){
  static int actual_duration =0;
  pot_value=analogRead(mypot);
  pot_map=map(pot_value,0,1023,0,5000);
  if (pot_map != actual_duration) {
     Timer3.attachInterrupt(myHandler);
     Timer3.start(pot_map); // Calls every 50ms
     actual_duration = pot_map;
   }
}

You don't need to attach the handler EVERY TIME that you change the period. Just do that once in setup().

1 Like

I agree, I just change the code as little as possible to make it clearer to OP. But you're right, it's more correct

this works fine. TYSM

Hi again my code works fine but I want to stop the timer when a switch is not pressed. I tried using the keyword Timer3.stop();. But it doesn't work

#include <DueTimer.h>

int myLed = 13;
int mypot=A0;
int pot_value=0;
int pot_map=0;

bool ledOn = false;
void myHandler(){
  ledOn = !ledOn;

  digitalWrite(myLed, ledOn); // Led on, off, on, off...
}

void setup(){
  
  pinMode(myLed, OUTPUT);
  pinMode(mypot,INPUT);

  
}

void loop(){
  if(digitalRead(22)==HIGH){
 static int actual_duration =0;
  pot_value=analogRead(mypot);
  pot_map=map(pot_value,0,1023,0,5000);
  if (pot_map != actual_duration) {
     Timer3.attachInterrupt(myHandler);
     Timer3.start(pot_map); // Calls every 50ms
     actual_duration = pot_map;
   }
  }
  else{
      Timer3.stop();
    }
}

It sounds strange. Explain what's mean "does not work"?
What is your switch is? a button? How it is connected?

It is a dpdt switch.

do you use pullup or pulldown for it?

sorry for late response.
I am using dpdt switch which has three pins
vcc to 3.3v
gnd to gnd
third pin- pin 22 of due

timer3 is working fine but it dosen't stop when I press the switch. I passedTimer3.stop(); when switch is pressed

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.