Changing variable in loop

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);
}

How do you know it is staying the same?

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

What is it that makes you think that it is not changing ?

Using the initialise method "breaks analogWrite() for digital pins 9 and 10 on Arduino." according to the documentation for the Timer1 library.

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

Well i can see it changing also via serial; Thanks :slight_smile: I am also learning how to debug..

But the led i have on pin9 does not change???

Where in the code did you tell it to change the LED?

-br

billroy:
Where in the code did you tell it to change the LED?

-br

By changing variable duty in loop does this not affect the setup?

I know i must sound like a real NOOB!! thats because i am..... Sorry guys. :~

I have made some changes. and it is working

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();
  }

I think i have done it.... YAY

Thanks guys.. i really appreciate your help!!

#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)
  shutdown();
  //delay (1000);
}
void shutdown()
{
  // add you emergency shutdown code here
  Timer1.disablePwm(9);
  while(1);  // endless loop
}