Visual basic serial communication

Hello,

I'm trying to create a program in Visual Basic (2010) that would let me control leds connected to my Arduino board.

I use 3 different digital outputs for 3 different leds. However the issue I currently have is that for example when I turn on led1 all 3 of the leds would turn on, so basicly it writes the same command to all 3 of the outputs instead of doing so separately.

I suspect my error to be in the Arduino code rather than my Visual basic code, because I've only been using Arduino for a week.

This is the code I used:

void setup() {
  pinMode(13,OUTPUT);
  Serial.begin(9600)
  
  ;pinMode(12,OUTPUT);
  Serial.begin(9600)
  
  ;pinMode(11,OUTPUT);
  Serial.begin(9600)

;}

void loop() {
  int val;
  if (Serial.available()){
    
    delay(100);
    
    while (Serial.available() > 0)  {
      val=Serial.read();
      
      if(val=='1') {digitalWrite(13,HIGH); }
      else if (val=='0') {digitalWrite(13,LOW); }
      
      if(val=='1') {digitalWrite(12,HIGH); }
      else if (val=='0') {digitalWrite(12,LOW); }
      
      if(val=='1') {digitalWrite(11,HIGH); }
      else if (val=='0') {digitalWrite(11,LOW); }
    }
  }
}

Hopefully someone can help me out on this, thanks :)

why not use switch instate of if

you would get something like

switch (var) { case 1: //do something when var equals 1 break; case 2: //do something when var equals 2 break; default: // if nothing else matches, do the default // default is optional }

see also the online manual http://arduino.cc/en/Reference/SwitchCase

Semicolons go the end of statements, NOT at the beginning. Fix every line that starts with a semicolon.

The semicolons wouldn’t cause the code to fail, though that is definitely bad form when it comes to programming.

The poster’s problem is this:

while (Serial.available() > 0)  {
      val=Serial.read();
      
      if(val=='1') {digitalWrite(13,HIGH); }
      else if (val=='0') {digitalWrite(13,LOW); }
      
      if(val=='1') {digitalWrite(12,HIGH); }
      else if (val=='0') {digitalWrite(12,LOW); }
      
      if(val=='1') {digitalWrite(11,HIGH); }
      else if (val=='0') {digitalWrite(11,LOW); }
    }

Notice that for every LED, he/she is testing for the same val Hence, when a 1 is sent, all the IF tests will trigger, so all LEDs will turn on and off at once. You need different values for each one if you want them to operate independently.

So we have a case here where you program is doing exactly what you programmed it to do. :wink:

while (Serial.available() > 0)  {
      val=Serial.read();
      
      if(val=='1') {digitalWrite(13,HIGH); }
      else if (val=='0') {digitalWrite(13,LOW); }
      
      if(val=='2') {digitalWrite(12,HIGH); }
      else if (val=='3') {digitalWrite(12,LOW); }
      
      if(val=='4') {digitalWrite(11,HIGH); }
      else if (val=='5') {digitalWrite(11,LOW); }
    }

If I were to guess based on the code, you are sending three bytes. One for each LED. So for example 101 would mean LED1 ON, LED2 off, LED3 ON. I say that because the while(Serial) part is telling it to keep grabbing bytes from the serial buffer until it is empty. If so, you need something more like this…

for(int i=0;i < 2;i++){ //loop 3 times to grab three bytes
	val=Serial.read(); //one byte is read per loop
	int pin = 13-i; //first byte will be pin 13, and first i will be 0. Since the pins are descending, we have to subtract
	if(val=='1){
		
		digitalWrite(pin,HIGH); //turn LED on
	}
	else if(val=='0'){
		digitalWrite(pin,LOW); //turn LED off
	}
} //run loop again until 3 loops are done
Serial.flush() //Empty serial buffer after the 3 bytes were processed

If so, you need something more like this...

Except for the Serial.flush() which does NOT do what your comment suggests it does.

flush() Description Waits for the transmission of outgoing serial data to complete. (Prior to Arduino 1.0, this instead removed any buffered incoming serial data.)

Dangnabbit. You are correct, of course.

Thank you for the help everyone. I got my program to work.