VB6 to arduino OUTPUT Slow

//O/P ADD
int K01 =  22;      // PIN OUTPUT 01 LED
String incoming = "";   // for incoming serial string data


void setup() 
{
// initialize O/P:
  pinMode(K01, OUTPUT); 

  // initialize serial communication:
  Serial.begin(9600);
}

void loop() 
{
CheckOUTPUT();
}

void CheckOUTPUT()
{
  if (Serial.available() > 0) 
  {
    // read the incoming:
    incoming = Serial.readString();
//    Serial.println(incoming);
    
    if (incoming == ("K01_ON") )
    {
      Serial.println("K01_ON");
      digitalWrite(K01, HIGH);  
    } 
    else if (incoming == "K01_OFF") 
    {
      Serial.println("K01_OFF");
      digitalWrite(K01, LOW);  
    }      
  }
    
    
    Serial.flush();    
  }

VB Code:

FormMain.Com1.Output = "K01_ON"

i send K01_ON frm vb6 to arduino.

but it take about 2 second for LED response to power on.

ist normal?

Yes, readString has a timeout value. It's basically used to determine when no more characters arrive.

Further you should not use String (capital S) if you don't want to experience unexplainable behaviour when you start making heavy use of Strings.

I suggest that you read the serial input basics to get some ideas.

Serial.readString() is a blocking function with a timeout.

Have a look at the examples in Serial Input Basics - simple reliable ways to receive data. There is also a parse example to illustrate how to extract numbers from the received text.

The technique in the 3rd example will be the most reliable.

You can send data in a compatible format with code like this - you can get the same effect in any programming language

Serial.print('<'); // start marker
Serial.print(value1);
Serial.print(','); // comma separator
Serial.print(value2);
Serial.println('>'); // end marker

There is no added value in sending "K01_ON" rather than just 'N' for ON and sending a single character will make the Arduino code simpler.

There is this clever idea if you just want to check for single characters.

...R

Robin2:
Serial.readString() is a blocking function with a timeout.

Have a look at the examples in Serial Input Basics - simple reliable ways to receive data. There is also a parse example to illustrate how to extract numbers from the received text.

The technique in the 3rd example will be the most reliable.

You can send data in a compatible format with code like this - you can get the same effect in any programming language

Serial.print('<'); // start marker

Serial.print(value1);
Serial.print(','); // comma separator
Serial.print(value2);
Serial.println('>'); // end marker




There is no added value in sending "K01_ON" rather than just 'N' for ON and sending a single character will make the Arduino code simpler.

There is [this clever idea](https://forum.arduino.cc/index.php?topic=573423.msg3906953#msg3906953) if you just want to check for single characters.

...R

i cant use single character because im planning to control more than 10 Output separately

jazmie:
i cant use single character because im planning to control more than 10 Output separately

Just a statement?

sterretje:
Just a statement?

void CheckOUTPUT()
{
  if (Serial.available() > 0) 
  {
    // read the incoming:
    incoming = Serial.readString();
    Serial.println(incoming);
    
    OUTPUT01();       
    OUTPUT02();  
    OUTPUT03();    
  }
    
    
//    else 
//    {
//      Serial.println("something else");
//      incoming = "";
//    } 
    Serial.flush();    
  }

void OUTPUT01()
{
    if (incoming == ("K01_ON") )
    {
      Serial.println("K01_ON");
      digitalWrite(K01, HIGH);  
    } 
    else if (incoming == "K01_OFF") 
    {
      Serial.println("K01_OFF");
      digitalWrite(K01, LOW);  
    }      
}  

void OUTPUT02()
{
    if (incoming == ("K02_ON") )
    {
      Serial.println("K02_ON");
      digitalWrite(K02, HIGH);  
    } 
    else if (incoming == "K02_OFF") 
    {
      Serial.println("K02_OFF");
      digitalWrite(K02, LOW);  
    }         
} 

void OUTPUT03()
{
    if (incoming == ("K03_ON") )
    {
      Serial.println("K03_ON");
      digitalWrite(K03, HIGH);  
    } 
    else if (incoming == "K03_OFF") 
    {
      Serial.println("K03_OFF");
      digitalWrite(K03, LOW);  
    }         
}

jazmie:
i cant use single character because im planning to control more than 10 Output separately

At last count there were over 60 human readable ASCII characters, and there are 255 in total.

...R

I believe you think the following statement does something different than it really does.

    Serial.flush();

Robin2:
At last count there were over 60 human readable ASCII characters, and there are 255 in total.

...R

now temporary i change using character as advise...its working perfectly.
for string matter just easier for me reference for future and arrange which output etc if compare character that hard to refer.

anyway tq all's

Whandall:
I believe you think the following statement does something different than it really does.

    Serial.flush();

ok noted...tq