for loop in a else if ?

can i add a loop into a else if statement ?
it has to send the value for a few full void loops before i change variable newPosition = oldPosition.

else if(newPosition > oldPosition){
     //Read switch 30
     tmpButtonState = BUTTON30;

can i add a loop into a else if statement ?

Yes.

it has to send the value for a few full void loops

Full void's, huh? What are you talking about?

There is no such thing as a void loop. There is a loop() function.
Yes, you can nest a for loop inside an if statement.

How should that look like ? Because when i tried the whole code stopped working.
Like it wasn't exiting the loop.

But i have to say i am still really bad at understanding everything.

What whole code?

Welcome to the Forum. Please read the two posts at the top of this Forum by Nick Gammon on guidelines for posting here, especially the use of code tags ("</>") when posting source code files. Also, before posting the code, use Ctrl-T in the IDE to reformat the code in a standard format, which makes it easier for us to read.

You don't need a for() to "read switch 30". Just call digitalRead on the input pin.

Because when i tried the whole code stopped working.
Like it wasn't exiting the loop.

The onus is on you to show us what you tried.

We have no idea what the for loop that you are looking for is supposed to do.

florinc:
You don't need a for() to "read switch 30". Just call digitalRead on the input pin.

... and make sure you have a Mega or such... :slight_smile:

hmm i can't post all of the code because of some 9000 chars limit ?

Lorien:
hmm i can't post all of the code because of some 9000 chars limit ?

Add it as an attachment.
Fastening my seat belt.

Lorien:
hmm i can't post all of the code because of some 9000 chars limit ?

You can use the Reply button, and the Additional Options link to attach your code.

Ok i attached it now. :slight_smile:

all the normal switches are working, now i did try to incorporate the rotary encoders into my code.
And this is causing me some problems.

When i turn the encoder it registers as button30 as it should, but only 4 times if i reset the Position value down the line.

So from my understanding this gets filtered by my debouncer.

To repair this i wanted to place a for loop inside that else if for button30 and reset position after loop ends.

GamepadV2.ino (11.4 KB)

You never set oldPosition to newPosition. Never set it to anything but the -999 that you gave it when you declared it.

You should only debounce the real (physical) buttons.

You see how contraproductive it is to debounce the encoder. :wink:

Many variables could be placed in arrays.
The giant if then else construct could be replaced by a map(..) and a table lookup.

The giant switch case statement could be written on one line.

The whole sketch is about 300 lines too long.

Sorry it is my first code ever, it also took quite long to understand those basic stuff, which i am still not getting all.
Is it possible somehow in ide to run step by step debug ? guess that would help me understand it better ?

The oldPosition to newPosition is currently not in because i was working on that loop.

But as Whandall said it is not good to debounce the encoder for the same time i do the switches, but i have no idea how i could separate them.
So i think that's the way to go then, to get a debounce for the normal switches and one for the encoders.

Is it possible somehow in ide to run step by step debug ?

No, because the stepping is happening on another computer with no debugger.

but i have no idea how i could separate them.

Why not? The encoder may need to be read using interrupts, so you don't miss a pulse. The other switches most certainly do not.

So i think that's the way to go then, to get a debounce for the normal switches and one for the encoders.

None for the encoders is probably more like it.

I used the encoders now without debounce and it works.

but now i needed a for loop for something else and it does not increment the variable.

i just used a small program for testing and test variable does not increment.
what is wrong, i don't get it ?

int test;

void setup() {
  // put your setup code here, to run once:

}

void loop() {
  // put your main code here, to run repeatedly:

  for (int test = 0; test > 10 ; test++)
  {

    Serial.println("testdone");
  }
  Serial.println(test);

}

Try

  for (int test = 0; test < 10 ; test++)