Problem with code '...was not declared in this scope'

Hi,

I'm working on a project. I used an old code witch was working and added 4 relays and 4 switches. Unfortunately the code is working anymore. Does someone has a clue why?



#define CLOSED                       LOW
#define OPENED                       HIGH

#define ENABLED                      true
#define DISABLED                     false

#define PUMPon                       LOW
#define PUMPoff                      HIGH

//***************************************************************

boolean switch1Flag                = !ENABLED;
boolean switch2Flag                = !ENABLED;
boolean switch3Flag                = !ENABLED;
boolean switch4Flag                = !ENABLED;  //valve
boolean switch5Flag                = !ENABLED;  //valve
boolean switch6Flag                = !ENABLED;  //valve


byte lastSwitch1State              = OPENED;
byte lastSwitch2State              = OPENED;
byte lastSwitch3State              = OPENED;
byte lastSwitch4State              = OPENED;  //valve
byte lastSwitch5State              = OPENED;  //valve
byte lastSwitch6State              = OPENED;  //valve


byte pump1State                    = PUMPoff;
byte pump2State                    = PUMPoff;
byte pump3State                    = PUMPoff;
byte pump4State                    = PUMPoff; //valve
byte pump5State                    = PUMPoff; //valve
byte pump6State                    = PUMPoff; //valve


const byte pump1Relay              = 3;
const byte pump2Relay              = 4;
const byte pump3Relay              = 5;

const byte pump4Relay              = 6; //valve
const byte pump5Relay              = 1; //valve
const byte pump6Relay              = 2; //valve

const byte SWITCH1                 = 7;
const byte SWITCH2                 = 10;
const byte SWITCH3                 = 9;

const byte SWITCH4                 = 8; //valve
const byte SWITCH5                 = 11;  //valve
const byte SWITCH6                 = 12;  //valve

const byte heartbeatLED            = 13;

//timing stuff
unsigned long pump1Millis;
unsigned long pump2Millis;
unsigned long pump3Millis;
unsigned long pump4Millis;  //valve
unsigned long pump5Millis;  //valve
unsigned long pump6Millis;  //valve
unsigned long heartbeatMillis;
unsigned long switchMillis;

unsigned long pump1_interval;
unsigned long pump2_interval;
unsigned long pump3_interval;
unsigned long pump4_interval; //valve
unsigned long pump5_interval; //valve
unsigned long pump6_interval; //valve

// PUMP 1 right 
const unsigned long pump1_ON_time  = 1000;
const unsigned long pump1_OFF_time = 4000;

// PUMP 2 left 
const unsigned long pump2_ON_time  = 9000;
const unsigned long pump2_OFF_time = 3000;

// PUMP 3 left 
const unsigned long pump3_ON_time  = 1000;
const unsigned long pump3_OFF_time = 1400;

// PUMP 4 (Valve) 
const unsigned long pump4_ON_time  = 1500;   //valve // geschlosse lampe aus (switch need to be on o)
const unsigned long pump4_OFF_time = 1000;   //valve  // offen lampe an

// PUMP 5 (Valve) 
const unsigned long pump5_ON_time  = 1000;  //valve
const unsigned long pump5_OFF_time = 1400;  //valve

// PUMP 6 (Valve) 
const unsigned long pump6_ON_time  = 1000;  //valve
const unsigned long pump6_OFF_time = 1400;  //valve



//***************************************************************
void setup()
{
  Serial.begin(115200);

  pinMode(heartbeatLED, OUTPUT);
  pinMode(pump1Relay, OUTPUT);
  pinMode(pump2Relay, OUTPUT);
  pinMode(pump3Relay, OUTPUT);
  pinMode(pump4Relay, OUTPUT);
  pinMode(pump5Relay, OUTPUT);
  pinMode(pump6Relay, OUTPUT);

  pinMode(SWITCH1, INPUT_PULLUP);
  pinMode(SWITCH2, INPUT_PULLUP);
  pinMode(SWITCH3, INPUT_PULLUP);
  pinMode(SWITCH4, INPUT_PULLUP);
  pinMode(SWITCH5, INPUT_PULLUP);
  pinMode(SWITCH6, INPUT_PULLUP);

  digitalWrite(pump1Relay, PUMPoff);
  pump1State = PUMPoff;
  pump1_interval = pump1_OFF_time;

  digitalWrite(pump2Relay, PUMPoff);
  pump2State = PUMPoff;
  pump2_interval = pump2_OFF_time;

  digitalWrite(pump3Relay, PUMPoff);
  pump3State = PUMPoff;
  pump3_interval = pump3_OFF_time;

   digitalWrite(pump4Relay, PUMPoff);
  pump4State = PUMPoff;
  pump4_interval = pump4_OFF_time;

    digitalWrite(pump5Relay, PUMPoff);
  pump5State = PUMPoff;
  pump5_interval = pump5_OFF_time;

      digitalWrite(pump6Relay, PUMPoff);
  pump6State = PUMPoff;
  pump6_interval = pump6_OFF_time;

} //END of setup()

