I have a VERY long code:
Sketch uses 30468 bytes (92%) of program storage space. Maximum is 32768 bytes.
I've been trying to reduce it's size so i can write more code, one of my main part of code which repeated many times in switch cases or in if statements is related to de-bouncing the buttons.
Here's a piece of my code as an example:
case 12:
if ((millis() - millistimer) > BounceD) {
if (receivedValue == RFb) {
magnetDistance += 5;
millistimer = millis();
receivedValue = 0;
}
}
if ((millis() - millistimer) > BounceD) {
if (receivedValue == RFd) {
magnetDistance -= 5;
millistimer = millis();
receivedValue = 0;
}
}
break;
The de-bouncing actions happens in each and every case and for each and every button.
There's two buttons which in each case
increase/decrease different values.
How to avoid repeating the de-bouncing action for every button?
EDIT:
The recievedValue
is updated via RC buttons value once any button is pressed on the remote.
1 Answer 1
In the short example of above, first find out what is a duplicate (the two outer if
statements including their body), than the differences:
- Rfb versus Rfd
- +5 versus -5
Than make a function for the duplicated code, and turn all differences into a parameter (I don't know the type of Rfd, so I take an int for it).
You will get:
case 12:
processButton(&Rfb, 5); // Offset magnet distance by +5
processButton(&RFd, -5); // Offset magnet distance by -5
break;
...
void processButton(int* rf, int diff)
{
if ((millis() - millistimer) > BounceD) {
if (receivedValue == *rf) {
magnetDistance += diff;
millistimer = millis();
*rf = 0;
}
}
}
Note that if magnetDistance
and millistimer
are local variables you also have to pass them (as pointer to change the value), if they are globals you can keep the code as is above.
UPDATE
The way parameter works is as follows. E.g.
processButton(Rfb, 5);
Will call the following function:
void processButton(int rf, int diff)
Where value rf will be Rfb, and diff will be 5 (+5).
Thus when magnetDistance += diff;
is executed, diff will be +5 and magnetDistance will be increased with +5. In the next call, magnetDistance will be -5 and thus will be subtracted.
UPDATE 2
To change a variable, you have to pass it as pointer, otherwise the (copied) parameter value will be changed and not return its value to the called function. So you have to give the parameter as an address (& operator), and use -> instead of . to get to the value.
-
can you please comment that code? does it just increase the value of magnetDistance or decrease it too? because RFb and RFd are two different buttons.ElectronSurf– ElectronSurf2019年07月11日 09:47:40 +00:00Commented Jul 11, 2019 at 9:47
-
Instead of commenting code (which is more or less trivial), I added an explanation how parameters work, I guess that is the information what you miss. It increases or decreases the value, depending on the value passed to the function (first time +5, second time -5).Michel Keijzers– Michel Keijzers2019年07月11日 10:11:33 +00:00Commented Jul 11, 2019 at 10:11
-
thanks but what if we press RFb three times? it will just do +5 and -5 three times instead of just do +5 three times.ElectronSurf– ElectronSurf2019年07月11日 10:32:31 +00:00Commented Jul 11, 2019 at 10:32
-
1@newbie I changed my answer to accommodate for the zero assignment statement (see chrisl's comment too above).Michel Keijzers– Michel Keijzers2019年07月11日 13:06:05 +00:00Commented Jul 11, 2019 at 13:06
-
1@newbie I will.Michel Keijzers– Michel Keijzers2019年07月16日 19:04:31 +00:00Commented Jul 16, 2019 at 19:04
char[]
and use the corresponding methods for string manipulation.