Nick Gammon, can you help me please - incorrect incremented variable in ISR

Hello

I think had a similar problem in the past and u helped me, no other could. The problem in the past was the following:
I configured interrupt on INT0 for an atmel328p with a pushbutton. In the ISR i incremented a variable for each time i press the button, each press also wakes up the mcu. The variable has initial value of 0. So if i press the button for the first time it should count 1. In the loop section i then check in multiple IF´s if variable has value of 1 or 2 or 3.

Whe i pressed the button the first time, it should count 1 and go in the first IF that checks the variable for 1, but it started always in the second IF that checked for 2. U then gave me the tip that i should put

EIFR = bit (INTF0); // clear flag for interrupt 0

before i go to sleep and that solved my problem.

Now i have a similar problem, but with an attiny85. It always starts in the second IF, then goes to the third and then start again with the second. I think that the equivalent of

EIFR = bit (INTF0);

for the attiny is

//GIFR |= (1 << INTF0); // clear int0 flag

( or am i wrong ? ) I put this everywhere in the Code but without luck.

I thought maybe it is the button bouncing, so i put 6x 0.1µF in parallel ( had no other caps ) but it didnt solve the problem. And after some time i thought this couldnt be a bouncing button. Because it ALWAYS starts in the second IF, there is no randomness of a bouncing button, also because of the observations below.

Observations:

  • If i give variable “a” a start value of 2. Then it ALWAYS starts correctly in the third if. But if “a” has a start value of 0, then it ALWAYS starts in the second IF.

  • if i put a delay of atleast 115ms before i go to sleep then all works correctly. If the delay is below that, it doesnt work.

Can u or others please help me, because im out of ideas. I appreciate it.

Here is my code for the attiny85:

volatile boolean sleep = 1;
volatile byte a = 0;


#define alarm_on() (PORTB |= (1<<PORTB0)) //PB0 High
#define alarm_off() (PORTB &= ~(1<<PORTB0)) //PB0 Low

#define adc_disable() (ADCSRA &= ~(1<<ADEN)) // disable ADC (before power-off)
#define adc_enable()  (ADCSRA |= (1<<ADEN)) // re-enable ADC

void setup() {

  DDRB = B11101011; //PB2|Pin7 Alarm input, PB0|Pin5 Alarm activate output, PB4|Pin3 Battery read analog input.
  //         ^^^^^
  //         32765 //Physical pins

  PORTB = B00000100; //All pins low, pin 7 pullup enabled



  delay(1000);
  for (byte c = 3; c > 0; c--) {

    alarm_on();
    delay(20);
    alarm_off();
    delay(500);
  }


}

void loop() {


  if (sleep) {

    //delay(120); //test

    adc_disable();

    //GIFR |= (1 << INTF0); // clear int0 flag
   
    cli(); //disable global interrupt
    GIMSK |= (1 << INT0); //enable int0 interrupt
    MCUCR = (1 << SM1) | (1 << SE); //sleep enable and power down mode
    sei(); //enable global interrupt
    asm volatile ("sleep"); //sleep
    adc_enable();
  }


  if (a == 1) {

    alarm_on();
    delay(20);
    alarm_off();

    sleep = 1;
  }

  if (a == 2) {

    alarm_on();
    delay(20);
    alarm_off();
    delay(500);
    alarm_on();
    delay(20);
    alarm_off();

    sleep = 1;
  }

  if (a == 3) {

    alarm_on();
    delay(20);
    alarm_off();
    delay(500);
    alarm_on();
    delay(20);
    alarm_off();
    delay(500);
    alarm_on();
    delay(20);
    alarm_off();

    a = 0;
    sleep = 1;
  }


}

ISR(INT0_vect) {

  GIMSK &= ~(1 << INT0); //disable int0 interrupt
  MCUCR &= ~(1 << SE); //disable sleep
  sleep = 0;
  a++;
}

