Go Down

Topic: Arduino, ethernet shield, motorshield, hangs in position and stays there. (Read 1 time) previous topic - next topic

TheDigitalToaster

Hello everyone,

So I got an arudino with ethernet shield and motor shield. This is connected to my router and I control my motorshield over the wireless internet with VB. But sometimes when I use my arrow keys to control my motors the arduino just stays in that position. Like it just stays going forward and it doesn't stop untill I reset the arduino. Any sollutions for this?

Thanks in advance.

Arduino code:

Code: [Select]

//*************************************************************************************************************************************
// 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,102}; // 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);   
        switch (c) {
          case 'v':
                  digitalWrite(M1,HIGH); 
                  digitalWrite(M2, HIGH);       
                  analogWrite(E1, 255);   //PWM Speed Control
                  analogWrite(E2, 255);   //PWM Speed Control   
                  break;   
          case 'a':
                  digitalWrite(M1,LOW); 
                  digitalWrite(M2, LOW);       
                  analogWrite(E1, 255);   //PWM Speed Control
                  analogWrite(E2, 255);   //PWM Speed Control   
                  break;
          case 'r':
                  digitalWrite(M1,LOW); 
                  digitalWrite(M2, HIGH);       
                  analogWrite(E1, 255);   //PWM Speed Control
                  analogWrite(E2, 255);   //PWM Speed Control   
                  break;
          case 'l':
                  digitalWrite(M1,HIGH); 
                  digitalWrite(M2, LOW);       
                  analogWrite(E1, 255);   //PWM Speed Control
                  analogWrite(E2, 255);   //PWM Speed Control   
                  break;
          case 's':
                  analogWrite(E1, 0);   //PWM Speed Control
                  analogWrite(E2, 0);   //PWM Speed Control 
                  break;       
        }
      }
    }
  }
}



VB Code: (I think it crashes on the arduino side)
Code: [Select]

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
    Dim IP As Long
    Dim vooruit As Boolean = False
    Dim achteruit As Boolean = False
    Dim links As Boolean = False
    Dim rechts As Boolean = False
    Dim keyboard As New KeyBoardInput()
    Dim VLC As Process
    Dim er As Integer


    Public Sub New()

        ' This call is required by the designer.
        InitializeComponent()

    End Sub
    Private Sub StartVLC()
        Dim proc As New ProcessStartInfo()
        proc.FileName = My.Settings.Locatie
        proc.Arguments = "-vvv http://192.168.1.100:81/videostream.cgi?loginuse=admin&loginpas=admin " '--fullscreen"
        VLC = Process.Start(proc)
    End Sub
    Private Function Arduino_Connect(ByVal IP As String, ByVal Port As Integer) As Boolean
        Try
            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
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
        Return False
    End Function

    Private Sub Arduino_Write(ByVal Output As String)
        Try
            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)
                er = 0
            Else
                If er < 1 Then
                    System.Threading.Thread.Sleep(10000)
                Else
                    MsgBox("ERROR")
                    er = er + 1
                End If
            End If
        Catch ex As Exception
            Arduino_Disconnect() 'De arduino kant crasht en connectie valt weg :/

            Arduino_Connect("192.168.1.102", 80)
            Arduino_Write("s")
        End Try

    End Sub

    Private Sub Form1_load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        If My.Settings.Locatie = "" Then 'Als de string nog leeg is kennen we de locaatie van VLC niet en starten we form2
            Me.Hide()
            Form2.ShowDialog()
            Me.Show()
        End If
        Try
            Arduino_Connect("192.168.1.102", 80)
            StartVLC()
            keyboard.RegisterKey(Keys.Up)
            keyboard.RegisterKey(Keys.Down)
            keyboard.RegisterKey(Keys.Left)
            keyboard.RegisterKey(Keys.Right)
            AddHandler keyboard.KeyChanged, AddressOf KeyChanged
            keyboard.StartPolling()
            Timer1.Start()
        Catch ex As Exception
        End Try
    End Sub
    Private Sub Arduino_Disconnect()
        If Not IsNothing(networkStream) Then
            tcpClient.Close()
            networkStream = Nothing
        End If
    End Sub
    Private Sub KeyChanged(ByVal sender As Object, ByVal e As KeyEventArgs)
        If Not e.Pressed Then
            Arduino_Write("s")
        Else
            If e.Key.Equals(Keys.Up) Then
                Arduino_Write("v")
            ElseIf e.Key.Equals(Keys.Down) Then
                Arduino_Write("a")
            ElseIf e.Key.Equals(Keys.Left) Then
                Arduino_Write("l")
            ElseIf e.Key.Equals(Keys.Right) Then
                Arduino_Write("r")
            End If
        End If
    End Sub
    Private Sub ButtonDown(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.MouseDown, Button2.MouseDown, Button3.MouseDown, Button4.MouseDown
        If sender.Equals(Button1) Then
            Arduino_Write("v")
        End If
        If sender.Equals(Button2) Then
            Arduino_Write("l")
        End If
        If sender.Equals(Button3) Then
            Arduino_Write("a")
        End If
        If sender.Equals(Button4) Then
            Arduino_Write("r")
        End If
    End Sub
    Private Sub ButtonUp(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.MouseUp, Button2.MouseUp, Button3.MouseUp, Button4.MouseUp
        Arduino_Write("s")
    End Sub
    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        If VLC.HasExited Then
            Me.Close()
        End If
    End Sub

    Private Sub Panel1_Paint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PaintEventArgs)

    End Sub
End Class




Go Up