May be used uninitialized in this function

I cant seem to find the cause of this error. I've reviewed other posts and I think I've corrected everything that is mentioned... The specific errors are:

> C:\Program Files\WindowsApps\ArduinoLLC.ArduinoIDE_1.8.57.0_x86__mdqgnx93n4wtt\hardware\arduino\avr\cores\arduino\main.cpp: In function 'main':
> C:\Users\don_d\OneDrive\Documents\Arduino\MJUK\RemoteTestv5\RemoteTestv5.ino:106:12: warning: 'prevposSlide' may be used uninitialized in this function [-Wmaybe-uninitialized]
>        else if ( prevposSlide == Right ) { //3g
>             ^
> C:\Users\don_d\OneDrive\Documents\Arduino\MJUK\RemoteTestv5\RemoteTestv5.ino:48:7: note: 'prevposSlide' was declared here
>    int prevposSlide = Left;
>        ^

Note that the quoted declaration initializes the variable in the warning. You can see this in my sketch below. In addition, there are two other variables that I use in a similar way, but so far, I dont get an error with them.

// start
// Remote Control Wiring/Operation Test Sketch v5
// (c) 2022 Don Lutz

#define UpDownPin A0
#define PeekOutPin A1
#define SlideLeftPin A2
#define SlideRightPin A3
#define Forever true

enum Command {Up, Down, PeekOut, Hide, Left, Center, Right };
bool UpDown = true;
bool Peek = true;
bool SlideLeft = true;
bool SlideRight = true;

void setup() {
  Serial.begin(9600);
  pinMode(UpDownPin, INPUT_PULLUP);
  pinMode(PeekOutPin, INPUT_PULLUP);
  pinMode(SlideLeftPin, INPUT_PULLUP);
  pinMode(SlideRightPin, INPUT_PULLUP);
}


bool checkButtons() {
  bool isCommand;

  UpDown = ! bool(digitalRead(UpDownPin));
  Peek = ! bool(digitalRead(PeekOutPin));
  SlideLeft = ! bool(digitalRead(SlideLeftPin));
  SlideRight = ! bool(digitalRead(SlideRightPin));

  isCommand = UpDown || Peek || SlideLeft || SlideRight;
  return isCommand;
}


void loop() {

  bool noCommand = true;

  int prevposUpDown = Down;
  int posUpDown;
  int prevposPeek = Hide;
  int posPeek;
  int prevposSlide = Left;
  int posSlide;

  const int debounceDelay = 500;
  unsigned long now;
  unsigned long then;
  then = millis();

  while (Forever) { //1

    noCommand = true;


    // GET REMOTE CONTROL COMMAND !!!
    while (noCommand) { //2a
      now = millis();

      if ((now - then) > debounceDelay) { //3a
        if (checkButtons()) {//4a
          then = millis();
          noCommand = false;
        }//4a
      }//3a
    }//2a


    // PROCESS BUTTON PRESSED
    if (UpDown) {//2b
      if (prevposUpDown == Down ) {//3b
        Serial.println("[A] Dog pops up !");
        posUpDown = Up;
      }//3b
      else if (prevposUpDown == Up) {//3c
        Serial.println("[A] Dog pops down !!");
        posUpDown = Down;
      }//3c
      prevposUpDown = posUpDown;
      UpDown = false;
    }//2b

    if (Peek) {//2d
      if ( prevposPeek == Hide ) {//3d
        Serial.println("[B] Dog peeks out !!");
        posPeek = PeekOut;
      }//3d
      else if ( prevposPeek == PeekOut ) {//3e
        Serial.println("[B] Dog hides !!");
        posPeek = Hide;
      }//3e
      prevposPeek = posPeek;
      Peek = false;
    }//2d

    if (SlideRight) {//2f
      if ( prevposSlide == Left ) {//3f
        Serial.println("[C] Dog slides to the right !!!");
        posSlide = Right;
      }//3f
      else if ( prevposSlide == Right ) { //3g
        Serial.println("[C] Dog is already to the right !!!");
        posSlide = Right;
      }//3g
      prevposSlide = posSlide;
      SlideRight = false;
    }//2f

    if (SlideLeft) {//2h
      if ( prevposSlide == Right ) {//3h
        Serial.println("[D] Dog slides to the left !!!!");
        posSlide = Left;
      }//3h
      else if ( prevposSlide == Left ) {//3i
        Serial.println("[D] Dog is already to the left !!!!");
        posSlide = Left;
      }//3i
      prevposSlide = posSlide;
      SlideLeft = false;
    }//2h
  }//1
}// loop
// end

the compiler is misleading you a bit by putting the '^' under the if

the issue is at that line:

      prevposSlide = posSlide;

as posSlide does not have a default value. try to define it as

  int posSlide = Left;

and see if you still get the warning

Bingo!

I didn't even consider that because 'posSlide' would be set to a value in the preceeding if blocks.

Uninitialized is the correct value. The logic would be confusing (to me) if I used a valid value from the enum. So I added another value in the enum, called it Nothing and set posSlide to that value. I also modified the other similar blocks and values.

Works perfect! And compiles perfectly!
Thanks for your quick response...
Don

that's the right way to do so.

not in all theoretical cases, which is what the compiler is saying (although in practice yes)

why do you do

  while (Forever) {

you should just let the loop() spin. that's what it's for (and use static or global variables if you need to remember their values across calls to the loop() function)

I’ve been programming for 55 years. I hope I know the best way!

But, one thing I haven’t understood is how loop() works. An earlier version did not have the while(Forever), but variables lost their values between subsequent execution of loop().

I had an idea but not the time to test it. Should I have declared those variables static? You know, the more I think, the more I bet you’ll say yes.

the loop() is just a function like the others when it comes to C++ rules, so scope and lifetime of variables you declare within the function is what the standard says they are : limited to the function.

if you want a variable to remember its value across calls, make it global (if that makes sense, that is you need to use it in multiple places) or declare it as static in the function.

void loop() {
  static int prevposUpDown = Down; // will be remembered
  int posUpDown; // won't be remembered
  ...

}

the main() function that is generated by the IDE will take care of calling the setup() function once and then the loop repetitively for you basically doing:

	setup();   
	for (;;) {
		loop();
	}

see the full code here : ArduinoCore-avr/cores/arduino/main.cpp at master · arduino/ArduinoCore-avr · GitHub (as it does a few other things)

I get it! It slipped by me in your original post because I was paying attention to the original warning.

I’ve used static variables before in functions. The concept that loop() is also a function is probably why I was having trouble understanding.

And I use global variables a lot. Because I understood them. And I always felt that was the wrong solution. Now, I think you’ve taught me enough that in the future, I’ll not have some of these issues again.

great - have fun !