Crosspost. See http://forum.arduino.cc/index.php?topic=358558.0

As you ignore the advice given in the German section and just repost the original question,
this is at least annoying me, so good luck.

This thread is addressed to me personally. It is followed up by a PM to me, despite that I ask in my signature that people do not PM me for technical advice.

This forum is greater, much greater, than one person. I have learned a great deal from my fellow posters, and acknowledge that information I post is often just echoing what they have taught me.

Just want to address a greater range of people, i think there is nothing wrong with it.

It’s called cross-posting.

Please do not cross-post. This wastes time and resources as people attempt to answer your question on multiple threads.

If you PM me again I will take stronger action.

in a thread addressed puplicly to you and others

I must've missed the other addressees, or maybe you're writing about a different thread.

sp. "Publicly"

Ardunaut:
Just want to address a greater range of people, i think there is nothing wrong with it.

You think wrong. It is bad practice to cross-post. What makes you so special that your problem should get more attention than those of others?

And asking specifically for the help of a particular member is the equivalent of directly PMing him asking for help - another no-no.

Good luck with it.

Ardunaut:
Tell me, what should i do if no one can help in a particular part of the forum.

Well I'm sure that your aggressive attitude on this thread hasn't won you any friends around here. Probably the best thing to do would be to either drop the attitude and get on with your question or to just leave and go work on your project alone. Bitching at us isn't going to get anyone to help you.

Ardunaut:
And im not special. Instead to criticise me for what i did wrong, TELL ME HOW i can do it BETTER. But neither YOU nor Nick did. It is easy to criticise one, but if you do, tell how to improve !

Don't cross-post. Full stop!

Ardunaut:
Im not bitching by any means. Just telling how it is, so if you get offended by THIS, then i dont know dude. So you are another one just telling me what is "wrong" without any ideas on how to improve. Ok

When you are asking people to volunteer their time for you it is best not to be aggressive with them. A better solution would have been to say, "Sorry about that. Didn't mean to break the rules. Now here is my question about Arduino." instead of all the ranting about how you expect to be treated and how we should talk to you. Remember that we aren't being paid by anyone and certainly have the right to just ignore you, especially when you talk to us this way.

Go sit down for a minute and take a few deep breaths. Get a little bit of humility about you. And then come ask your Arduino question.

Ardunaut:
Wow, so easy ? Your post is pointless

Ok then how much time has to past, so that a second thread can not longer be seen as cross post ?

Why don't you just ask that it be moved if it isn't getting responses where you put it?

Ardunaut:
Then you have to read more carefully... before you act smart. But since you dont care, i dont expect you to read anything. But good try.

Edit: And for the case you missed it, it is a puplic forum and i posted it on a puplic forum, so it must be addressed to everyone.

Sp. "Public".
Really, you must read more carefully

Ardunaut:
I never said anything on how anyone else should talk to me ......... i demand honesty

Nobody was dishonest with you. Your response to Nick was quite aggressive. You could have just taken your lumps there and moved on and solved the problem with your program. Yet here you are, still bitching about the "smart guys" writing responses that you don't like. Get over it already before you end up getting banned. What purpose does any of this serve? I don't even know what your question was at this point, just that you seem really pissed off that someone asked you to follow the rules and now you seem to have a really nasty attitude about it.

I tried to get you to calm down and get to your question. You don't seem interested in doing that so I'm done with this discussion. Don't waste your time to reply to this because I won't be reading anything else with your name on it at any point in the future. So there's one source of help you've completely thrown away. And being that I am well versed in the registers and interrupts on the ATTiny85, that was probably a mistake. Maybe your problem never gets solved. Who cares at this point? You obviously don't.

@Ardunaut, no one is interested in reading any more of your crap. All you've managed to do with your extremely poor attitude is alienate everyone that might have been able to help you.
Like Delta_G, I'm unsubscribing from this thread, and will make a point of never reading your posts in the future after this little temper tantrum. Grow up.