0% found this document useful (0 votes)
29 views

Arduino Program

This document contains code for an Arduino program and a Visual Basic program that communicate with each other via a serial port. The Arduino code defines variables and functions for controlling stepper motor pins, reading serial input, and moving the motor right or left. The Visual Basic code defines a form with buttons to send motor movement commands via the serial port to the Arduino. A timer is used to periodically send the motor position back from the Arduino to the Visual Basic program.

Uploaded by

Hennesey Lourice
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views

Arduino Program

This document contains code for an Arduino program and a Visual Basic program that communicate with each other via a serial port. The Arduino code defines variables and functions for controlling stepper motor pins, reading serial input, and moving the motor right or left. The Visual Basic code defines a form with buttons to send motor movement commands via the serial port to the Arduino. A timer is used to periodically send the motor position back from the Arduino to the Visual Basic program.

Uploaded by

Hennesey Lourice
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10

A.

ARDUINO PROGRAM

String y;
int x, a, b, c, e;
int z;
boolean f = false;
boolean g = true;
void setup() {
Serial.begin(9600);
pinMode (2, OUTPUT); //DIR
pinMode (3, OUTPUT); //STEP
pinMode (4, OUTPUT); //MS3
pinMode (5, OUTPUT); //MS2
pinMode (6, OUTPUT); //MS1
pinMode (7, OUTPUT); // ENABLE
pinMode(13, OUTPUT);
pinMode(11, INPUT);
pinMode(12, INPUT);
A:
if ( f == false) {
for (e = 0; e <= 650; e++) {
digitalWrite(7, LOW);
digitalWrite(6, HIGH);
digitalWrite(5, LOW);
digitalWrite(4, LOW);
digitalWrite(2, LOW);
digitalWrite(3, HIGH);
delayMicroseconds(500);
digitalWrite(3, LOW);
delayMicroseconds(500);
if ( digitalRead(12) == 0)
{
f = true;
digitalWrite(13, HIGH);
goto A;
}
}
for (e = 0; e <= 1300; e++) {
digitalWrite(7, LOW);
digitalWrite(6, HIGH);
digitalWrite(5, LOW);
digitalWrite(4, LOW);
digitalWrite(2, LOW);
digitalWrite(3, HIGH);
delayMicroseconds(500);
digitalWrite(3, LOW);
delayMicroseconds(500);
if ( digitalRead(12) == 0)
{
f = true;
digitalWrite(13, HIGH);
goto A;
}
}
for (e = 0; e <= 1950; e++) {
digitalWrite(7, LOW);
digitalWrite(6, HIGH);
digitalWrite(5, LOW);
digitalWrite(4, LOW);
digitalWrite(2, LOW);
digitalWrite(3, HIGH);
delayMicroseconds(500);
digitalWrite(3, LOW);
delayMicroseconds(500);
if ( digitalRead(12) == 0)
{
f = true;
digitalWrite(13, HIGH);
goto A;
}
}
for (e = 0; e <= 2600; e++) {
digitalWrite(7, LOW);
digitalWrite(6, HIGH);
digitalWrite(5, LOW);
digitalWrite(4, LOW);
digitalWrite(2, LOW);
digitalWrite(3, HIGH);
delayMicroseconds(500);
digitalWrite(3, LOW);
delayMicroseconds(500);
if ( digitalRead(12) == 0)
{
f = true;
digitalWrite(13, HIGH);
goto A;
}
}
}
}
void loop() {
if (Serial.available() > 0) {
y = Serial.readString();
delay(100);
a = y.toInt();
if (g == true) {
if (b < a) {
c = a - b;
right();
}
else if (b > a) {
c = b - a;
left();
}
b = a;
}
}
}
void right() {
g = false;
for (x = 1; x <= c; x++) {
digitalWrite(7, LOW);
digitalWrite(6, HIGH);
digitalWrite(5, LOW);
digitalWrite(4, LOW);
digitalWrite(2, HIGH);
digitalWrite(3, HIGH);
delayMicroseconds(500);
digitalWrite(3, LOW);
delayMicroseconds(500);
if (digitalRead(11) == 0) {
digitalWrite(7, LOW);
digitalWrite(6, HIGH);
digitalWrite(5, LOW);
digitalWrite(4, LOW);
digitalWrite(2, HIGH);
digitalWrite(3, HIGH);
delayMicroseconds(500);
digitalWrite(3, HIGH);
delayMicroseconds(500);
digitalWrite(13, LOW);
z = 5;
Serial.println(z);
g = true;
return;
}
}
z = 5;
Serial.println(z);
g = true;
return;
}
void left() {
g = false;
for (x = c; x >= 1; x--) {
digitalWrite(7, LOW);
digitalWrite(6, HIGH);
digitalWrite(5, LOW);
digitalWrite(4, LOW);
digitalWrite(2, LOW);
digitalWrite(3, HIGH);
delayMicroseconds(500);
digitalWrite(3, LOW);
delayMicroseconds(500);
if (digitalRead(12) == 0) {
digitalWrite(7, LOW);
digitalWrite(6, HIGH);
digitalWrite(5, LOW);
digitalWrite(4, LOW);
digitalWrite(2, HIGH);
digitalWrite(3, HIGH);
delayMicroseconds(500);
digitalWrite(3, HIGH);
delayMicroseconds(500);
digitalWrite(13, HIGH);
z = 5;
Serial.println(z);
g = true;
return;
}
}
z = 5;
Serial.println(z);
g = true;
return;
}
A. VISUAL BASIC PROGRAM

Public Class Form1 Timer1.Enabled = True


Dim x As String x=0
Dim y As String SerialPort1.Open()
Dim z As String = 0 SerialPort1.Write(x)
Private Sub Form1_Load(sender As SerialPort1.Close()
System.Object, e As System.EventArgs) 'Button2.Enabled = False
Handles MyBase.Load 'Button3.Enabled = False
Try 'Button4.enabled = False
SerialPort1.BaudRate = 9600 'Button5.Enabled = False
SerialPort1.DataBits = 8
SerialPort1.StopBits = Catch ex As Exception
IO.Ports.StopBits.One
SerialPort1.Parity = End Try
IO.Ports.Parity.None flat.Location = New Point(59, 35)
Dim ports As String() = End Sub
SerialPort1.GetPortNames()
Dim port As String Private Sub Button2_Click(sender As
For Each port In ports System.Object, e As System.EventArgs)
SerialPort1.PortName = port Handles Button2.Click
Label2.Text = port Try
Next port Timer1.Enabled = True
Catch ex As Exception x = 650
End Try SerialPort1.Open()
End Sub SerialPort1.Write(x)
SerialPort1.Close()
Private Sub Button1_Click(sender As 'Button1.Enabled = False
System.Object, e As System.EventArgs) 'Button3.Enabled = False
Handles Button1.Click 'Button4.Enabled = False
Try 'Button5.Enabled = False
Catch ex As Exception SerialPort1.Write(x)
End Try SerialPort1.Close()
flat.Location = New Point(230, 35) 'Button1.Enabled = False
'Button2.Enabled = False
End Sub 'Button3.Enabled = False
'Button5.Enabled = False
Private Sub Button3_Click(sender As Catch ex As Exception
System.Object, e As System.EventArgs) End Try
Handles Button3.Click flat.Location = New Point(587, 35)
Try End Sub
Timer1.Enabled = True
x = 1300 Private Sub Button5_Click(sender As
SerialPort1.Open() System.Object, e As System.EventArgs)
SerialPort1.Write(x) Handles Button5.Click
SerialPort1.Close() Try
'Button1.Enabled = False Timer1.Enabled = True
'Button2.Enabled = False x = 2600
'Button4.Enabled = False SerialPort1.Open()
'Button5.Enabled = False SerialPort1.Write(x)
Catch ex As Exception SerialPort1.Close()
End Try 'Button1.Enabled = False
flat.Location = New Point(409, 35) 'Button2.Enabled = False
End Sub 'Button3.Enabled = False
'Button4.Enabled = False
Private Sub Button4_Click(sender As Catch ex As Exception
System.Object, e As System.EventArgs) End Try
Handles Button4.Click flat.Location = New Point(772, 35)
Try End Sub
Timer1.Enabled = True
x = 1950
SerialPort1.Open()
Private Sub Timer1_Tick(sender As Timer1.Enabled = False
System.Object, e As System.EventArgs) 'Button1.Enabled = True
Handles Timer1.Tick 'Button2.Enabled = True
Dim incoming As String 'Button3.Enabled = True
Try 'Button4.Enabled = True
Label3.Text = x 'Button5.Enabled = True
incoming = End If
SerialPort1.ReadExisting() Catch ex As Exception
z = incoming End Try
Label4.Text = incoming End Sub
If incoming = 5 Then End Class

You might also like