1

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
    Replace C++ ´string´ with C style strings which are arrays of char - char[] and use the corresponding methods for string manipulation. – Kwasmich Jul 11 '19 at 8:41
  • 1
  • 1
  • 1
    There are even libraries for debouncing, like the Bounce 2 library – chrisl Jul 11 '19 at 9:00
  • 1
    Could you post all the code, or at least some more? As the code doesn't even do a digitalRead, to read the buttons state. Secondly, the second if statement will never evaluate to true, so can be entirely removed. – Gerben Jul 11 '19 at 9:08
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 Jul 11 '19 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 Jul 11 '19 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 Jul 11 '19 at 10:32
  • 1
    @newbie I changed my answer to accommodate for the zero assignment statement (see chrisl's comment too above). – Michel Keijzers Jul 11 '19 at 13:06
  • 1
    @newbie I will. – Michel Keijzers Jul 16 '19 at 19:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.