EDIT:FINAL SOLUTION:
Seeing as the final solution was buried in comments, I'll put it here for future viewers.
My problem is that the signal I was using to stop an action included "r", also the command for stop (I thought I was being clever, combining my "left" command and "stop" command). The C++ (arduino) code read each caharacter individually, hence stopping the signal altogether, not just the one which I wanted.
Thankyou to dbasnett for poitning this out :)
Alrighty, hi all, this is my final question for this latest project of mine. The idea, simply, was to make a program to control an RC car through an arduino. It works!!! (with only a small bug).
Simply, I use the arrow keys or WASD to control the program. It sends a signal through a Serial Port to the arduino, which has a few functions in it for interpreting the signal. You have a total of 9 signal that can be sent; one for each direction, one to stop each directional input (e.g. left makes car point wheels left, stop-left points the wheels straight ahead).
The PROBLEM IS: When I go both FORWARD (or reverse) and LEFT (or right), if I change DIRECTION (left/right), it stops all together. I've spent most of the project trying to work this out, and I just can't see what Im doing wrong.
so, my code:
Imports System.IO.Ports
Imports System.IO
Imports System.Threading
Public Class Form1
Shared _continue As Boolean
Shared _serialPort As SerialPort
Shared lturn As Boolean
Shared rturn As Boolean
Shared keydelay As Integer = 0
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
SerialPort1.PortName = "com3" 'change com port to match your Arduino port
SerialPort1.BaudRate = 115200
SerialPort1.DataBits = 8
SerialPort1.Parity = Parity.None
SerialPort1.StopBits = StopBits.One
SerialPort1.Handshake = Handshake.None
SerialPort1.Encoding = System.Text.Encoding.Default 'very important!
End Sub
Private Sub Form1_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown
Dim bHandled As Boolean = False
If SerialPort1.IsOpen Then
Else
SerialPort1.Open()
End If
Select Case e.KeyCode
Case Keys.Right
pbBgML.BackColor = Color.Transparent
pbBgMR.BackColor = Color.Black
SerialPort1.Write("d")
e.Handled = True
Case Keys.Left
pbBgMR.BackColor = Color.Transparent
pbBgML.BackColor = Color.Black
SerialPort1.Write("a")
e.Handled = True
Case Keys.Up
pbBgBC.BackColor = Color.Transparent
pbBgTC.BackColor = Color.Black
SerialPort1.Write("w")
e.Handled = True
Case Keys.Down
pbBgTC.BackColor = Color.Transparent
pbBgBC.BackColor = Color.Black
SerialPort1.Write("s")
e.Handled = True
Case Keys.Space
SerialPort1.Write("r")
e.Handled = True
End Select
End Sub
Private Sub Form1_KeyUp(ByVal sender As Object, ByVal e As KeyEventArgs) Handles Me.KeyUp
Select Case e.KeyCode
Case Keys.Right
pbBgMR.BackColor = Color.Transparent
SerialPort1.Write("dr")
Case Keys.Left
pbBgML.BackColor = Color.Transparent
SerialPort1.Write("ar")
Case Keys.Up
pbBgTC.BackColor = Color.Transparent
SerialPort1.Write("wr")
Case Keys.Down
pbBgBC.BackColor = Color.Transparent
SerialPort1.Write("sr")
End Select
End Sub
End Class
Also, if you want it, the arduino code (in C++):
// Car Control v. 0.2
int reversePin = 9;
int forwardPin = 8;
int leftPin = 10;
int rightPin = 11;
byte byteRead;
int time;
void forward(int time){
digitalWrite(reversePin, HIGH);
Serial.println("This is forward...");
digitalWrite(forwardPin, LOW);
delay(time);
}
void reverse(int time){
digitalWrite(forwardPin, HIGH);
Serial.println("This is reverse...");
digitalWrite(reversePin, LOW);
delay(time);
}
void left(int time){
digitalWrite(rightPin, HIGH);
Serial.println("This is left...");
digitalWrite(leftPin, LOW);
delay(time);
}
void right(int time){
digitalWrite(leftPin, HIGH);
Serial.println("This is right...");
digitalWrite(rightPin, LOW);
delay(time);
}
void off(){
Serial.println("This is stop...");
digitalWrite(leftPin, HIGH);
digitalWrite(rightPin, HIGH);
digitalWrite(reversePin, HIGH);
digitalWrite(forwardPin, HIGH);
}
void setup() {
// initialize the digital pins as an output.
pinMode(rightPin, OUTPUT);
pinMode(leftPin, OUTPUT);
pinMode(forwardPin, OUTPUT);
pinMode(reversePin, OUTPUT);
Serial.begin(115200);
Serial.print("\n\nStart...\n");
}
void loop()
{
//Turn everything off...
if (Serial.available()) {
/* read the most recent byte */
byteRead = Serial.read();
switch(byteRead)
{
case 'w':
forward(5);
break;
case 'wr':
digitalWrite(forwardPin, HIGH);
break;
case 's':
reverse(5);
break;
case 'sr':
digitalWrite(reversePin, HIGH);
break;
case 'a':
left(5);
break;
case 'ar':
digitalWrite(leftPin, HIGH);
break;
case 'd':
right(5);
break;
case 'dr':
digitalWrite(rightPin, HIGH);
break;
case 'r':
off();
break;
}
}
}
Sorry for the huge blocks of text, but I simply have no idea where the issue is. Cheers guys.