//***************************************************************
void loop()
{
  //*************************************                          h e a r t b e a t   T I M E R
  //to see if the sketch is blocking,
  //toggle the heartbeat LED every 500ms
  if (millis() - heartbeatMillis >= 500)
  {
    //restart the TIMER
    heartbeatMillis = millis();

    //toggle the LED
    digitalWrite(heartbeatLED, !digitalRead(heartbeatLED));
  }

  //*************************************                          c h e c k S w i t c h   T I M E R
  //is it time to read the switches ?
  if (millis() - switchMillis >= 50)
  {
    //restart the TIMER
    switchMillis = millis();

checkSwitches();
  }

  //*************************************                          P U M P   1
  //if the switch is 'not enabled' proceed with autmatic operation
  if (switch1Flag == !ENABLED)
  {
    //***********************************                          p u m p 1   T I M E R
    //is it time to toggle the pump relay ?
    if (millis() - pump1Millis >= pump1_interval)
    {
      //restart the TIMER
      pump1Millis = millis();

      //is pump1 now OFF ?
      if (pump1State == PUMPoff)
      {
        digitalWrite(pump1Relay, PUMPon);

        pump1State = PUMPon;

        pump1_interval = pump1_ON_time;
      }

      else
      {
        digitalWrite(pump1Relay, PUMPoff);

        pump1State = PUMPoff;

        pump1_interval = pump1_OFF_time;
      }
    
  } //END of   if(switch1Flag == !ENABLED)


  //*************************************                          P U M P   2
  //if the switch is 'not enabled' proceed with autmatic operation
  if (switch2Flag == !ENABLED)
  {
    //***********************************                          p u m p 2   T I M E R
    //is it time to toggle the pump relay ?
    if (millis() - pump2Millis >= pump2_interval)
    {
      //restart the TIMER
      pump2Millis = millis();

      //is pump2 now OFF ?
      if (pump2State == PUMPoff)
      {
        digitalWrite(pump2Relay, PUMPon);

        pump2State = PUMPon;

        pump2_interval = pump2_ON_time;
      }

      else
      {
        digitalWrite(pump2Relay, PUMPoff);

        pump2State = PUMPoff;

        pump2_interval = pump2_OFF_time;
      }
    }
  } //END of    if(switch2Flag == !ENABLED)

} //END of loop()




//*************************************                          P U M P   3
  //if the switch is 'not enabled' proceed with autmatic operation
  if (switch3Flag == !ENABLED)
  {
    //***********************************                          p u m p 3   T I M E R
    //is it time to toggle the pump relay ?
    if (millis() - pump3Millis >= pump3_interval)
    {
      //restart the TIMER
      pump3Millis = millis();

      //is pump3 now OFF ?
      if (pump3State == PUMPoff)
      {
        digitalWrite(pump3Relay, PUMPon);

        pump3State = PUMPon;

        pump3_interval = pump3_ON_time;
      }

      else
      {
        digitalWrite(pump3Relay, PUMPoff);

        pump3State = PUMPoff;

        pump3_interval = pump3_OFF_time;
      }
    }
  } //END of    if(switch3Flag == !ENABLED)

} //END of loop()

