I am using Arduino Uno board. I wanted to evaluate test cases Serially.
It should keep executing case 1 unless user change input to other test case. Can someone guide me here. what changes can be done.
int incomingByte = 0; // for incoming serial data
void setup() {
pinMode(13,OUTPUT);
Serial.begin(9600); // opens serial port, sets data rate to 9600 bps
}
void loop() {
// send data only when you receive data:
if (Serial.available() > 0) {
// read the incoming byte:
incomingByte = Serial.read();
// say what you got:
Serial.print("I received: ");
Serial.println(incomingByte, DEC);
}
switch(incomingByte)
{
case 1: digitalWrite(13,HIGH); Serial.println(HIGH);break;
case 2: digitalWrite(13,LOW); Serial.println(LOW);break;
case 3: digitalWrite(13,HIGH); Serial.println(HIGH);delay(1000);
digitalWrite(13,LOW); Serial.println(LOW);delay(1000);
default : break;
}
}
when you enter 1,2 and 3, each followed by pressing Return
If you see
I received: 49
I received: 13
I received: 10
I received: 50
I received: 13
I received: 10
I received: 51
I received: 13
I received: 10
Then the 10s and 13s are the Line ending added by the Serial monitor which you set by changing the Line Ending setting of the Serial monitor
Now, what about the numbers 59, 50 and 51 ?
These are the ASCII values of the numbers you entered
You can fix this by either changing the values in the case statements to match or by subtracting 48 from the value of incomingByte before printing it or using it in switch/case
I could able to change easily one Switch case based on input. Thanks for the input.
In a similar way, can I do it for Serial?
I have attached an image for reference. I am getting character printed like this when goes inside second loop
Currently, I am switching Upper switch cases and could able to print particular cases.
I have another switch case in the test case:
Q: used to quit
C: continue the test
XYZ used to evaluate other cases as mentioned.
char incomingByte = 0; // for incoming serial data
char Switch_byte=0;
char Switch_command;
char myCommand;
unsigned int Main_flag=0;
unsigned int Sub_flag=0;
void setup()
{
pinMode(13, OUTPUT);
Serial.begin(9600); // opens serial port, sets data rate to 9600 bps
}
void loop()
{
// send data only when you receive data:
if(Main_flag==0)
{
if (Serial.available() > 0)
{
// read the incoming byte:
incomingByte = Serial.read();
// say what you got:
Serial.print("I received: ");
Serial.println(incomingByte);
if (incomingByte >= 'a' && incomingByte < 'd')
{
myCommand = incomingByte;
Main_flag=1;Sub_flag=0;
}
}
}
switch (myCommand)
{
case 'a':
Serial.print("Testcase1: \n");
Serial.print(1);
Serial.print("value: \n");
Serial.println(1);
break;
case 'b':
Serial.print("Testcase-b Executing \n");
if(Sub_flag==0)
{
if (Serial.available() > 0)
{
// read the incoming byte:
Switch_byte = Serial.read();
// say what you got:
Serial.print("I received sub function: ");
Serial.println(Switch_byte);
if (Switch_byte >= 1 && Switch_byte < 3)
{
Switch_command = Switch_byte;Sub_flag=0;Main_flag=1;
}
if (Switch_byte >= 'q' )
{
Sub_flag=1;Main_flag=0;
}
}
}
Serial.print("Switch_command: ");
Serial.println(Switch_command);
switch(Switch_command)
{
case 1:Serial.println("Case X \n");break;
case 2:Serial.println("Case y \n");break;
case 3:Serial.println("Case z \n");break;
}
break;
case 'c':
default :
break;
} //END of switch...case
delay(1000);
}
