Switch case Trouble

can i initialize the "case" in a switch case?

in my program i would like to start in the off "mode"

i get this error:
error: 'Mode' does not name a type; did you mean 'tone'?

where i try to do this
Mode =(0); //initial start in the off mode

// scope :  turn on and off a unit using the digital control fed to relay. The goal is to run the required cycle counts or till the unit dies
// On /off  requirement, example: 5 min on 3 min off.
// cycle counts are counted on an additional device. 
//this program only used to turn on and off unit. 


const int ledPin1 = 12; 

Onrequirement = 5;   //change on time here 
Offrequirement = 3;  //change off time here 

Mode  =(0); //initial start in the off mode

void setup() {
  Serial.begin(9600);
  pinMode(ledPin1, OUTPUT);
  
 
}

void loop() {
  
  switch (Mode);
  
    case 1; // On Mode
    
      if (Ontime  < Onrequirment)
       { 
        digitalWrite(ledPin1, HIGH);
        Ontime++;
        delay(5000);
        Serial.print Mode;
        Serial.println (Ontime);
       }

        Else  if 
        {
          Mode = 2;
          Ontime = 0;
        }

        break;

        
     case 2; // Off Mode
    
      if (Offtime  < Offrequirment)
       { 
        digitalWrite(ledPin1, Low);
        Offtime++;
        delay(5000);
        Serial.print Mode;
        Serial.println (Offtime);
       }

        Else  if 
        {
          Mode = (1);
          Offtime = 0;
        }

        break;
        
}

Start by fixing the basic problems with the sketch that you posted

For instance

Onrequirement = 5;   //change on time here
Offrequirement = 3;  //change off time here

Mode = (0);  //initial start in the off mode

This code is not in a function so will not compile

    switch (Mode)
        ;

    case 1; // On Mode

And these too

  1. What is the first semicolon doing there ?

  2. case statements use a colon not a semicolon

Also you may want to read up on how switch()... case() works. You can do what you want much simpler with the proper code.

and this

      Else  if 

That should be else rather than Else, and you don't have a condition for the else if anyway

oh, Are you saying it needs to be in the void setup instead?

I think you have to use case:

You could declare them as global variables and define their value outside of a function if that is what you want to do but they must have a data type in order to do that

What data type do you want those variables to have ?

There are so many errors in your code that it is difficult to describe them.
So I corrected it and I'm posting the corrected code.
I defined some variables that were not defined and assigned the value 0.

Hth

// scope :  turn on and off a unit using the digital control fed to relay. The goal is to run the required cycle counts or till the unit dies
// On /off  requirement, example: 5 min on 3 min off.
// cycle counts are counted on an additional device.
//this program only used to turn on and off unit.

const int ledPin1 = 12;
int Onrequirement = 5;   //change on time here
int Offrequirement = 3;  //change off time here
int Ontime = 0;
int Offtime = 0;
int Mode  = 0; //initial start in the off mode
//------------------------------------------------------------------
void setup() {
  Serial.begin(9600);
  pinMode(ledPin1, OUTPUT);
}
//------------------------------------------------------------------
void loop() {
  switch (Mode) {
    case 1: // On Mode
      if (Ontime  < Onrequirement) {
        digitalWrite(ledPin1, HIGH);
        Ontime++;
        delay(5000);
        Serial.print (Mode);
        Serial.println (Ontime);
      }
      else  {
        Mode = 2;
        Ontime = 0;
      }
      break;
    case 2: // Off Mode
      if (Offtime  < Offrequirement) {
        digitalWrite(ledPin1, LOW);
        Offtime++;
        delay(5000);
        Serial.print( Mode);
        Serial.println (Offtime);
      }
      else {
        Mode = (1);
        Offtime = 0;
      }
      break;
  }
}

You can absolutely do what you are aiming for. Any code you wanna write can be in a switch/case case.

But you'll have to get the pesky syntax right and as has been said…

… this may not be the best way to accomplish whatever it is your goal.

a7

i was planning on
1 for on mode (case 1)
2 for off mode (case2 )

yes i think that is my biggest handycap the syntax

Compare your code with the code by @ruilviana in post #9 and learn from it :wink:

The syntax is not difficult and your faults have been pointed out to you

  1. The switch statement does not end with a semicolon
  2. The case statements end end with a colon, not a semicolon
  3. Variables must be declared with a data type
  4. Variables can be defined as part of the declaration
  5. The else statement is all lowercase, not Else
  6. else/if requires a condition
  7. You can give the switch variable a value before the switch/case is executed for the first time. Indeed, the switch variable must have a value before the switch/case is executed

ur update works as i intended, i need to improve on my syntax and learn some basic concepts of Arduino.

i was close 80 % but no cigar
thanks everyone for the input.

i will save this as one of my templates for later projects