In the below code, "K" value is not updated ,when I give a second input ..
void setup()
{
Serial.begin(115200);
}
void loop()
{
Serial.print("Enter a No:\n ");
String source;
char sv[2];
readSerial(sv);
Serial.print("sv="); Serial.print(sv);
Serial.print("\n");
int k = Serial.write(sv);
Serial.print("k=");Serial.print(k);
switch (k) {
case 1: //Serial.print("Kanuvai");
source = String("Kanuvai");
break;
case 2: //Serial.print("KNG Pudur");
source = String("KNG Pudur");
break;
default:
Serial.print("Enter a correct value");
break;
}
Serial.println("Source");
Serial.println(source);
}
/*
Read input serial
*/
int readSerial(char result[])
{
int i = 0;
while(1)
{
while (Serial.available() > 0)
{
char inChar = Serial.read();
if (inChar == '\n')
{
result[i] = '\0';
Serial.flush();
return 0;
}
if(inChar!='\r')
{
result[i] = inChar;
i++;
}
}
}
}
Result :
WHEN I GAVE 1 :
sv=1
1k=1Source
Kanuvai
WHEN I GAVE 1 :
sv=2
2k=1Source
Kanuvai
"K" value is not updated .. Why is this not updates its values