Hi Guys i am trying to change "int duty" in my loop.
Can you please tell me where i am going wrong?
At the moment the variable just stays at the initial value set in global.
#include "TimerOne.h"
unsigned long time;
unsigned long Cutout = 10000;
unsigned long Cutout2 = 20000;
int duty = 614;
void setup()
{
pinMode(9, OUTPUT);
Timer1.initialize(1000000); // initialize timer1, and set a 1 second period
Timer1.pwm(9,duty); // setup pwm on pin 9, 0-1024 max resolution /100*% to get a % duty eg; 614.4 is 60%
}
void loop()
{
time = millis();
if (time >= Cutout)
duty = 100;
//Timer1.disablePwm(9);
}
Changing the variable in loop() will have no effect on your usage in setup(). The call to Timer1.pwm() only happens once. If you want to update it with a new value of duty, you need to make a call to that function again.
It would appear that your time never goes past your cutout time although as per previous post, how do you know it never gets reset? you never use it again
Classic case for using Serial to debug, with an "I'm here" print from inside an if, and a "valX=...." at any time you would expect a variable to change.
Well, It works fine for me. It takes about 10 seconds, which I expect is what it is supposed to do. I added Serial.begin and Serial.print so I could check on the status of time and duty.
#include "TimerOne.h"
unsigned long time;
unsigned long Cutout = 10000;
unsigned long Cutout2 = 20000;
int duty = 614;
void setup()
{
pinMode(9, OUTPUT);
Timer1.initialize(1000000); // initialize timer1, and set a 1 second period
Timer1.pwm(9,duty); // setup pwm on pin 9, 0-1024 max resolution /100*% to get a % duty eg; 614.4 is 60%
Serial.begin(9600);
}
void loop()
{
time = millis();
if (time >= Cutout)
duty = 100;
Serial.print("time = ");
Serial.print(time);
Serial.print(" duty = ");
Serial.println(duty);
//Timer1.disablePwm(9);
}
part of the serial monitor printout:
time = 9830 duty = 614
time = 9856 duty = 614
time = 9880 duty = 614
time = 9905 duty = 614
time = 9930 duty = 614
time = 9955 duty = 614
time = 9980 duty = 614
time = 10005 duty = 100
time = 10031 duty = 100
time = 10057 duty = 100
time = 10083 duty = 100
time = 10108 duty = 100
But now i cannot get it to stop......(Bangs Head on wall)
#include "TimerOne.h"
unsigned long time;
unsigned long Cutout = 10000;
unsigned long Cutout2 = 20000;
int duty = 614;
void setup()
{
pinMode(9, OUTPUT);
Timer1.initialize(1000000); // initialize timer1, and set a 1 second period
Serial.begin(9600);
}
void loop()
{
time = millis();
Timer1.pwm(9,duty); // setup pwm on pin 9, 0-1024 max resolution /100*% to get a % duty eg; 614.4 is 60%
if (time >= Cutout)
duty = 100;
Serial.print("time = ");
Serial.print(time);
Serial.print(" duty = ");
Serial.println(duty);
if (time >= Cutout2)
Timer1.stop();
}