//*************************************                          P U M P   VALVE 4
  //if the switch is 'not enabled' proceed with autmatic operation
  if  (switch4Flag == !ENABLED)
  {
    //***********************************                          p u m p 4   T I M E R
    //is it time to toggle the pump relay ?
    if (millis() - pump4Millis >= pump4_interval)
    {
      //restart the TIMER
      pump4Millis = millis();

      //is pump4 now OFF ?
      if (pump4State == PUMPoff)
      {
        digitalWrite(pump4Relay, PUMPon);

        pump4State = PUMPon;

        pump4_interval = pump4_ON_time;
      }

      else
      {
        digitalWrite(pump4Relay, PUMPoff);

        pump4State = PUMPoff;

        pump4_interval = pump4_OFF_time;
      }
    }
  } //END of    if(switch4Flag == !ENABLED)
}
} //END of loop()


//*************************************                          P U M P   VALVE 5
  //if the switch is 'not enabled' proceed with autmatic operation
  if (switch5Flag == !ENABLED)
  {
    //***********************************                          p u m p 5  T I M E R
    //is it time to toggle the pump relay ?
    if (millis() - pump5Millis >= pump5_interval)
    {
      //restart the TIMER
      pump5Millis = millis();

      //is pump5 now OFF ?
      if (pump5State == PUMPoff)
      {
        digitalWrite(pump5Relay, PUMPon);

        pump5State = PUMPon;

        pump5_interval = pump5_ON_time;
      }

      else
      {
        digitalWrite(pump5Relay, PUMPoff);

        pump5State = PUMPoff;

        pump5_interval = pump5_OFF_time;
      }
    }
  } //END of    if(switch5Flag == !ENABLED)
}
} //END of loop()




//*************************************                          P U M P   VALVE 6
  //if the switch is 'not enabled' proceed with autmatic operation
  if (switch6Flag == !ENABLED)
  {
    //***********************************                          p u m p 6  T I M E R
    //is it time to toggle the pump relay ?
    if (millis() - pump6Millis >= pump6_interval)
    {
      //restart the TIMER
      pump6Millis = millis();

      //is pump6 now OFF ?
      if (pump6State == PUMPoff)
      {
        digitalWrite(pump6Relay, PUMPon);

        pump6State = PUMPon;

        pump6_interval = pump6_ON_time;
      }

      else
      {
        digitalWrite(pump6Relay, PUMPoff);

        pump6State = PUMPoff;

        pump6_interval = pump6_OFF_time;
      }
    }
  } //END of    if(switch6Flag == !ENABLED)
}
} //END of loop()



