Paul,
Arduino code: #define DEBUG 0
#define WAIT_FOR_START 1
unsigned char incomingByte = 0;
unsigned long loop_count = 0;
unsigned char horn = 32;
unsigned char redLED = 64;
unsigned char greenLED = 128;
unsigned char forward = 1;
unsigned char backward = 2;
unsigned char left = 4;
unsigned char right = 8;
unsigned char PORTB_val;
unsigned char PORTD_val;
unsigned char in_char = 0;
int ledPin = 13;
int sensor1 = 3;
int er1=0;
int x=0;
void setup()
{
//PORTD = digital IO 0-7
//horn, redLED, greenLED
pinMode(5, OUTPUT); // sets the digital pin as output
pinMode(6, OUTPUT); // sets the digital pin as output
pinMode(7, OUTPUT); // sets the digital pin as output
//PORTB = digital IO 8 - 13
//right, left, backwards, forward
pinMode(8, OUTPUT); // sets the digital pin as output
pinMode(9, OUTPUT); // sets the digital pin as output
pinMode(10, OUTPUT); // sets the digital pin as output
pinMode(11, OUTPUT); // sets the digital pin as output
pinMode(ledPin, OUTPUT);
Serial.begin(9600); // set up Serial library at 9600 bps
PORTD = redLED; // turn on the red LED
#if DEBUG
flash_led(3,500);
#endif
semi_start();
//wait_for_start(); //Waits for startup message from router serial port
//continues after receiving it.
}
void flash_led(unsigned int count, unsigned int rate)
{
// debug routine that flashes an LED
int n_count = 0;
while (n_count < count)
{
n_count++;
digitalWrite(13, HIGH); // sets the LED on
delay(rate); // waits for a bit
digitalWrite(13, LOW); // sets the LED off
delay(rate); // waits for a bit
}
}
char get_char()
{
//Function that waits for a character from the serial port
//If none are received, it returns 0.
//The timeout is so that if the router stops sending data to the microcontroller,
//the micrcontroller will stop driving the car, rather than just going forever with
//the last command. Timeout is around 250mS.
while (loop_count < 30000)
{
loop_count++;
if (Serial.available() > 0)
{
incomingByte = Serial.read();
loop_count = 0;
return incomingByte;
}
}
loop_count = 0;
#if DEBUG
Serial.print('X', BYTE);
#endif
return 0;
}
unsigned char wait_for_start()
{
//Waits for startup message from router serial port
#if WAIT_FOR_START
#if DEBUG
Serial.println("Waiting...");
#endif
while(1)
{
if (get_char() == 'j' && get_char() == 'b' && get_char() == 'p' && get_char() == 'r' && get_char() == 'o')
{
#if DEBUG
Serial.print("Passcode Accepted");
#endif
return 0;
}
}
#endif
}
void loop()
{
//Function that processes input from serial port and drives the car based
//on that input.
in_char = get_char();
er1= digitalRead(sensor1);
//Split byte received in to upper and lower halves.
PORTB_val = in_char & 0x0F;
PORTD_val = in_char & 0xF0;
//Make sure the greenLED is turned on now.
if ((PORTD_val & greenLED) == 0)
{
PORTD_val = PORTD_val + greenLED;
}
//The following IF statements are sanity checks to make sure that FORWARD and BACKWARD cannot be on at the same time
//and that LEFT and RIGHT can't be on at the same time.
if ((PORTB_val & (left + right)) == (left + right))
{
PORTB_val = PORTB_val - right;
}
if ((PORTB_val & (forward + backward)) == (forward + backward))
{
PORTB_val = PORTB_val - backward;
}
//Write the processed values to the ports.
PORTD = PORTD_val;
PORTB = PORTB_val;
//sensor test
if(er1 == HIGH)
{
digitalWrite(ledPin, LOW);
//this is where we want to put the code to be sent to VB
//is it Serial.print(x,BYTE)??
}
#if DEBUG
Serial.print(PORTD, HEX);
Serial.print(PORTB, HEX);
#endif
}
unsigned char semi_start()
{
digitalWrite(ledPin, HIGH); // set the LED on
delay(5000); // wait for a second
digitalWrite(ledPin, LOW); // set the LED off
delay(1000); // wait for a second
wait_for_start();
}
VB Code: Option Explicit
Public direction As String
Public ttime As Integer
Public address As String
Public upstatus, downstatus, leftstatus, rightstatus, hornstatus As Integer
Public output As Integer
Private Sub call_timer_Timer()
Call motion("manual", upstatus + downstatus + rightstatus + leftstatus + hornstatus)
'Calls motion module. Lets it know manual driving and what value to output to the
'parallel port
End Sub
Private Sub Command1_Click()
If sock.State = sckClosed Then ' if the socket is closed
sock.RemoteHost = lbladdress.Text ' set server adress
sock.RemotePort = "1500" ' set server port
Label5.Caption = "Connected"
sock.Connect ' start connection attempt
Else ' if the socket is open
sock.Close ' close it
Label5.Caption = "Not Connected"
End If
End Sub
Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
'Detects if control keys are pressed
If KeyCode = vbKeyEscape Then End 'Exits
If KeyCode = vbKeyT Then Call Command1_Click
If KeyCode = vbKeyUp Or KeyCode = vbKeyW Then: cmd_up.BackColor = &HFF0000: cmd_up.Picture = cmd_up.DownPicture: upstatus = 1
'If up arrow is pressed or W then change pictures (make it blue) and change upstatus.
If KeyCode = vbKeyDown Or KeyCode = vbKeyS Then cmd_down.BackColor = &HFF0000: cmd_down.Picture = cmd_down.DownPicture: downstatus = 2
'If down arrow is pressed or S then change pictures (make it blue) and change downstatus.
If KeyCode = vbKeyLeft Or KeyCode = vbKeyA Then cmd_left.BackColor = &HFF0000: cmd_left.Picture = cmd_left.DownPicture: rightstatus = 4
'If left arrow is pressed or A then change pictures (make it blue) and change rightstatus.
If KeyCode = vbKeyRight Or KeyCode = vbKeyD Then cmd_right.BackColor = &HFF0000: cmd_right.Picture = cmd_right.DownPicture: leftstatus = 8
'If right arrow is pressed or D then change pictures (make it blue) and change leftstatus.
If KeyCode = vbKeyH Then hornstatus = 32
End Sub
Private Sub Form_KeyUp(KeyCode As Integer, Shift As Integer)
'Stops output to that direction when key is lifted.
'Changes pictures back to unactivated (none-blue).
If KeyCode = vbKeyUp Or KeyCode = vbKeyW Then cmd_up.BackColor = &HFFFFFF: cmd_up.Picture = cmd_up.DisabledPicture: upstatus = 0
If KeyCode = vbKeyDown Or KeyCode = vbKeyS Then cmd_down.BackColor = &HFFFFFF: cmd_down.Picture = cmd_down.DisabledPicture: downstatus = 0
If KeyCode = vbKeyLeft Or KeyCode = vbKeyA Then cmd_left.BackColor = &HFFFFFF: cmd_left.Picture = cmd_left.DisabledPicture: rightstatus = 0
If KeyCode = vbKeyRight Or KeyCode = vbKeyD Then cmd_right.BackColor = &HFFFFFF: cmd_right.Picture = cmd_right.DisabledPicture: leftstatus = 0
If KeyCode = vbKeyH Then hornstatus = 0
End Sub
Private Sub Form_Load()
Dim line As String
'Stores {Arrow with white} pic in .DisabledPicture for each button
cmd_up.DisabledPicture = cmd_up.Picture
cmd_down.DisabledPicture = cmd_down.Picture
cmd_left.DisabledPicture = cmd_left.Picture
cmd_right.DisabledPicture = cmd_right.Picture
'Following Opens Config.txt and collect Parallel Port Address and Refresh Rate
Open CurDir & "\config.txt" For Input As #1
Line Input #1, line
Line Input #1, line
address = line 'Sets parallel port address
lbladdress.Text = address 'displays address
call_timer.Interval = 5
End Sub
Private Sub sock_Close()
sock.Close ' has to be
End Sub
Private Sub sock_Error(ByVal Number As Integer, Description As String, ByVal Scode As Long, ByVal Source As String, ByVal HelpFile As String, ByVal HelpContext As Long, CancelDisplay As Boolean)
MsgBox "Socket Error " & Number & ": " & Description
sock.Close ' close the erraneous connection
End Sub
'Motion Executor
Sub motion(direction As String, ttime As Integer)
address = Form1.address
Select Case direction
Case 1 To 10
Case "manual"
'If manual driving, parallel port output required for direction has already
'been calculated, so output value passed to ttime
output = ttime
Case "FF"
output = 1
Case "BB"
output = 2
Case "RR"
output = 8
Case "LL"
output = 4
Case "FR"
output = 9
Case "FL"
output = 5
Case "BR"
output = 10
Case "BL"
output = 6
Case "SS"
output = 0
End Select
Rem vbOut address, output
'sock.SendData output & Chr(0)
If sock.State = sckConnected Then
If output = &HC Or output = &H1C Then output = output - 8
If output = &H3 Or output = &H13 Then output = output - 2
sock.SendData Chr(output + 1) & Chr(0)
Label5.Caption = Chr(output + 1) & Chr(0)
Else
Label5.Caption = "NOT sending data"
Label2.Caption = output
End If
End Sub
Need: Sensor Feedback to VB application.
Setup: arduino to router to VB ( wirelessly)
Regards,