Go Down

Topic: stepper motor arduino vb2010 (Read 2246 times) previous topic - next topic

wentaw


Hi all,
Can someone help me with this, i have to create a program that the user will choose either short or long  then enter a value in the textbox and the stepper will move depends on what the user chose. I dont know how to do it, but i have to  :smiley-confuse:




VB 2010 code


Code: [Select]

Imports System.IO
Imports System.IO.Ports
Imports System.Threading

Public Class Form1
    Shared _continue As Boolean
    Shared _serialPort As SerialPort

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

        SerialPort1.Close()
        SerialPort1.PortName = "Com5"
        SerialPort1.BaudRate = 9600
        SerialPort1.DataBits = 8
        SerialPort1.Parity = Parity.None
        SerialPort1.StopBits = StopBits.One
        SerialPort1.Handshake = Handshake.None
        SerialPort1.Encoding = System.Text.Encoding.Default

        ComboBox1.Items.Add("Long")
        ComboBox1.Items.Add("Short")

    End Sub

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



        If ComboBox1.SelectedItem = "Long"  Then

            SerialPort1.Open()
            SerialPort1.Write(Textbox1.Text)
            SerialPort1.Close()

        ElseIf ComboBox1.SelectedItem = "Short" Then

            SerialPort1.Open()
            SerialPort1.Write(TextBox1.Text)
            SerialPort1.Close()
        End If

    End Sub

End Class



Arduino code

Code: [Select]
#include <Stepper.h>
#define STEPS 70
Stepper stepper1(STEPS, 8, 9, 10, 11);
Stepper Stepss(STEPS, 2, 3, 4, 5);
 
void setup()
{
  Serial.begin(9600);
}


void loop()
{
 
 while (Serial.available() == 0);{
 
 }

  int val = Serial.read();
  Serial.println(val); 


  if (val==0)
  {

   stepper1.setSpeed(460);
  delay(50); //pause for effect
 stepper1.step(7000);
  delay(10); //pause for effect


  digitalWrite(8,LOW);
  digitalWrite(9,LOW);
  digitalWrite(10,LOW);
  digitalWrite(11, LOW);
  }


else if (val==1)

{
      Stepss.setSpeed(460);
  delay(50);
 Stepss.step(-7000);
  delay(10);

    digitalWrite(2,LOW);
  digitalWrite(3,LOW);
  digitalWrite(4,LOW);
  digitalWrite(5,LOW);
}
 
  }

Robin2

#1
Oct 21, 2017, 12:07 pm Last Edit: Oct 21, 2017, 12:07 pm by Robin2
For receiving data in the Arduino have a look at the examples in Serial Input Basics - simple reliable ways to receive data. There is also a parse example to illustrate how to extract numbers from the received text.

I am no longer familiar with Windows so I can't help with the VB stuff.

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

acboother

What is the difference between the long and the short?

How is the Arduino going to know whether it has come from one or the other?


PaulS

Code: [Select]

            SerialPort1.Open()
            SerialPort1.Write(Textbox1.Text)
            SerialPort1.Close()

Opening and closing the serial port resets the Arduino, unless you have physically modified it not to reset. If you have, I think that you should have mentioned that fact.
The art of getting good answers lies in asking good questions.

wentaw

What is the difference between the long and the short?

How is the Arduino going to know whether it has come from one or the other?


That is actually my problem sir. Using a combo box with short and long choises, how can i program it in vbnet that can read in arduino?

ex. If I choose the short text in combo box, the arduino will will read the "short string" and only one stepper motor will move.

i cant explain it in english haha sorry but I hope you understand

PaulS

Quote
ex. If I choose the short text in combo box, the arduino will will read the "short string" and only one stepper motor will move.
The Arduino has NO idea what combo box entry you selected. The ONLY purpose of the combo box is to control what the VB app sends. That is the ONLY thing the Arduino cares about.
The art of getting good answers lies in asking good questions.

acboother

#6
Nov 29, 2017, 09:50 am Last Edit: Nov 29, 2017, 09:52 am by acboother
I don't think your problem is likely to be very difficult to solve but the "short" and "long" doesn't mean anything at the moment and that is the root of the problem (for us).

Why not write your requirement in your own language into BING translator and see if it does a good job and then post that here.


Try and answer the question why you need to send a short or a long and what difference you expect to happen on the Arduino with each. Perhaps a bit more information about what is physically attached to the Arduino would assist as well.

If you have been asked/told to do this perhaps you could send this information (translated) and we can see exactly where you are coming from and decide if you are going in the right direction.

By the waay you imply in your first post there is only one stepper but a subsequent post seems to imply there is more than one. Perhaps you could clarify.

wentaw

