I guess the problem lies with the commands sent through the arduino. Since the arduino I'm using is interfaced with Visual Basic on a laptop.
I guess the arduino can't act fast enough.
Everytime I press a button, a button w/ a key shortcut "W" for example (for forward), a character will be sent, which will be received and read by the arduino. .
a part of the VB code (for the "W" only. the button display codes are not included and others):
Private Sub Main_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
If e.KeyCode = Keys.W Then
If sysActivated = True Then
forwardControl()
ElseIf sysActivated = False Then
Voice.Speak("The Serial Port is closed! Make sure that the port is open before operating. Thank You ", SpFlags)
MsgBox("The Serial Port is closed. . Make sure that the port is open before operating. Thank You ", MsgBoxStyle.Critical, "Error")
Private Sub forwardControl()
GroupBox2.Enabled = True
If RadioButton1.Checked = True Then
SerialPort1.Write("5")
ElseIf RadioButton2.Checked = True Then
SerialPort1.Write("6")
ElseIf RadioButton3.Checked = True Then
SerialPort1.Write("7")
Else
SerialPort1.Write("1")
End If
Private Sub Main_KeyUp(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyUp
If e.KeyCode = Keys.W Then
btnForwardOn.Visible = False
btnForwardOff.Visible = True
stopControl()
Private Sub stopControl()
SerialPort1.Write("0")
So in VB, while i'm pressing the "W" on keyboard, it will continually send a char (char because the code written in the Serial is enclosed in "", string form)("5" - for speed LOW; "6" - for Normal speed; and "7" for fast) through the Serial
and if the "W" key is released, a "0" code is sent for Stop command
In the arduino (not full):
under void loop(). .
while (Serial.available() == 0);
int val = Serial.read() - '0';
//for stop
if (val == 0)
{
digitalWrite(INAboard1, LOW);
digitalWrite(INBboard1, LOW);
digitalWrite(INAboard2, LOW);
digitalWrite(INBboard2, LOW);
}
// val 5 to 7, speed control of forward
else if (val == 5)
{
analogWrite(PWMboard1, 127);
analogWrite(PWMboard2, 127);
digitalWrite(INAboard1, HIGH);
digitalWrite(INBboard1, LOW);
digitalWrite(INAboard2, LOW);
digitalWrite(INBboard2, HIGH);
}
else if (val == 6)
{
analogWrite(PWMboard1, 191);
analogWrite(PWMboard2, 191);
digitalWrite(INAboard1, HIGH);
digitalWrite(INBboard1, LOW);
digitalWrite(INAboard1, LOW);
digitalWrite(INBboard2, HIGH);
}
else if (val == 7)
{
analogWrite(PWMboard1, 255);
analogWrite(PWMboard2, 255);
digitalWrite(INAboard1, HIGH);
digitalWrite(INBboard1, LOW);
digitalWrite(INAboard2, LOW);
digitalWrite(INBboard2, HIGH);
}
by the way. the pins used are all defined already.
The problem is, if while i'm pressing the W button in VB, and LOW speed radiobutton is checked, only 1 motor will turn,, same with Normal speed,
on the HIGH speed, both motors will run but not at the same time.