sending More command (Val) to Arduino?

hello guys ,
my project is controlling LED light by using Visual basic Program made by me.

i have a little problem in my project , how can i send more command to the arduino from my PC??

for example ,

this is the Arduino codes that i uploaded ,


int ledPin = 13; // the number of the LED pin

void setup() {
Serial.begin(9600); // set serial speed
pinMode(ledPin, OUTPUT); // set LED as output
digitalWrite(ledPin, LOW); //turn off LED
}

void loop(){
while (Serial.available() == 0); // do nothing if nothing sent
int val = Serial.read() - '0'; // deduct ascii value of '0' to find numeric value of sent number

if (val == 1) { // test for command 1 then turn on LED
Serial.println("LED on");
digitalWrite(ledPin, HIGH); // turn on LED
}
else if (val == 0) // test for command 0 then turn off LED
{
Serial.println("LED OFF");
digitalWrite(ledPin, LOW); // turn off LED
}
else // if not one of above command, do nothing
{
//val = val;
}
Serial.println(val);
Serial.flush(); // clear serial port
}


as you can see , ( val = 1 ) will turn LED 1 on , ( val = 2) will turn LED 1 off .
and i also added 2 more LEDs light to the same arduino sketch , so now ( val = 3 ) will turn LED 2 on , (val = 4 ) will turn LED 2 Off , and the same process to the other LED ,

but when i add one more LED , and when i type ( val = 10 ) the LED 1 Will turn On ,

and i don't know why LED 1 turn on although i specified the val = 10 .


here is how to send (Val) from my program i made in vb

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles button1.Click
SerialPort1.Open()
SerialPort1.Write("1") <<<<< this will turn LED 1 On
SerialPort1.Close()
End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles button2.Click
SerialPort1.Open()
SerialPort1.Write("0") <<<<<< this will turn LED 1 off
SerialPort1.Close()

End Sub

and same process for the other LED depending on their Val

please could any one tell me how to solve this problems ,,

..you read only one char.. '1'.. (read till eol)

but when i add one more LED , and when i type ( val = 10 ) the LED 1 Will turn On ,

and i don't know why LED 1 turn on although i specified the val = 10 .

LED 1 turns on because Serial.read() only reads one byte. Hence, when the 1 arrives the corresponding action is taken straight away.

If you want to send a longer command string then you must keep reading the serial input, adding each character received to a null terminated char array which is a C string (not String) until it is complete. Then you can act on the string received.

This page will help Gammon Forum : Electronics : Microprocessors : How to process incoming serial data without blocking

As you will see, having a character indicating that the serial input is complete makes the processing easier.

(deleted)

thaank you guys for helping me i appreciate thaat , but till this moment i didn't solve my problem =(

i wrote like what you said 00 11 , but unfortunately nothing happens .

how can i send two chars to the arduino . ?

:* please help me

how can i send two chars to the arduino . ?

Did you look at the link in my previous reply ?

UKHeliBob:

how can i send two chars to the arduino . ?

Did you look at the link in my previous reply ?

yees i saw it , it helps me a little bit, because i'm NOT an expert of programming arduino .

:~ but alse i couldn't find a solution for sending many chars to the AAduino...

need a help :frowning:

HOW to send many Chars to the arduino so i can control on many LEDs using My visual basic software????

Use the program from the link that I gave earlier, choosing your own terminating character. In the example code it is a newline but you can choose what you send.

When the terminating character is reached the example code calls this function

void process_data (const char * data)
{
  // for now just display it
  // (but you could compare it to some value, convert to an integer, etc.)
  Serial.println (data);
}  // end of process_data

As it says, all it does is to display what has been received but you could use the atoi() function, for instance, to turn the string in the data array into an integer and act on that integer value. Or don't bother to convert it and compare the string received with a constant string using the strcmp() functions. Both the atoi() and strcmp() functions are well documented on the Web and are standard C functions.

A very simple example of sending characters to the arduino from the serial monitor, capturing the characters into a String, then evaluating the captured String for the contained command.

// zoomkat 8-6-10 serial I/O string test
// type a string in serial monitor. then send or enter
// for IDE 0019 and later

int ledPin = 13;
String readString;

void setup() {
  Serial.begin(9600);
  pinMode(ledPin, OUTPUT); 
  Serial.println("serial on/off test 0021"); // so I can keep track
}

void loop() {

  while (Serial.available()) {
    delay(3);  
    char c = Serial.read();
    readString += c; 
  }

  if (readString.length() >0) {
    Serial.println(readString);

    if (readString == "on")     
    {
      digitalWrite(ledPin, HIGH);
    }
    if (readString == "off")
    {
      digitalWrite(ledPin, LOW);
    }

    readString="";
  } 
}

thank youu so much guys i appreciate that for you , i actually didn't get the solution but i will try to solve it . :sweat_smile: