Using encoder,visal basic GUI control motor

// motor A
int In1 = 13;
int In2 = 12;
int EnA = 11;
//encoder
int val;
int encoder0PinA = 2;
int encoder0PinB = 4;
int encoder0Pos = 0;
int encoder0PinALast = LOW;// define the first step of encoderPinLast
int n = LOW;
int p ;
int IN ;

void setup()
{
//motor
pinMode(In1, OUTPUT);
pinMode(In2, OUTPUT);
digitalWrite(In1, HIGH);
digitalWrite(In2, LOW);
pinMode (encoder0PinA,INPUT);
pinMode (encoder0PinB,INPUT);
Serial.begin (9600);
attachInterrupt(0, Encoder, CHANGE);
}

void loop()
{

IN = Serial.read();

if( IN == 'A')
{
stop1000();
}

// stop1000();
}

void stop1000(){

analogWrite(EnA, 255);
digitalWrite(In1,HIGH);
digitalWrite(In2,LOW);
if ( encoder0Pos == 1000)
{
digitalWrite(In1,LOW);
digitalWrite(In2,LOW);
}
}

void Encoder(){

n = digitalRead(encoder0PinA);//digitalRead() can return either HIGH or LOW

if ((encoder0PinALast == LOW) && (n == HIGH))
{
if (digitalRead(encoder0PinB) == LOW)
{ encoder0Pos--; }
else
{ encoder0Pos++; }
}

encoder0PinALast = n;

}

HELLO!! EVERYBODY I AM a new player of using arduino. i use VB and give out a GUI to control the motor. in VB, i press a button ,it will give out A and sent it to the arduino, when it receive it , encoder0Pos will count 1 cycle (encoder is 1000P/R) and then stop. if i don't use GUI, it can realy stop when motor rotate 1 cycle. But, if using GUI like

IN = Serial.read();

if( IN == 'A')
{
stop1000();
}
It just rotates forever ,Why this happening??? THX A LOT!!!!!!

if just use stop1000(); it works!!!!!!!!!!!

It also works when using GUI to call the motor stop or run by using
if( IN == 'B')
{
analogWrite(EnA, 255);
digitalWrite(In1,HIGH);
digitalWrite(In2,LOW);
}

if( IN == 'C')
{
digitalWrite(In1,LOW);
digitalWrite(In2,LOW);
}

quinton1987:
// motor A

int In1 = 13;

int In2 = 12;
int EnA = 11;
//encoder
int val;
int encoder0PinA = 2;
int encoder0PinB = 4;
int encoder0Pos = 0;
int encoder0PinALast = LOW;// define the first step of encoderPinLast
int n = LOW;
int p ;
int IN ;

void setup()
{
   //motor
    pinMode(In1, OUTPUT);
pinMode(In2, OUTPUT);
   digitalWrite(In1, HIGH);
   digitalWrite(In2, LOW);
   pinMode (encoder0PinA,INPUT);
   pinMode (encoder0PinB,INPUT);
   Serial.begin (9600);
    attachInterrupt(0, Encoder, CHANGE);
}

void loop()
{
   
   IN = Serial.read();

if( IN == 'A') 
      {
          stop1000();
      }

// stop1000();
  }

void stop1000(){     
               
                analogWrite(EnA, 255); 
                    digitalWrite(In1,HIGH);
                    digitalWrite(In2,LOW);
               if ( encoder0Pos == 1000)
                {
                    digitalWrite(In1,LOW);
                    digitalWrite(In2,LOW);
                }
}

void Encoder(){

n = digitalRead(encoder0PinA);//digitalRead() can return either HIGH or LOW
   
                if ((encoder0PinALast == LOW) && (n == HIGH))
   {
           if (digitalRead(encoder0PinB) == LOW)
           { encoder0Pos--; }
           else
           { encoder0Pos++; }
}

encoder0PinALast = n;
   
             }




here IS THE VB CODE



Public Class Form1

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

SerialPort1.Write("A")
    End Sub

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        SerialPort1.Open()
    End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        SerialPort1.Write("B")
    End Sub

Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        SerialPort1.Write("C")
    End Sub

Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
        SerialPort1.Write("G")
    End Sub
End Class



HELLO!! EVERYBODY I AM a new player of using arduino. i use VB and give out a GUI to control the motor. in VB, i press a button ,it will give out A and sent it to the arduino, when it receive it , encoder0Pos will count 1 cycle (encoder is 1000P/R) and then stop. if i don't use GUI, it can realy stop when motor rotate 1 cycle. But, if using GUI like

IN = Serial.read();

if( IN == 'A') 
{
stop1000();
}
It just rotates forever ,Why this happening??? THX A LOT!!!!!!

if just use stop1000(); it works!!!!!!!!!!!

It also works when using GUI to call the motor stop or run by using
if( IN == 'B') 
{
analogWrite(EnA, 255); 
digitalWrite(In1,HIGH);
digitalWrite(In2,LOW);
}

if( IN == 'C') 
{ 
digitalWrite(In1,LOW);
digitalWrite(In2,LOW);
}

Where is the initialization of the SerialPort1 object? Does the baud rate match the Arduino? Are the stop and start bits and parity set correctly?

I find it hard to believe that code will ever stop the motor for several reasons.

  1. Your encoder0Pos needs to be declared volatile.

  2. You are checking your encoder0Pos value in your stop1000() function, but you only call that function 1 time when you receive an A character. You need to be checking the value repeatedly until your stop condition has been met.

  3. You're only stopping the motor when encoders0Pos exactly equals 1000. You can't be guaranteed that the code will perform the check when the encoder value is exactly 1000. If Encoder0Pos equals 1001, then you've missed your small window for stopping the motor. A better approach would be to stop the motor when Encoder0Pos is greater than 1000 (but of course, you still need to be repeatedly checking the value)

PaulS:
Where is the initialization of the SerialPort1 object? Does the baud rate match the Arduino? Are the stop and start bits and parity set correctly?

In Arduino that i used to control stop and start

   IN = Serial.read();
   
   switch(IN) {
      case 'A':
        stop1000();
       break;
      case 'B':
        reset();
        break;
      case 'C':
        stopstop();
        break;

The SerialPort1 i set to COM3, that match to arduino PORT
but how to know that their baud rate are match together??
and how to set the priority of stop and start???
THX A LOT!!!!!!!

jraskell:
I find it hard to believe that code will ever stop the motor for several reasons.

  1. Your encoder0Pos needs to be declared volatile.

  2. You are checking your encoder0Pos value in your stop1000() function, but you only call that function 1 time when you receive an A character. You need to be checking the value repeatedly until your stop condition has been met.

  3. You're only stopping the motor when encoders0Pos exactly equals 1000. You can't be guaranteed that the code will perform the check when the encoder value is exactly 1000. If Encoder0Pos equals 1001, then you've missed your small window for stopping the motor. A better approach would be to stop the motor when Encoder0Pos is greater than 1000 (but of course, you still need to be repeatedly checking the value)

THX A LOT!!!
for1: I HAVE SET encoder0Pos to be zero for the globe value, is it ok??

for 2: I want to know that are the following code just check one time???
case 'A':
stop1000();
break;

how to make it check several time????

But, if i just type stop1000(); in my program, It works!!!!!!
Also i also check it

if ( encoder0Pos >= 1000)//there is not possible to push it in the check low high if function
{
digitalWrite(In1,LOW);
digitalWrite(In2,LOW);
}

It can't work too....

For 3: i' ve done it in 2

anyone help me here?