arduino problem with VB.Net 2008

hi guys,
i have been created the program in vb.net for controlling Dc motor
here is the program

int pin = 5;// Pwm motor vertikal 1 pin
int pin1 = 6;// pwm motor vertikal 2 pin
int m1 = 7;
int m2 = 4;
int pulsewidth2 = 0;
int pulsewidth3 = 50;
#define RED 11
int speed_case;
int speed_fix;
byte incomingByte;
void set_speed(int value);
void mhantap();

void setup(){
Serial.begin(9600);
speed_case = 1;

}

void loop(){

if(Serial.available()>0){

int userInput=Serial.read();
if(userInput==‘1’) // 1 to set the LED ON with pwm
{
digitalWrite(m2, HIGH);
analogWrite(pin, 50);

}
if(userInput==‘4’) // 2 to set the LED ON with pwm
{
digitalWrite(m1, HIGH);
analogWrite(pin, 200);

}
if(userInput==‘0’) // 0 to set the LED Off with pwm
{
analogWrite(pin1, pulsewidth2);
analogWrite(pin, pulsewidth2);
}
}

/Serial.flush();/
byte input[9];
memset(input, ‘\0’, 9);
byte startByte = ‘\0’;
while(startByte != ‘!’){

startByte = Serial.read();

}

if(startByte == ‘!’){
// Fetch the next 3 characters

while(Serial.available() < 9){

;

}
for( int i = 0; i < 3; i++){
input = Serial.read();

  • }*

  • speed_case = 1;*

  • for(int j=0; j<3 ;j++){*

  • int value = 0;*

  • for(int i = 0; i<3; i++){*
    _ incomingByte = input[(3*j)+i];_

  • if((incomingByte >= ‘0’) && (incomingByte <=‘9’)){*

  • // Parse the characters into an integer value*
    _ value = value * 10 + (incomingByte - 48);_

  • }*

  • }*

  • // Each time set_leds is called, the value of led_case is increased*

  • // by one. Hence, we iterate over all colors.*

  • set_speed(value);*

  • }*

  • }*

_ /Serial.flush();/_

}
void set_speed(int value){

  • if((value >= 0) && (value <= 255))*
  • {*
  • switch(speed_case){*
  • case 1:*
  • analogWrite(RED,(value-255));*
  • speed_case++;*
  • break;*
    }
  • }*
  • }*
    what is the problem of that program?, because i always failure to get the result from arduino( the output is led for the experiment)
    here is the vb.net screenshot
    16-06-2011 14-17-58.jpg

i have been created the program in vb.net for controlling Dc motor
here is the program

No, that was the Arduino code.

  /*Serial.flush();*/

Don’t just comment this out. Get rid of it.

byte input[9];
  memset(input, '\0', 9);
  byte startByte = '\0';
  while(startByte != '!'){
    
    
    
    startByte = Serial.read();
  
    
    
    
  }

Get rid of the blank lines. They contribute nothing to your code. You are reading serial data until you find a !, when there may be nothing to read. Why?

for( int i = 0; i < 3; i++){
      input = Serial.read();
      
  
    }

There’s a reason that there is a separate button for posting code. Unless you use that, the forum software decides that you wanted to italicize the rest of the post. Was that your intent?

Anyway, reading 4 more bytes of possibly non-existent serial data isn’t a good idea.

Next, you read 3 characters, after waiting for there to be 9 characters, and then reference all 9 of them. How well is that working?

if((incomingByte >= '0') && (incomingByte <='9')){
          // Parse the characters into an integer value
          value = value * 10 + (incomingByte - 48);
        }

48? What the heck does 48 mean? You know that it is perfectly possible to subtract ‘0’ there, right? Makes the meaning a whole lot clearer.

    switch(speed_case){

    case 1:
      analogWrite(RED,(value-255));
      speed_case++;
      break;
}

Pretty inefficient to use a switch statement with one case.

