Hello everybody,
I have an Arduino Uno R3 + WiFi shield + L293D Dual H-bridge. That's what I use to control and move a tank. It's working, but not really good: It takes a whole second to close the connection between the Arduino and my laptop, also it beeps when the motors move. Here's my code:
#include <SPI.h>
#include <WiFi.h>
char ssid[] = "MAHNETWORK"; // your network SSID (name)
char pass[] = MAHPASSWORD"; // your network password
char ssid2[]="Arduino Tank"; // My hotspot on my laptop to be able to control the tank everywhere
char pass2[]="wowmuchawesome"; // The password for that
WiFiServer server(3178);
int pin1=3; //LED for the transistor that controls the forward speed of the left motor
int pin2=5; //LED for the transistor that controls the backward speed of the left motor
int pin3=6; //Same like pin1 but for right
int pin4=9; //Same like pin2 but for right
int led=13;
int netz=0; // which network, 0-none, 1-homenetwork, 2-Arduino Tank (hotspot)
boolean go=false; // So it only starts to add chars when I already got a "G" because of "GET"
long time=0; // for timeout
int run=60001; // I don't want to check the connection all the time
int currenttime=0;
int duration=3000;
String relativex=""; //Left speed
String relativey=""; //Right speed (variable names stayed although I changed the way I control it, ignore)
String currentLine=""; //The current received line
int status = WL_IDLE_STATUS; // the Wifi radio's status
char floatbuf[8]; //For converting chars in flaot
float speedlinks=0.0; //speed left
float speedrechts=0.0; //speed right
void setup() {
Serial.begin(9600);
pinMode(pin1, OUTPUT);
pinMode(pin2, OUTPUT); // I just see that I forgot to add the other two pins, how the hell could the work?!
pinMode(pin3, OUTPUT);
pinMode(pin4, OUTPUT);
pinMode(led, OUTPUT);
}
void loop() {
if (WiFi.status()!=WL_CONNECTED) {
digitalWrite(led,LOW);
tryconnect();
} else {
digitalWrite(led,HIGH);
webserver();
}
if (millis()-currenttime>duration) {
currenttime=millis();
analogWrite(pin1,0);
analogWrite(pin2,0);
analogWrite(pin3,0);
analogWrite(pin4,0);
}
}
void tryconnect() { // works like a charm
Serial.println("Trying to connect.");
if (int num=WiFi.scanNetworks()>(-1)) {
for (int thisNet = 0; thisNet<num; thisNet++) {
if (WiFi.SSID(thisNet)=="NerdtechCorporation") {
netz=1;
}
else if (WiFi.SSID(thisNet)=="Arduino Tank") {
netz=2;
}
else {
netz=0;
}
}
if (WiFi.status()!=WL_CONNECTED) {
Serial.println("Attempting to connect to Arduino Tank...");
if (netz==2) {
status = WiFi.begin(ssid2, pass2);
}
if ( status != WL_CONNECTED) {
Serial.println("Couldn't connect to Arduino Tank.");
Serial.println("Attempting to connect to NerdtechCorporation...");
if (netz=1) {
status = WiFi.begin(ssid, pass);
}
if ( status != WL_CONNECTED) {
Serial.println("Couldn't connect to NerdtechCorporation.");
}
else {
Serial.println("Connected to NerdtechCorporation.");
server.begin();
printWifiStatus();
}
}
else {
Serial.println("Connected to Arduino Tank.");
server.begin();
printWifiStatus();
}
}
else {
Serial.println("Already connected");
}
}
}
void webserver() {
WiFiClient client = server.available();
if (client) {
time=millis(); //stores the beginning time
Serial.println("new client");
currentLine = "";
while (client.connected()) {
if (millis()-time>5000) { // if it took 5 seconds timeout so stop everything and go back to loop()
Serial.println("Timeout.");
client.flush();
client.stop();
return;
}
if (client.available()) {
char c = client.read(); // reads incoming stuff
Serial.print(c);
currentLine+=c;
if (c == '\n') {
currentLine = "";
Serial.println("Blanked current line");
}
if (currentLine.substring(0,5)=="GET /"&¤tLine.indexOf("HTTP")!=-1) { // if the beginning is "GET /" and it contains "HTTP", it also contains the speed of the motors
currentLine=currentLine.substring(5,currentLine.length()-1);
Serial.println(currentLine);
currentLine=currentLine.substring(0,currentLine.indexOf(" "));
Serial.println(currentLine);
relativex=(currentLine.substring(0,currentLine.indexOf("x")));
relativey=(currentLine.substring(currentLine.indexOf("x")+1,currentLine.length()));
Serial.println(relativex);
Serial.println(relativey);
relativex.toCharArray(floatbuf,sizeof(floatbuf));
speedlinks=atof(floatbuf)*255;
relativey.toCharArray(floatbuf,sizeof(floatbuf));
speedrechts=atof(floatbuf)*255;
Serial.println(speedlinks);
Serial.println(speedrechts);
if (speedrechts<0) { // if it's backwards, stop the forward transistor (or so) and start the backwards one
analogWrite(pin3,0);
analogWrite(pin4,speedrechts*(-1));
}
else { //else stop the backwards one and start the forwards one
analogWrite(pin4,0);
analogWrite(pin3,speedrechts);
}
if (speedlinks<0) { //same for left
analogWrite(pin1,0);
analogWrite(pin2,speedlinks*(-1));
}
else {
analogWrite(pin2,0);
analogWrite(pin1,speedlinks);
}
currenttime=millis();
Serial.println("Finished speed thing");
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println("Connection: close");
client.println();
client.println("Banana");
Serial.println("Sent response");
client.flush();
Serial.println(millis());
client.stop();
Serial.println(millis());
delay(10);
Serial.println("Delay over");
return;
}
} else {
Serial.println("Going to stop client manually");
client.stop();
Serial.println("client disconnected");
}
}
}
}
void printWifiStatus() {
// print the SSID of the network you're attached to:
Serial.print("SSID: ");
Serial.println(WiFi.SSID());
// print your WiFi shield's IP address:
IPAddress ip = WiFi.localIP();
Serial.print("IP Address: ");
Serial.println(ip);
// print the received signal strength:
long rssi = WiFi.RSSI();
Serial.print("signal strength (RSSI):");
Serial.print(rssi);
Serial.println(" dBm");
// print where to go in a browser:
Serial.print("To see this page in action, open a browser to http://");
Serial.println(ip);
}
I connected everything according to this video: - YouTube
Can someone help me with these problems?