giiibs
February 20, 2023, 6:53pm
1
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 ?
giiibs
February 20, 2023, 6:59pm
4
Thanks, I'm using Mega 2560
b707
February 20, 2023, 7:01pm
5
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'?
giiibs
February 20, 2023, 7:08pm
7
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);
}
giiibs
February 20, 2023, 7:10pm
8
Sorry I forget to modify that while posting, "Gradient" is actually "Typename"
b707
February 20, 2023, 7:10pm
9
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.
giiibs
February 20, 2023, 7:14pm
10
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.
giiibs
February 20, 2023, 7:20pm
11
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
giiibs
February 20, 2023, 7:25pm
13
OMG Thank u.
I shouldnt use switch, unfamiliar to me
Only way it will be familiar to you will be to use it.
1 Like
system
Closed
August 19, 2023, 7:28pm
15
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.