//********************************************************************************
void checkSwitches()
{

  //*********************************************                    S W I T C H 1
  //SWITCH1 code
  byte currentState = digitalRead(SWITCH1);

  //**********************
  //was there a change in state ?
  if (lastSwitch1State != currentState)
  {
    //update to the new state
    lastSwitch1State = currentState;

    //**********************                                         C L O S E D
    //is the switch closed ?
    if (currentState == CLOSED)
    {
      digitalWrite(pump1Relay, PUMPon);

      pump1State = PUMPon;

      //allow manual switch bypass
      switch1Flag = ENABLED;
    }

    //**********************                                         O P E N E D
    //the switch is opened
    else
    {
      digitalWrite(pump1Relay, PUMPoff);

      pump1State = PUMPoff;

      pump1_interval = pump1_OFF_time;

      pump1Millis = millis();

      //cancel manual switch bypass
      switch1Flag = DISABLED;
    }

  } //END of SWITCH1 code


  //*********************************************                    S W I T C H 2
  //SWITCH2 code
  currentState = digitalRead(SWITCH2);

  //**********************
  //was there a change in state ?
  if (lastSwitch2State != currentState)
  {
    //update to the new state
    lastSwitch2State = currentState;

    //**********************                                         C L O S E D
    //is the switch closed ?
    if (currentState == CLOSED)
    {
      digitalWrite(pump2Relay, PUMPon);

      pump2State = PUMPon;

      //allow manual switch bypass
      switch2Flag = ENABLED;
    }

    //**********************                                         O P E N E D
    //the switch is opened
    else
    {
      digitalWrite(pump2Relay, PUMPoff);

      pump2State = PUMPoff;

      pump2_interval = pump2_OFF_time;

      pump2Millis = millis();

      //cancel manual switch bypass
      switch2Flag = DISABLED;
    }

  } //END of SWITCH2 code


 //*********************************************                    S W I T C H 3
  //SWITCH3 code
  currentState = digitalRead(SWITCH3);

  //**********************
  //was there a change in state ?
  if (lastSwitch3State != currentState)
  {
    //update to the new state
    lastSwitch3State = currentState;

    //**********************                                         C L O S E D
    //is the switch closed ?
    if (currentState == CLOSED)
    {
      digitalWrite(pump3Relay, PUMPon);

      pump3State = PUMPon;

      //allow manual switch bypass
      switch3Flag = ENABLED;
    }

    //**********************                                         O P E N E D
    //the switch is opened
    else
    {
      digitalWrite(pump3Relay, PUMPoff);

      pump3State = PUMPoff;

      pump3_interval = pump3_OFF_time;

      pump3Millis = millis();

      //cancel manual switch bypass
      switch3Flag = DISABLED;
    }

  } //END of SWITCH3 code


 //*********************************************                    S W I T C H 4
  //SWITCH4 code
  currentState = digitalRead(SWITCH4);

  //**********************
  //was there a change in state ?
  if (lastSwitch4State != currentState)
  {
    //update to the new state
    lastSwitch4State = currentState;

    //**********************                                         C L O S E D
    //is the switch closed ?
    if (currentState == CLOSED)
    {
      digitalWrite(pump4Relay, PUMPon);

      pump4State = PUMPon;

      //allow manual switch bypass
      switch4Flag = ENABLED;
    }

    //**********************                                         O P E N E D
    //the switch is opened
    else
    {
      digitalWrite(pump4Relay, PUMPoff);

      pump4State = PUMPoff;

      pump4_interval = pump4_OFF_time;

      pump4Millis = millis();

      //cancel manual switch bypass
      switch4Flag = DISABLED;
    }

  } //END of SWITCH4 code


   //*********************************************                    S W I T C H 5
  //SWITCH5 code
  currentState = digitalRead(SWITCH5);

  //**********************
  //was there a change in state ?
  if (lastSwitch5State != currentState)
  {
    //update to the new state
    lastSwitch5State = currentState;

    //**********************                                         C L O S E D
    //is the switch closed ?
    if (currentState == CLOSED)
    {
      digitalWrite(pump5Relay, PUMPon);

      pump5State = PUMPon;

      //allow manual switch bypass
      switch5Flag = ENABLED;
    }

    //**********************                                         O P E N E D
    //the switch is opened
    else
    {
      digitalWrite(pump5Relay, PUMPoff);

      pump5State = PUMPoff;

      pump5_interval = pump5_OFF_time;

      pump5Millis = millis();

      //cancel manual switch bypass
      switch5Flag = DISABLED;
    }

  } //END of SWITCH5 code



//*********************************************                    S W I T C H 6
  //SWITCH6 code
  currentState = digitalRead(SWITCH6);

  //**********************
  //was there a change in state ?
  if (lastSwitch6State != currentState)
  {
    //update to the new state
    lastSwitch6State = currentState;

    //**********************                                         C L O S E D
    //is the switch closed ?
    if (currentState == CLOSED)
    {
      digitalWrite(pump6Relay, PUMPon);

      pump6State = PUMPon;

      //allow manual switch bypass
      switch6Flag = ENABLED;
    }

    //**********************                                         O P E N E D
    //the switch is opened
    else
    {
      digitalWrite(pump6Relay, PUMPoff);

      pump6State = PUMPoff;

      pump6_interval = pump6_OFF_time;

      pump6Millis = millis();

      //cancel manual switch bypass
      switch6Flag = DISABLED;
    }

  } //END of SWITCH6 code

  //*********************************************                    o t h e r S w i t c h e s
  //next switch code
  //*********************************************

} //END of   checkSwitches()



//********************************************************************************

If you Auto Format the sketch in the IDE you will see this at the end of the loop() function

  }
} //END of    if(switch6Flag == !ENABLED)
}
} //END of loop()

You either have too many closing braces or not enough opening braces around code blocks

Over to you to find which it is and fix it

1 Like

Copy/paste error(s) it looks like. When you copied the code for the switch flags you also copied the closing bracket for loop() multiple times. Pretty easy to see in this case since those brackets have the comment "END of loop()".

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.