//initialization
#include <ps2.h>
//mouse pin
PS2Mouse mouse(11, 12); //pin 11 as SCLK, pin 12 as SDIO
//pin initialization
//motor
int rmotorpin1=5;
int rmotorpin2=6;
int rena=4;
int lmotorpin1=9;
int lmotorpin2=10;
int lena=8;
//ultrasonic sensor
int pingKiri=2;
int pingKanan=3;
//LED
int LED=A5;
//variable initialization
//ultrasonic sensor
long durationKiri, durationKanan, cmKanan, cmKiri;
//mouse coordinates
int mouseX;
int mouseY;
//serial declaration to show serial.read
int serial;
char buff[24];
void setup()
{
//pin declaration
//motor
pinMode(rmotorpin1,OUTPUT);
pinMode(rmotorpin2,OUTPUT);
pinMode(rena,OUTPUT);
pinMode(lmotorpin1,OUTPUT);
pinMode(lmotorpin2,OUTPUT);
pinMode(lena,OUTPUT);
digitalWrite(rena, HIGH);
digitalWrite(lena, HIGH);
//LED
pinMode(LED, OUTPUT);
//mouse initialization
mouse.init();
//open com
Serial.begin(38400);
}
void loop()
{
ultrasonic();
motor();
printing();
}
//declaring function for motor movement
void motor()
{
//motor kanan &kiri maju
analogWrite(rmotorpin1, 200);
digitalWrite(rmotorpin2, LOW);
analogWrite(lmotorpin1, 200);
digitalWrite(lmotorpin2, LOW);
}
void ultrasonic()
{
// The ultrasonic sensor is triggered by a HIGH pulse of 2 or more microseconds.
// Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
//Kiri
pinMode(pingKiri, OUTPUT);
digitalWrite(pingKiri, LOW);
delayMicroseconds(2);
digitalWrite(pingKiri, HIGH);
delayMicroseconds(5);
digitalWrite(pingKiri, LOW);
// The same pin is used to read the signal from the ultrasonic sensor: a HIGH
// pulse whose duration is the time (in microseconds) from the sending
// of the ping to the reception of its echo off of an object.
pinMode(pingKiri, INPUT);
durationKiri = pulseIn(pingKiri, HIGH);
//doing the same routine for right sensor
//Kanan
pinMode(pingKanan, OUTPUT);
digitalWrite(pingKanan, LOW);
delayMicroseconds(2);
digitalWrite(pingKanan, HIGH);
delayMicroseconds(5);
digitalWrite(pingKanan, LOW);
pinMode(pingKanan, INPUT);
durationKanan = pulseIn(pingKanan, HIGH);
// convert the time into a distance
cmKiri = microsecondsToCentimeters(durationKiri);
cmKanan = microsecondsToCentimeters(durationKanan);
}
long microsecondsToCentimeters(long microseconds)
{
// The speed of sound is 340 m/s or 29 microseconds per centimeter.
// The ping travels out and back, so to find the distance of the
// object we take half of the distance travelled.
return microseconds / 29 / 2;
}
void printing()
{
//getting mouseinfo for printing subroutine.
MouseInfo mouseInfo;
mouse.getData(&mouseInfo);
//sprintf(buff, "%.3d %.3d %.3d %.3d", cmKanan, cmKanan, cmKanan, cmKanan); //printing with the same digit for all numbers
//Serial.println(buff); //print numbers
sprintf(buff, "%.3d", cmKanan);
Serial.print(buff);Serial.print(" ");
sprintf(buff, "%.3d", cmKiri);
Serial.print(buff);Serial.print(" ");
sprintf(buff, "%.3d", mouseInfo.cX, DEC);
Serial.print(buff);Serial.print(" ");
sprintf(buff, "%.3d", mouseInfo.cY, DEC);
Serial.print(buff);Serial.println();
delay(500);
}
i have this code inside the arduino and each of the serial.print is taken to VB. inside the VB data is processed into making dots based on the numbers.
these is the code inside the VB,
Imports System.IO.Ports
Imports System.Drawing
Imports System.Drawing.Drawing2D
Imports System.Windows.Forms
Imports System.Windows.Forms.Cursor
Public Class Form1
Dim RangeLeft As Integer
Dim RangeRight As Integer
Dim X As Integer
Dim Y As Integer
Dim LeftScreen As Integer
Dim RightScreen As Integer
Dim XScreen As Integer
Dim YScreen As Integer
Dim picture1 As Object
Dim Ico As New Icon("Vehicle.ico")
Dim Rect As New Rectangle(-100, -100, Ico.Width, Ico.Height)
Dim pen1 As New System.Drawing.Pen(Color.Black, 10)
Dim pen2 As New System.Drawing.Pen(Color.Red, 10)
Dim g As System.Drawing.Graphics
Dim PingPosLeft As Integer
Dim PingPosRight As Integer
Dim power As Integer
Dim j As Random
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Control.CheckForIllegalCrossThreadCalls = False
DoubleBuffered = True
With SerialPort1
'Set the serial port you want to use.
.PortName = "COM29"
'Set baud rate, parity, data bits, and stop bits.
.BaudRate = 38400
.Parity = Parity.None
.DataBits = 8
.StopBits = StopBits.One
'Set DTR and RTS
.DtrEnable = True
.RtsEnable = True
'Set number of bytes before DataRecieved event is raised.
.ReceivedBytesThreshold = 1
'Open serial port.
.Open()
End With
End Sub
Private Sub SerialPort1_DataReceived(ByVal sender As System.Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived
'reading data form serial
TextBox1.Text = SerialPort1.ReadLine()
RangeLeft = Mid(SerialPort1.ReadLine(), 1, 3)
RangeRight = Mid(SerialPort1.ReadLine(), 5, 3)
X = Mid(SerialPort1.ReadLine(), 9, 3)
Y = Mid(SerialPort1.ReadLine(), 13, 3)
'printing data to the form
LabelLeft.Text = RangeLeft
LabelRight.Text = RangeRight
LabelX.Text = X
LabelY.Text = Y
'make the data as variable to easily access them
XScreen = (X + 400)
YScreen = (Y + 300)
PingPosLeft = (XScreen - RangeLeft)
PingPosRight = (XScreen + RangeRight)
'create a 'moving-like' icon by substract the previous picture
Invalidate(Rect)
Rect.Y = YScreen
Rect.X = XScreen
'creating an instance of pen to draw the 'wall'
Dim pen2 As New System.Drawing.Pen(Color.Black, 10)
Dim g As System.Drawing.Graphics
g = PictureBox1.CreateGraphics
g.DrawArc(pen2, PingPosRight, YScreen, 5, 5, 180, 90)
g.DrawArc(pen2, PingPosLeft, YScreen, 5, 5, 180, 90)
Dim vehicle As New Icon("Vehicle.ico", 2, 2)
Dim d As System.Drawing.Graphics
Dim icon1 As New System.Drawing.Icon(vehicle, 1, 1)
d = PictureBox1.CreateGraphics
d.DrawIcon(Ico, Rect)
End Sub
the problem is, data sent and received is too slow, so that the dot's representation is not so thick..
the thing that i want to ask is, is there any way that i can make the data transfer between arduino and VB is fast enough so that every step or every data is count precisely?
thank you..