what is the problem of that program?, because i always failure to get the result from arduino( the output is led for the experiment)
here is the vb.net screenshot
16-06-2011 14-17-58.jpg

Posting the picture would have been more useful than posting the name. Posting the actual code would have been even better.

first, thanks for the solution
sorry for the unclear information, the problem is i want to control dc motor via vb.net 2008 and here is the vb screen shot:
!(http://16-06-2011 14-17-58.jpg)

16-06-2011 14-17-58.jpg

here is the explanation
the vertical motor control arduino program :
int pin = 5;// LED connected to PWM pin 11
int pin1 = 6;
int m1 = 7;
int m2 = 4;

int pulsewidth2 = 0;
int pulsewidth3 = 50;

void setup()
{
Serial.begin(9600);

}

void loop()
{
if(Serial.available()>0){
int userInput=Serial.read();
if(userInput==‘1’) // 1 to set the LED ON with pwm
{
digitalWrite(m2, HIGH);
analogWrite(pin, 50);

}
if(userInput==‘4’)
{
digitalWrite(m1, HIGH);
analogWrite(pin, 200);

}
if(userInput==‘0’)
{
analogWrite(pin1, pulsewidth2);
analogWrite(pin, pulsewidth2);
}
}
}
the horizontal motor control arduino program ( on vb.net i use trackbar to change the value of pwm) :
#define RED 11
int speed_case;
byte incomingByte;
void set_speed(int value);

void setup(){
Serial.begin(9600);
speed_case = 1;

}

void loop(){

serial.flush();
byte input[9];
memset(input, ‘\0’, 9);
byte startByte = ‘\0’;
while(startByte != ‘!’){

startByte = Serial.read();

}

if(startByte == ‘!’){
// Fetch the next 3 characters

while(Serial.available() < 9){
;
}
for( int i = 0; i < 3; i++){
input = Serial.read();

  • }*

  • speed_case = 1;*

  • for(int j=0; j<3 ;j++){*

  • int value = 0;*

  • for(int i = 0; i<3; i++){*
    _ incomingByte = input[(3*j)+i];_

  • if((incomingByte >= ‘0’) && (incomingByte <=‘9’)){*

  • // Parse the characters into an integer value*
    _ value = value * 10 + (incomingByte - 48);_

  • }*

  • }*

  • // Each time set_leds is called, the value of led_case is increased*

  • // by one. Hence, we iterate over all colors.*

  • set_speed(value);*

  • }*

  • }*

  • Serial.flush();*

}
void set_speed(int value){

  • if((value >= 0) && (value <= 255))*
  • {*
  • switch(speed_case){*
  • case 1:*
  • analogWrite(RED,(value-255));*
  • speed_case++;*
  • break;*
    }
  • }*
  • }*
    sorry for my bad program, because i am newbie in arduino, if i have mistake, please correct it.(the program actually is not my program, i combine and modify it , the source is from internet, sorry i am forgot where i got the source…

There's a reason that there is a separate button for posting code. Unless you use that, the forum software decides that you wanted to italicize the rest of the post. Was that your intent?

Find a club and whack yourself over the head a couple of times. Then, go back and modify your posts. Select all the code, and use the # button on the second row. Save your changes. The forum software interprets stuff like i to be an "italicize the text, until I tell you to stop" instruction, where you mean something completely different. At least, I hope so.

Still haven't seen any VB code.

PaulS, why so rude?

I don't think I was being rude. OP posted code incorrectly in the first post. I asked for the VB code to be posted, and the first post corrected. OP ignored both requests and posted the same code again. There is a limit to my patience with people who ignore requests for information and to follow the accepted standards of behavior, while still demanding help.

thanks for your information Paul, i am very sorry give you terrible information, now i have been corrected the arduino and vb.net program, and the system is now work well.
here is the vb.net code:
and here is the arduino code

vb.net code.txt (2.71 KB)

Arduino code.txt (1.25 KB)