Hello,
So this is for my final work on school but I am a little bit stuck.
I control my arduino motorshield trough ethernet shield with VB.
When I only have forward it works, when I add backwards only backwards works
When i add rotate and everything, only the last added statement works, with the other ones it then just makes a silent clicking sound. I am really desperate here.
Arduino code:
//*************************************************************************************************************************************
// Declarations
//*************************************************************************************************************************************
#include <SPI.h>
#include <Ethernet.h>
//*************************************************************************************************************************************
// Wired configuration parameters
//*************************************************************************************************************************************
byte mac[] = { 0x90, 0xA2, 0xDA, 0x00, 0xD1, 0x4B };
unsigned char local_ip[] = {192,168,1,104}; // IP address of WiShield
unsigned char gateway_ip[] = {192,168,1,1}; // router or gateway IP address
unsigned char subnet_mask[] = {255,255,255,0}; // subnet mask for the local network
int E1 = 8;
int M1 = 9;
int E2 = 6;
int M2 = 7;
EthernetServer server(80);
//char buffer[20];
String buffer = "";
void setup()
{
pinMode(M1,OUTPUT);
pinMode(M2,OUTPUT);
Ethernet.begin(mac, local_ip);
Serial.begin(9600);
}
void loop()
{
EthernetClient client = server.available();
if (client)
{
boolean currentLineIsBlank = true;
while (client.connected())
{
if (client.available())
{
char c = client.read();
Serial.print(c);
buffer+=c;
int val;
if (c == 'O')
{
digitalWrite(M1,HIGH);
digitalWrite(M2, HIGH);
analogWrite(E1, 255); //PWM Speed Control
analogWrite(E2, 255); //PWM Speed Control
}
else if (c == 'F')
{
analogWrite(E1, 0); //PWM Speed Control
analogWrite(E2, 0); //PWM Speed Control
}
if (c == 'A')
{
digitalWrite(M1,LOW);
digitalWrite(M2, LOW);
analogWrite(E1, 255); //PWM Speed Control
analogWrite(E2, 255); //PWM Speed Control
}
else if(c == 'E')
{
analogWrite(E1, 0); //PWM Speed Control
analogWrite(E2, 0); //PWM Speed Control
}
if (c == 'R')
{
digitalWrite(M1,LOW);
digitalWrite(M2, HIGH);
analogWrite(E1, 255); //PWM Speed Control
analogWrite(E2, 255); //PWM Speed Control
}
else if (c == 'T')
{
analogWrite(E1, 0); //PWM Speed Control
analogWrite(E2, 0); //PWM Speed Control
}
if (c == 'L')
{
digitalWrite(M1,HIGH);
digitalWrite(M2, LOW);
analogWrite(E1, 255); //PWM Speed Control
analogWrite(E2, 255); //PWM Speed Control
}
else if(c == 'M')
{
analogWrite(E1, 0); //PWM Speed Control
analogWrite(E2, 0); //PWM Speed Control
}
}
}
}
}
VB code:
Imports System.Net.Sockets
Imports System.Text
Public Class Form1
Dim tcpClient As New System.Net.Sockets.TcpClient()
Dim networkStream As NetworkStream
Dim KeyPressed As Integer
Private Declare Function GetAsyncKeyState Lib "user32" (ByVal vkey As Integer) As Short
Private Function Arduino_Connect(ByVal IP As String, ByVal Port As Integer) As Boolean
tcpClient.Connect(IP, Port)
networkStream = tcpClient.GetStream()
If Not networkStream.CanWrite Or Not networkStream.CanRead Then
tcpClient.Close()
networkStream = Nothing
Return False
End If
Return True
End Function
Private Sub Arduino_Write(ByVal Output As String)
If Not IsNothing(networkStream) Then
Dim sendBytes As [Byte]() = Encoding.ASCII.GetBytes(Output)
Dim endByte As [Byte]() = {&HFE}
networkStream.Write(sendBytes, 0, sendBytes.Length)
networkStream.Write(endByte, 0, 1)
Else
MsgBox("ERROR")
End If
End Sub
Private Sub Form1_load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Arduino_Connect("192.168.1.104", 80)
Timer1.Start()
End Sub
Private Sub Arduino_Disconnect()
If Not IsNothing(networkStream) Then
tcpClient.Close()
networkStream = Nothing
End If
End Sub
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
If (GetAsyncKeyState(38)) Then
Arduino_Write("O")
Else
Arduino_Write("F")
End If
If (GetAsyncKeyState(40)) Then
Arduino_Write("A")
Else
Arduino_Write("E")
End If
If (GetAsyncKeyState(39)) Then
Arduino_Write("R")
Else
Arduino_Write("T")
End If
If (GetAsyncKeyState(37)) Then
Arduino_Write("L")
Else
Arduino_Write("M")
End If
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
End Sub
End Class