Breaking a while loop

I am writing a routeen to send nzri data out one of the ports using timer 1 as a time base and written a delay process as the standard delay re configures timer 1;

if I have a while loop waiting for a counter set by the timer interrupt it never ends however it I toggle a pin in the while loop it works as expected.

int timer;

void setup(){
//set timer1 interrupt at 9600Hz
TCCR1A = 0;// set entire TCCR2A register to 0
TCCR1B = 0;// same for TCCR2B
TCNT1 = 0;//initialize counter value to 0
// set compare match register for 8khz increments
OCR1A = 207;// = (1610^6) / (96008) - 1 (must be <256)
// turn on CTC mode
TCCR1A |= (1 << WGM21);
// Set CS21 bit for 8 prescaler
TCCR1B |= (1 << CS21);
// enable timer compare interrupt
TIMSK1 |= (1 << OCIE2A);;
sei(); //enable interrupts
pinMode(10, OUTPUT);
pinMode(9, OUTPUT);
}

void delay_time(int Timer) {
int Time=Timer*3913;
timer=0;
while(Time>timer){
digitalWrite(9,digitalRead(9) ^ 1); // if this line is commented out the while never exits
}
digitalWrite(10,digitalRead(10) ^ 1);
}
ISR(TIMER1_COMPA_vect){//timer1 interrupt 9600hz toggles pin 9
timer++;
//other thing to happen at a regular basis
}

void loop(){
delay_time(3);
}

as it freezes/blocks your code from doing other things
As general "making small talk to get points, OK.

Not applicable in this case - the only "thing" the code does is to monitor temperature and controls the fan. KISS

Hera are some different ways to accomplish the task. Suggesting to add another while loop to keep the motor off for selected time and still keep checking the temp.

IN PSEUDOCODE not to be cut and paste !

int sensor = A0; //temp sensor on pin A0
int temp = 0; //do not change
int relay = 5; //relay is on pin 5

void setup() {
pinMode(sensor, INPUT); //set sensor as input
pinMode(relay, OUTPUT); //set relay as output
}

void loop()
{
// temp = analogRead(sensor); //read the value from the sensor
if (analogRead(sensor) >= 80) // If temp is 80 or above
{
temperature while loop
while (analogRead(sensor) > 70) { // and Until temp is below 70
digitalWrite(relay, HIGH ); // turn relay on

}
turn the relay off

get start time to keep relay off

relay off wait loop
while (current time - start time < motor off time & (analogRead(sensor) < ? temp ? ))
; keep motor off and keep checking temp

}

digitalWrite(relay, LOW); //Turn relay off and restart loop
}

Can't do code tags when copying code.

Did you wonder why your code looks like this?

OCR1A = 207;// = (1610^6) / (96008) - 1 (must be <256)

It's because you didn't put code tags around your code:

[code]
   OCR1A = 207;// = (16*10^6) / (9600*8) - 1 (must be <256)
[/code]

... so it looks like this when you click Preview:

   OCR1A = 207;// = (16*10^6) / (9600*8) - 1 (must be <256)

This is explained in How To Use the Forum.

And I hope your code is indented properly. Without code tags, all the leading spaces are removed. Type control-T in the IDE editor to format it before posting.

To answer your question, you must use the volatile attribute on variables that are shared between interrupt routines and non-interrupt code (e.g., loop or anything called from loop).

volatile int timer;

Read more about it here, here and here.

pggood:
if I have a while loop waiting for a counter set by the timer interrupt it never ends however it I toggle a pin in the while loop it works as expected.

If you have a WHILE loop waiting to be interrupted then you should not have a WHILE loop in the first place.

Use IF and allow loop() to take care of the iteration.

In general don't use WHILE or FOR loops unless they do something that completes in a few microsecs.

...R

Duplicate question, already answered over here.

How many answers do you want?

232:
Can't do code tags when copying code.

What?

  1. Copy the code (control-C) from somewhere.
  2. Paste the code (control-V) into the post editor.
  3. Select the pasted code (mouse click-drag-release OR control-A).
  4. Click "</>" button in upper left (adds code tags around selected text).
  5. Enter extra descriptive text before or after code tags.
  6. Click "Post" or "Preview" button in lower left (interprets tags to show code in white box).

If you're mouse-averse, you can just type left bracket, c, o, d, e, and right bracket before the code. Then arrow down to the end of the code and type left bracket, slash, c, o, d, e and right bracket after the code.

Example:

```
**IN PSEUDOCODE  not to be cut and paste !

[nobbc][code]int sensor = A0; //temp sensor on pin A0
int temp = 0; //do not change
int relay = 5;  //relay is on pin 5

void setup() {
  pinMode(sensor, INPUT);  //set sensor as input
  pinMode(relay, OUTPUT);  //set relay as output
}
  etc.
[/code][/nobbc]

...more commentary...**
```

@pggood, do not hijack. Thread split.

@pggood, do not cross-post. Threads merged.

-dev:
Duplicate question, already answered over here.

How many answers do you want?
What?

  1. Copy the code (control-C) from somewhere.
  2. Paste the code (control-V) into the post editor.
  3. Select the pasted code (mouse click-drag-release OR control-A).
  4. Click "</>" button in upper left (adds code tags around selected text).
  5. Enter extra descriptive text before or after code tags.
  6. Click "Post" or "Preview" button in lower left (interprets tags to show code in white box).

If you're mouse-averse, you can just type left bracket, c, o, d, e, and right bracket before the code. Then arrow down to the end of the code and type left bracket, slash, c, o, d, e and right bracket after the code.

Nope,

Example:

```
**IN PSEUDOCODE  not to be cut and paste !

[nobbc][code]int sensor = A0; //temp sensor on pin A0
int temp = 0; //do not change
int relay = 5;  //relay is on pin 5

void setup() {
  pinMode(sensor, INPUT);  //set sensor as input
  pinMode(relay, OUTPUT);  //set relay as output
}
  etc.
[/code][/nobbc]

...more commentary...**

```

NOPE,
KISS
properly posted code in code tags has an option to "Select" and then "copy' via mouse .
Next using "paste" via mouse into Quick reply form LOSSES the code tags.

Where have you been in past x years? Suggesting to use "control C / Control V " in graphic environment is archaic.

Here - with three (3!) mouse clicks - no code tags

volatile int timer;

And the answer is - it is my , Arduino customer , fault !

Thanks.

232:
Where have you been in past x years? Suggesting to use "control C / Control V " in graphic environment is archaic.

With experience you learn that not all new things are beneficial.

...R

Where have you been in past x years? Suggesting to use mouse clicks in a touch screen environment is archaic.

:grin: