HI, i have made a wireless “robot”. This works with VB.NET and Arduino. There is a router and ethernet shield on the robot for the connection.
My connection keeps failing, so it won’t even react or get stuck in a position.
Here is a printscreen of the Serial Monitor (dutch):
Any ideas? Here is the Arduino source code:
//*************************************************************************************************************************************
// Declarations
//*************************************************************************************************************************************
#include <SPI.h>
#include <Ethernet.h>
//*************************************************************************************************************************************
// Wired configuration parameters
//*************************************************************************************************************************************
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
unsigned char local_ip[] = {192,168,1,5}; // 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;
const int trigPin = 2;
const int echoPin = 3;
EthernetServer server(80);
String buffer = "";
void setup()
{
pinMode(M1,OUTPUT);
pinMode(M2,OUTPUT);
Ethernet.begin(mac, local_ip);
Serial.begin(9600);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
}
void loop()
{
char c;
long duration,cm;
Serial.println("Aan het wachten op een verbinding ...");
EthernetClient client = server.available();
if (client)
{
Serial.println("Verbonden met client");
boolean currentLineIsBlank = true;
while (client.connected())
{
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
cm = microsecondsToCentimeters(duration);
Serial.print("gemeten afstand");
Serial.print(cm);
Serial.println();
if (client.available())
{
c = client.read();
Serial.print("Commando:");
Serial.println(c);
switch (c) {
case 'v':
if (cm < 10) niet_rijden();
else
{
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;
}
}
else
{
Serial.println("Niet beschikbaar");
if( c = 'v' && cm < 10) niet_rijden();
}
}
}
long microsecondsToCentimeters(long microseconds)
{
// The speed of sound is 340 m/s or 29 microseconds per centimeter.
// The ping travels out and back, so to find the distance of the
// object we take half of the distance travelled.
return (microseconds / 29) / 2;
}
void niet_rijden()
{
analogWrite(E1, 0); //PWM Speed Control
analogWrite(E2, 0); //PWM Speed Control
}