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
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?
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.