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!