Try and answer the question why you need to send a short or a long and what difference you expect to happen on the Arduino with each. Perhaps a bit more information about what is physically attached to the Arduino would assist as well.

If you have been asked/told to do this perhaps you could send this information (translated) and we can see exactly where you are coming from and decide if you are going in the right direction.

By the waay you imply in your first post there is only one stepper but a subsequent post seems to imply there is more than one. Perhaps you could clarify.
So, we are making a printing machine sir where you can insert coins or bills to print your document. And that short and long thing there is bond paper.
So my part is when the user pick in combobox (either short or long bondpaper) and they click "print" button, only the selected paper and motor will run (there are 2 stepper motor for every paper rack, one for short and one for long, the stepper will run like a conveyor that transfer the selected paper from the rack to the printer to print the document of the user)

I hope you can imagine the flow sir, hahah Thank you


The Arduino has NO idea what combo box entry you selected. The ONLY purpose of the combo box is to control what the VB app sends. That is the ONLY thing the Arduino cares about.
So combobox is a wrong decision because the arduino cant read the string inside of it?

wentaw

arduino code

Code: [Select]
String j;
#include <Stepper.h>
#define rev 500
#define rev1 500
Stepper myStepper(rev, 8, 9, 10, 11);
Stepper myStepper1(rev1, 4, 5, 6, 7);
int mot, val;

void setup() {

  myStepper.setSpeed(60);
  myStepper1.setSpeed(60);
  Serial.begin(9600);

}

void loop()
{
  if (Serial.available() > 0 )
  {
    j = Serial.readString();
    val = Serial.parseInt();
    if (j == "SHORT")
    {
      while (val > 0)
      {
        Serial.println(val);
        myStepper.step(-455);
        delay(500);
        val--;
      }
    }

    else if (j == "LONG")
    {
      while (val> 0)
      {
        Serial.println(val);
        myStepper1.step(-5900);
        delay(500);
        val--;
      }
    }
  }

}



vbnet code

Code: [Select]
Imports System
Imports System.IO
Imports System.IO.Ports
Imports System.Threading


Public Class Form1
    Shared _continue As Boolean
    Shared _serialPort As SerialPort
    Delegate Sub SetTextCallback(ByVal [text] As String)

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Windows.Forms.Control.CheckForIllegalCrossThreadCalls = False

        SerialPort1.Close()
        SerialPort1.PortName = "Com11"
        SerialPort1.BaudRate = 9600
        SerialPort1.DataBits = 8
        SerialPort1.Parity = Parity.None
        SerialPort1.StopBits = StopBits.One
        SerialPort1.Handshake = Handshake.None
        SerialPort1.Encoding = System.Text.Encoding.Default
       
        ComboBox1.Items.Add("LONG")
        ComboBox1.Items.Add("SHORT")

    End Sub

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

        Dim LONGx As String
        LONGx = "LONG"
        Dim SHORTx As String
        SHORTx = "SHORT"

        If ComboBox1.SelectedItem = "LONG" Then

            SerialPort1.Open()
            SerialPort1.Write(TextBox1.Text)
            SerialPort1.Close()

        ElseIf ComboBox1.SelectedItem = "SHORT" Then
            SerialPort1.Open()
            SerialPort1.Write(TextBox1.Text)
            SerialPort1.Close()

        End If
    End Sub
End Class

Robin2

#9
Dec 10, 2017, 09:57 am Last Edit: Dec 10, 2017, 09:58 am by Robin2
So combobox is a wrong decision because the arduino cant read the string inside of it?
I think you are confusing the activity within your PC program (where the combobox exists) and the activity within your Arduino program.

All the Arduino program will know about is the data that it receives over its serial connection  with the PC. It will have no idea whether that data was created after something happened with a combobox or anything else.

If you want the Arduino to do two different things then you need to arrange for your PC program to send it two different messages. For example if you want the Arduino to move a long piece of paper your PC program could send <L> and if you want it to move a short piece of paper it could send <S>. (I am assuming you have studied the link in Reply #1)

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

horace

why are you opening and closing the serial port when the ComboBox is selected? open it when the Form is initialised and leave it open
also if your ComboBox contains LONG and SHORT you could simplify the event handler too
Code: [Select]
Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles Combo    Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged
        SerialPort1.WriteLine(ComboBox1.SelectedItem)
        SerialPort1.WriteLine(TextBox1.Text)
    End Sub



Robin2

open it when the Form is initialised and leave it open
The PC program should also allow time for the Arduino to complete its reset process before trying to send data. In my Arduino-PC projects I have Serial.println("Arduino is ready"); in setup() and my PC program waits until it receives that  before trying to communicate with the Arduino.

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

Go Up