Dynamic code from VB to arduino.

i have a 3 led lights, connected to pin 3,4,5 of my arduino uno

this my code in arduino: i used switch case so i can easily call the codes from my arduino.

int led3 = 3;
int led4 = 4;
int led5 = 5;

void setup() {                
  
  Serial.begin(9600); 
 
 pinMode(led3, OUTPUT); 
 pinMode(led4, OUTPUT);
pinMode(led5, OUTPUT); 

}

void loop() {
  int inByte = Serial.read();
  
  switch (inByte){
    case 'a': 
  digitalWrite(led3, HIGH);  
  delay(500);        
  digitalWrite(led3, LOW);   
  delay(500);  
digitalWrite(led4, HIGH);  
  delay(500);
digitalWrite(led4, LOW);  
  delay(500);  // wait for a second
  digitalWrite(led5, HIGH);   
  delay(500);              
  digitalWrite(led5, LOW);    
  delay(500);  

  break;
  
  case 'b':
  
  digitalWrite(led5 , HIGH) ;  
  delay(500);   
   digitalWrite(led5 , LOW);  
  delay(500);
  digitalWrite(led4 , HIGH) ;  
  delay(500);   
   digitalWrite(led4 , LOW);  
  delay(500);
   digitalWrite(led3 , HIGH) ;  
  delay(500);   
   digitalWrite(led3 , LOW);  
  delay(500);
  break;
  
  case 'c':
  digitalWrite(led4 , HIGH) ;  
  delay(500);   
   digitalWrite(led4 , LOW);  
  delay(500);
 digitalWrite(led5 , HIGH) ;  
  delay(500);   
   digitalWrite(led5 , LOW);  
  delay(500);
 digitalWrite(led3 , HIGH) ;  
  delay(500);   
   digitalWrite(led3 , LOW);  
  delay(500);
  break;
  
  case '1':
   digitalWrite(led3 , HIGH) ;
  break;
  case '2':
   digitalWrite(led3 , LOW) ;
   break;
   
   case '3':
   digitalWrite(led4 , HIGH) ;
  break;
  case '4':
   digitalWrite(led4 , LOW) ;
  break;
  
   case '5':
   digitalWrite(led5 , HIGH) ;
  break;
  case '6':
   digitalWrite(led5 , LOW) ;
  break;
  case '1':
   digitalWrite(led3 , HIGH) ;
  break;
  case '2':
   digitalWrite(led3 , LOW) ;
   break;
   
   case '3':
   digitalWrite(led4 , HIGH) ;
  break;
  case '4':
   digitalWrite(led4 , LOW) ;
  break;
  
 case '5':
   digitalWrite(led5 , HIGH) ;
  break;
  case '6':
   digitalWrite(led5 , LOW) ;
  break;
  
}
}

code in vb:

this is how i send byte in my arduino from vb.

My problem is my code is very Static. i want to make it dynamic.
i want to send a code like this in my serial monitor.

and there will be sequence of blinking led in my arduino

i hope some one can help me. Thank you so much.

You are using the terms static and dynamic incorrectly.

You have multiple case statements with the same value. Only one of them will ever be executed.

When a character appears on the serial port, and trigger a case, no more data will be read until the case completes. 'a' will take 3 seconds to complete.

If you want to send another letter and make something else happen at the same time case 'a' is, you need to rewrite your code, not using delay().

Look at the blink without delay example. Look at state machines, too.

You are using the terms static and dynamic incorrectly.

im sorry for not using it correctly.. im very newbie..

You have multiple case statements with the same value. Only one of them will ever be executed.

When a character appears on the serial port, and trigger a case, no more data will be read until the case completes. 'a' will take 3 seconds to complete.

if i send this letters in my serial "aaabbccc" , the case 'a' statement it will be read 3 times then after that the case 'b' will be read 2 times and the 'c' is 3 times and so on. arduino will execute the whole line.. thats why i want to make it make it like this. "a5b2c3"
thanks a lot.

if i send this letters in my serial "aaabbccc" , the case 'a' statement it will be read 3 times then after that the case 'b' will be read 2 times and the 'c' is 3 times and so on. arduino will execute the whole line.. thats why i want to make it make it like this. "a5b2c3"
thanks a lot.

You need to send more than that. You need to define some kind of end of packet marker. The VB app needs to send the string (with delimiters) followed by the end of packet marker.

The Arduino needs to read and store the serial data until the end of packet marker arrives, in a NULL terminated char array.

When the end of packet marker arrives, you need to parse the array, using strtok(). The first character in each token is the command (the case value). After copying that, change the first character to a space. Then, call atoi() on the token. The result is an int - the repetition count.

In the cases, use a for loop to iterate the required number of times.

You need to send more than that. You need to define some kind of end of packet marker. The VB app needs to send the string (with delimiters) followed by the end of packet marker.

The Arduino needs to read and store the serial data until the end of packet marker arrives, in a NULL terminated char array.

When the end of packet marker arrives, you need to parse the array, using strtok(). The first character in each token is the command (the case value). After copying that, change the first character to a space. Then, call atoi() on the token. The result is an int - the repetition count.

In the cases, use a for loop to iterate the required number of times.

i really appreciate your reply sir PaulS but i cant understand your terms like the "packet marker", "strtok()" ,"atoi()". im sorry sir but im a very newbie in arduino programming. Can you please give me a example code of what you want to explain to me. may be i can understand it more clearly..

Thanks again.

i really appreciate your reply sir PaulS but i cant understand your terms like the "packet marker", "strtok()" ,"atoi()". im sorry sir but im a very newbie in arduino programming.

"packet marker" - a character (or characters) at the end of a serial message so that the receiving system knows that the message is complete. This is often a carriage return or newline but does not have to be.

"strtok()" - a C function that will split a string into sections. This is done by defining the character (token) at which the string should be split. This is often a comma, but does not have to be. It is the responsibility of the system sending the message to insert the delimiters at appropriate points in the message. Google it

"atoi()" - a C function that will take a string and convert it to an int. Google it

To summarise:

read the incoming characters and store them in an array as a string until the packet marker is received
break the string into sections using the strtok() function and save the sections in strings
convert the numeric sections into ints using the atoi() function
use the ints and strings to control actions in your program

NOTE - this explanation uses C strings (null terminated arrays of chars), not the String class which has had problems in its Arduino implmentation

If you send "<a3, b2, c3>", then < is a start of packet marker and > is an end of packet marker. The following code will collect the data into an array:

#define SOP '<'
#define EOP '>'

bool started = false;
bool ended = false;

char inData[80];
byte index;

void setup()
{
   Serial.begin(57600);
   // Other stuff...
}

void loop()
{
  // Read all serial data available, as fast as possible
  while(Serial.available() > 0)
  {
    char inChar = Serial.read();
    if(inChar == SOP)
    {
       index = 0;
       inData[index] = '\0';
       started = true;
       ended = false;
    }
    else if(inChar == EOP)
    {
       ended = true;
       break;
    }
    else
    {
      if(index < 79)
      {
        inData[index] = inChar;
        index++;
        inData[index] = '\0';
      }
    }
  }

  // We are here either because all pending serial
  // data has been read OR because an end of
  // packet marker arrived. Which is it?
  if(started && ended)
  {
    // The end of packet marker arrived. Process the packet

    // Reset for the next packet
    started = false;
    ended = false;
    index = 0;
    inData[index] = '\0';
  }
}

Where it says "Process the packet" is where you would use strtok() and atoi() to extract and process the tokens ("a3", "b2", "c3"). You can google, can't you?