Go Down

Topic: Arduino + VB + ethernet shield, weird bug (Read 2363 times) previous topic - next topic

TheDigitalToaster

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:

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,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:

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
    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

mrcoder

its not a bug. you just did some weird coding. i suppose you got your code from somwhere on the web and then just added your own code ? the thing you are doing with the letters is all wrong !!

AWOL

#2
Nov 06, 2013, 09:31 pm Last Edit: Nov 06, 2013, 09:33 pm by AWOL Reason: 1
@mrcoder: Not helpful.

Code: [Select]
buffer+=c;
You don't seem to be using "buffer", so this is just gobbling memory.
Lose it.
I'd probably lose most if not all of the "else"s

AWOL

I'd be inclined to break the problem down a bit further, and use the following to make your testing easier.
Now, you just need a simple serial command handler to replace the ethernet code, and your motor handling is divorced from your packet handling.
Code: [Select]
void handleCommand (char command)
{
  switch (command)
  {
  case 'O':
    {
      digitalWrite(M1,HIGH); 
      digitalWrite(M2, HIGH);       
      analogWrite(E1, 255);   //PWM Speed Control
      analogWrite(E2, 255);   //PWM Speed Control
    }
    break;

  case 'F':
    {   
      analogWrite(E1, 0);   //PWM Speed Control
      analogWrite(E2, 0);   //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'E':
    {   
      analogWrite(E1, 0);   //PWM Speed Control
      analogWrite(E2, 0);   //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 'T':
    {   
      analogWrite(E1, 0);   //PWM Speed Control
      analogWrite(E2, 0);   //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 'M':
    {   
      analogWrite(E1, 0);   //PWM Speed Control
      analogWrite(E2, 0);   //PWM Speed Control
    }
    break;
  default:
  break;   
  }
}

zoomkat

Quote
I am really desperate here.


Perhaps you should dump the VB and try controlling the bot via a web page.
Google forum search: Use Google Search box in upper right side of this page.
Why I like my 2005 Rio Yellow Honda S2000  https://www.youtube.com/watch?v=pWjMvrkUqX0

TheDigitalToaster


Quote
I am really desperate here.


Perhaps you should dump the VB and try controlling the bot via a web page.

This is a must from my project teacher.

Go Up