The trouble with assumptions

Yes, I see. I guess that’s why you earn the big bucks. :wink:

And while I have fearless and successfully used interrupts, my first and second and third approach is to code around any perceived need to do. Therefore quite rarely.

So good thing this isn’t my problem. This is a case where I would discover the problem because my solution didn’t work, whereas a few minutes of competent thinking it through would reveal the flaw.

I am confused by the idea of what “global environment” is in effect during the interrupt service period. The ISR has access to any global variables, so would the same statements in a function with the same lines. Other than that, what could be different?

In fact trying to think about it raises a question for the OP and anyone who’s a bit further along with this - why the objection to suppressing interrupts when you don’t want them to happen? Is that undesirable unfashionable or illegitimate?

a7

You could go with a counter instead of a flag:

volatile uint8_t counter = 0;

void ISR() {
  counter++;
}

void loop() {
  if ( someSensorFunction() == true ) {
    counter++;
  }
  ...
  if ( counter > 0 ) {
    counter--;
    magicBus();
  }
  ...
counter++; // call it yourself or
  ...
  if (whatever)
    counter++;
}

void magicBus() {
  // whatever code needs to execute
}

Regarding what problem the OP is trying to solve or what he wants to do …. I tried reading through the posts several times. Quite frankly I found it too painful to attempt to read and parse / decode the description. And, since I’m not getting paid to do so ….

It’s so much easier to help someone if they provide a well-written problem description.

Which is worse. The failure window is larger.

Describe the scenario that you think will cause a problem with the code I posted.

Probably better with a while() loop:

volatile uint8_t counter = 0;

void ISR() {
  counter++;
}

void loop() {
  if ( someSensorFunction() == true ) {
    counter++;
  }

  if (whatever) {
    counter++;
  }

  counter++; // call it yourself or

  while ( counter > 0 ) {
    counter--;
    magicBus();
  }

}

void magicBus() {
  // whatever code needs to execute
}

Search for the copious posts in which I do that.

Not worth my time.

Then stop posting terrible advice.

Show me what’s wrong with the code in Post # 11. It will call magicBus() exactly the number of times requested regardless of where the requests come from … the ISR or the if() statements.

The is no “failure window” save for overflowing the uint8_t counter variable. if the interrupts come faster than the while() loop can iterate.

See reply #12.

Hardly a cogent argument. If you’re so sure you’re right, provide a link.

Not necessary.

You make a foolish assumption about uint8_t.

What did you assume?

@Koepel grabs a piece of cheese and a cup of coffee and watching this from a distance.

@alto777 swears off interrupts 4ever

and wonders how Post #10 can direct our attention to Post #12

a7

I split off the garbage.

I am saddened that I have to explain that to you.

I’m saddened to have read this much only to find that a moderator got rid of crucial posts.

1 Like

@gfvalvo He’s entirely unhelpful, but he’s right. You can access an 8bit integer atomically but adding numbers requires multiple instructions, which at any time can be interrupted. Potentially causing data corruption.

If you want to increment the counter variable you should disable interrupts or wrap it in an atomic block.

If you had taken then time to flag the “crucial” posts instead of whinging they would already be restored.

Taken the time to flag WHAT? No posts to flag when I started reading, just posts referring to what ain’t there. Then I see a clue given except the code it references was removed by the mod before I saw the thread. Whinge, no, it’s WTF was that?

You make a foolish assumption about uint8_t.

What did you assume?

I assumed that the error code would stay in the thread. The trouble with that is it didn’t.

1 Like

Since this was moved to Bar Sport, I tried to add a link in a sneaky way.
afbeelding
Should that be possible ? Someone could make a link without anyone noticing it.

[ADDED] Dicussion about this contiues here: Do you like the new forum? - #1191 by Koepel