The Infamous "Expected Primary-Expression before" Issue

Hi all,

In the interest of your time, here’s the tldr version:

I don’t want to define all the variables during the setup, so I want to be able to do so on a per need basis during the loop. All is going well until this expression:

if (bool f3 == 0) {

}

Where I get this error: expected primary-expression before ‘bool’

Here’s all of my code:

#define CLKPin 6
#define DTPin 7

#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd (0x27, 16, 2);

bool CLKCurrentState;
bool DTCurrentState;
bool CLKPreviousState;
int genericCounter = 0;
int MM = 0;
int buttonPin = 8;
bool buttonState = 1;
int genericCounterCorrected;








void setup () {
  pinMode (CLKPin, INPUT);
  pinMode (DTPin, INPUT);
  pinMode (buttonPin, INPUT);
  CLKPreviousState = digitalRead (CLKPin);
  lcd.init ();
  lcd.backlight ();

  Serial.begin (9600);
}


void loop () {
  bool f1 = 0;
  while (f1 == 0) {
    bool f2 = 0;
    while (f2 == 0) {
      CLKCurrentState = digitalRead (CLKPin);
      if (CLKCurrentState != CLKPreviousState) {
        if (digitalRead (DTPin) == CLKCurrentState) {
          genericCounter--;
        } else {
          genericCounter++;
        };
      };
      CLKPreviousState = CLKCurrentState;
      if (genericCounter != 0 && genericCounter % 2 == 0) {
        f2 = 1;
      };


      buttonState = digitalRead (buttonPin);
      if (buttonState == 0) {
        bool f3 = 1;
        f1 = 1;
      }
    };

    if (bool f3 == 0) {
      genericCounterCorrected = genericCounter / 2;
      MM = MM + genericCounterCorrected;
      genericCounter = 0;
      if (MM >= 32) {
        MM = 1;
      } else if (MM <= 0) {
        MM = 31;
      };
    }

    bool f3 = 0;


    int onesF = MM % 10;
    int tensF = ((MM - onesF) % 100) / 10;
    int hundredsF = ((MM - onesF - tensF) % 1000) / 100;
    int thousandsF = ((MM - onesF - tensF - hundredsF) % 10000) / 1000;

    lcd.setCursor (0, 0);
    lcd.print (tensF);
    lcd.setCursor (1, 0);
    lcd.print (onesF);
  }















}

is there a way to do so?

"f3" is declared out of scope, you are trying to redeclare it in an if-test which is not allowed. Try to declare f1, f2 and f3 in the begining of the loop like this:

void loop() {
  bool f1 = false, f2 = false, f3 = false;

  //The rest of the loop code here

}

Remember to remove all redeclarations from the rest of the loop code.

ArianKS: if (bool f3 == 0) { ... }

What do you imagine that line should do?

gfvalvo: What do you imagine that line should do?

I just didn't want to keep defining new flags before the set up loop and just wanted to be able to do them "on the go."

So I was expecting that line to read like this:

"if f3, which btw is a bool type variable, is ever 0, follow these conditions..."

ArianKS: "if f3, which btw is a bool type variable, is ever 0, follow these conditions..."

That is not how the piano plays.

Danois90: That is not how the piano plays.

Dang. Got it. Will define the variables before the setup.