State unexpected varies without INPUT

My code looks like following:

String recvString;

enum Gradient{
  ASC_HOR,
  DSC_HOR,
  ASC_VER,
  DSC_VER  
};

Gradient dir=Gradient::ASC_HOR;

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  pinMode(13,OUTPUT);

}

void loop() {

  if (Serial.available() > 0){
    recvString = Serial.readStringUntil('\n');
    Serial.flush();
  }

  if (recvString.equals("FLIP")){
    if(dir==Gradient::ASC_HOR||dir==Gradient::ASC_VER)  dir=static_cast<Gradient>(static_cast<int>(dir)+1);
    else dir=static_cast<Gradient>(static_cast<int>(dir)-1);
  }else if (recvString.equals("ROTATE")){
    if(dir==Gradient::ASC_HOR||dir==Gradient::DSC_HOR) dir=static_cast<Gradient>(static_cast<int>(dir)+2);
    else dir=static_cast<Gradient>(static_cast<int>(dir)-2);      
  }

  switch(dir){
    case ASC_HOR:
      {
        ascHor();
        Serial.println("ASC_HOR");
      }
    case DSC_HOR:
      {
        dscHor();
        Serial.println("DSC_HOR");
      }
    case ASC_VER:
      {
        ascVer();
        Serial.println("ASC_VER");
      }
    case DSC_VER:
      {
        dscVer();
        Serial.println("DSC_VER");
      }

    default:
      full();   
  }
  delay(5000);
}

void full(){
  digitalWrite(13,HIGH);
}

void ascHor(){
  digitalWrite(13,HIGH);
  delay(100);
  digitalWrite(13,LOW);
}

void dscHor(){
  digitalWrite(13,HIGH);
  delay(100);
  digitalWrite(13,LOW);
  delay(100);
  digitalWrite(13,HIGH);
  delay(100);
  digitalWrite(13,LOW);
}

void ascVer(){
  digitalWrite(13,HIGH);
  delay(2000);
  digitalWrite(13,LOW);
}

void dscVer(){
  digitalWrite(13,HIGH);
  delay(2000);
  digitalWrite(13,LOW);
  delay(2000);
  digitalWrite(13,HIGH);
  delay(2000);
  digitalWrite(13,LOW);  
}

Why does it print
"ASC_HOR,
DSC_HOR,
ASC_VER,
DSC_VER"
WITHOUT any input ? Thank u very much.

Welcome to the forum

Which Arduino board are you using ?

Thanks, I'm using Mega 2560

Your code is incomplete, Gradient type not defined, funcA(), FuncB()... etc are absent

The sketch as posted does not compile for me

'Gradient' does not name a type; did you mean 'radians'?

yeah, I dont want it to be too long .
it's as follows

void func(){
  digitalWrite(13,HIGH);
}

void funcR(){
  digitalWrite(13,HIGH);
  delay(100);
  digitalWrite(13,LOW);
}

void funcG(){
  digitalWrite(13,HIGH);
  delay(100);
  digitalWrite(13,LOW);
  delay(100);
  digitalWrite(13,HIGH);
  delay(100);
  digitalWrite(13,LOW);
}

void funcB(){
  digitalWrite(13,HIGH);
  delay(2000);
  digitalWrite(13,LOW);
}

void funcA(){
  digitalWrite(13,HIGH);
  delay(2000);
  digitalWrite(13,LOW);
  delay(2000);
  digitalWrite(13,HIGH);
  delay(2000);
  digitalWrite(13,LOW);  
}

Sorry I forget to modify that while posting, "Gradient" is actually "Typename"

Do not post a code snippets.
Please show code in full, posting it as new message.

Attention
Please copy the code directly from the IDE so you don't lose any brackets or letters. Judging by the question - you do not understand what may be important and what is not for your case. So copy the code EXACTLY as it is.

I've update it on the first floor, sorry again. I suppose it will be more convenient to see if it's shorter, seems it's wrong.

I've updated it on first floor

Do you see any differences between your switch-case and this one from the Arduino reference section:

switch (var) {
  case label1:
    // statements
    break;  //  <----------------
  case label2:
    // statements
    break;  //  <----------------
  default:
    // statements
    break;  //  <----------------
}
2 Likes

OMG Thank u. :rofl:
I shouldnt use switch, unfamiliar to me

Only way it will be familiar to you will be to use it.

1 Like

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