[SOLVED] Help with interrupts and classes

Hello everyone! Let’s see if you can help me!

For my engineering thesis, I’m building a device using Arduino Leonardo. It’s made with 3 identical sensors, 1 led, 1 buzzer, and 1 button.
My tutor said that, in order to be as tidy as possible, I should follow these rules:
-Only use one sketch.
-Only functions declared inside that sketch are Setup() and Loop().
-Use as less global variables as possible.
-Only use one library in that sketch.

So I managed to build libraries for the sensor, led and buzzer, and integrate them in one library called DARM (acronim for the device). The problem is that I haven’t been able to make the button work with previous rules.

This is button’s interrupt routine. As you see, it uses 4 variables: TimeCounter, ButtonTimeThreshold, cont, and botonApretado (means “button pushed” in spanish). The last one is the one that interacts with the rest of the code tu communicate that the button has been pushed and we should take an action.

void irqBoton_Debounce()
{
 //Introduce un umbral de tiempo en el que inhabilita el acceso a la interrupción 
 if(millis()>TimeCounter+ButtonTimeThreshold)
{
    botonApretado=true;
    TimeCounter=millis();  
    cont++;
    Serial.print(cont);
    Serial.print(",");
    Serial.println(TimeCounter);
} 
}

In the rest of the functions I have many pieces of code with the following format:

t0=millis();
while(millis()<t0+6000)//while 1 minite hasn't passed
{
    if(botonApretado==true)
    {
         botonApretado=false;// I reset the flag
         doSomething();//take an action
         break;    
    }
}

So, these are the problems:

  • Everything would work fine if I declare irqBoton_Debounce() and the 4 variables in the main sketch, but I would be breaking rule number 2.

  • If I want to put irqBoton_Debounce() inside the class, I need to declare it “static”, and do the same with the variables. If variables are declared “static”, the can only be accessed by one function, so I wouldn’t be able to call command like “if(botonApretado==true)” or “botonApretado=false”.

  • If I put irqBoton_Debounce() and the variables in the .h file but outside the class it doesn’t compile.

We’ll. That’s it. Thanks for reading. I’d love to hear your reccomendations.

Thank you very much, and sorry for the english mistakes!

Brian_yn:
-Only functions declared inside that sketch are Setup() and Loop().
-Only use one library in that sketch.

Those are bogus rules…

Yes, grouping things can make stuff clear. And with grouping it might be a start to only have a sketch with short setup() and loop(). But rather have multiple functions definitions in the sketch rather than a length loop()… Also, all stuff in a library should be related to the same (small-ish!) task. Multiple tasks => multiple libraries. Otherwise it’s like cleaning up by dumping everything in a different room. Doesn’t make finding stuff easier…

And at first you talk libraries and next you talk classes. They are NOT the same :wink:

And human pressed buttons don’t need interrupts. Heck, they are more trouble then they are worth! Interrupts are for bloody fast stuff!

if(millis()>TimeCounter+ButtonTimeThreshold)

is the wrong way of using millis() because it will fail on roll over.

if(millis()- TimeCounter > ButtonTimeThreshold)

does not (assuming TimeCounter holds the time you started whatever you want to time.

And if you try to call an object method as interrupt call back, see this.

As for more, we’re not Snippets R Us. Aka, post all the code or better, post a MCVE.

Septillion, thank you very much for your help! I'll try to answer everything:

Yes, grouping things can make stuff clear. And with grouping it might be a start to only have a sketch with short setup() and loop(). But rather have multiple functions definitions in the sketch rather than a length loop().... Also, all stuff in a library should be related to the same (small-ish!) task. Multiple tasks => multiple libraries. Otherwise it's like cleaning up by dumping everything in a different room. Doesn't make finding stuff easier...

I think I'm failing to apply the rules correctly but, except that error, they made me have short setup() and loop(). Same for functions in the library, they are smaller than they were and clearer, as they only have one function.

And at first you talk libraries and next you talk classes. They are NOT the same :wink:

You're right. I mistaked because I used one class per library.

And human pressed buttons don't need interrupts. Heck, they are more trouble then they are worth! Interrupts are for bloody fast stuff!

Do you mean I should just use analogRead(buttonPin)? I read I needed to use interruptions in order to deal with button's bouncing.

is the wrong way of using millis() because it will fail on roll over.

Nice tip, I'll use it. thanks!

As for more, we're not Snippets R Us. Aka, post all the code or better, post a MCVE.

I'm sorried, I didn't post it because there are 6 libraries on it and thaugt it was to much.

And if you try to call an object method as interrupt call back, see this.

Sorry, don't know how to use the info in that post to fix my code

Brian_yn:
You're right. I mistaked because I used one class per library.

And thus you use multiple libraries (and thus multiple classes).

Brian_yn:
Do you mean I should just use analogRead(buttonPin)?

No, digitalRead() :wink:

Brian_yn:
I read I needed to use interruptions in order to deal with button's bouncing.

No, interrupts are not a fix for bouncing. Heck, they even make it worse because an interrupt can sport every tiny bounce... Just poll it with digitalRead() and handle the bounce in code. Or let a library help you. Bounce2 does an excellent job :slight_smile: And don't say you may not use external libraries, because as you're using Arduino you're already use a lot of libraries, where do you think digitalRead() or attachIntterupt() is coming from?

Brian_yn:
I'm sorried, I didn't post it because there are 6 libraries on it and thaugt it was to much.

That probably is to much because of the time we need to spend. That's where a MCVE comes in. And as you split everything into libraries it should not be to hard to just use the bits (/libraries) that matter for the problem. Easy, isn't it? :wink:

Brian_yn:
Sorry, don't know how to use the info in that post to fix my code

Without all code (but than time is an issue so =>) or a MCVE we don't know either...

Thank you very much, I made it compile using Bounce2 lib.

Good to hear :slight_smile: