Hi all, I am currently doing up a school project that requires wifi to control a robot.
The program will let the robot run forever:
When parallax ping senses > 25cm, it will move forward
Parallax ping <=25 &&>10, it will make a left turn
Parallax ping <=10, it will reverse.
The program is able to run without the wifi enhancement, when i input the wifi section, the wifi will disconnect and both motors will run forward indefinitely.
Can anyone help?
Thanks!
#define ENABLE1 A4
#define DIRB1 4
#define DIRA1 5
#define DIRB2 6
#define DIRA2 3
#include <SPI.h>
#include <WiFi.h>
const unsigned int BUZZER_PIN = 8;
const unsigned int PING_SENSOR_IO_PIN = 13;
char ssid[] = "iggylicious"; // your network SSID (name)
char pass[] = "ignatius496031"; // your network password
int keyIndex = 0; // your network key Index number (needed only for WEP)
int status = WL_IDLE_STATUS;
WiFiServer server(80);
void setup() {
Serial.begin(9600); // initialize serial communication
pinMode(ENABLE1,OUTPUT);//Motor
pinMode(DIRA1,OUTPUT);//Motor
pinMode(DIRB1,OUTPUT);//Motor
pinMode(DIRA2,OUTPUT);//Motor
pinMode(DIRB2,OUTPUT);//Motor
pinMode(A0, OUTPUT); // set the LED pin mode
pinMode(A1, OUTPUT); // set the LED pin mode
pinMode(A2, OUTPUT); // set the LED pin mode
pinMode(A3, OUTPUT); // set the LED pin mode
digitalWrite(A0, LOW); // set the LED on
digitalWrite(A1, LOW); // set the LED on
digitalWrite(A2, LOW); // set the LED on
digitalWrite(A3, LOW); // set the LED on
pinMode(BUZZER_PIN, OUTPUT);
digitalWrite(ENABLE1,HIGH);
// check for the presence of the shield:
if (WiFi.status() == WL_NO_SHIELD) {
Serial.println("WiFi shield not present");
while(true); // don't continue
}
// attempt to connect to Wifi network:
while ( status != WL_CONNECTED) {
Serial.print("Attempting to connect to Network named: ");
Serial.println(ssid); // print the network name (SSID);
// Connect to WPA/WPA2 network. Change this line if using open or WEP network:
status = WiFi.begin(ssid, pass);
// wait 10 seconds for connection:
delay(10000);
}
server.begin(); // start the web server on port 80
printWifiStatus(); // you're connected now, so print out the status
digitalWrite(A0, LOW);
digitalWrite(A1, LOW);
digitalWrite(A2, LOW); // set the LED on
digitalWrite(A3, LOW);
}
void loop() {
WiFiClient client = server.available(); // listen for incoming clients
if (client) {
// if you get a client,
Serial.println("new client"); // print a message out the serial port
String currentLine = ""; // make a String to hold incoming data from the client
while (client.connected()) { // loop while the client's connected
if (client.available()) { // if there's bytes to read from the client,
char c = client.read(); // read a byte, then
Serial.write(c); // print it out the serial monitor
if (c == '\n') { // if the byte is a newline character
// if the current line is blank, you got two newline characters in a row.
// that's the end of the client HTTP request, so send a response:
if (currentLine.length() == 0) {
// HTTP headers always start with a response code (e.g. HTTP/1.1 200 OK)
// and a content-type so the client knows what's coming, then a blank line:
client.println("HTTP/1.1 200 OK");
client.println("Content-type:text/html");
client.println();
// the content of the HTTP response follows the header:
client.print("Click <a href=\"/H\">here</a> to go run enhancement
");
// The HTTP response ends with another blank line:
client.println();
// break out of the while loop:
break;
}
else { // if you got a newline, then clear currentLine:
currentLine = "";
}
}
else if (c != '\r') { // if you got anything else but a carriage return character,
currentLine += c; // add it to the end of the currentLine
}
// Check to see if the client request was "GET /H" or "GET /L":
if (currentLine.endsWith("GET /H")) {
pinMode(PING_SENSOR_IO_PIN, OUTPUT);
digitalWrite(PING_SENSOR_IO_PIN, LOW);
delayMicroseconds(2);
digitalWrite(PING_SENSOR_IO_PIN, HIGH);
delayMicroseconds(5);
digitalWrite(PING_SENSOR_IO_PIN, LOW);
pinMode(PING_SENSOR_IO_PIN, INPUT);
const unsigned long duration = pulseIn(PING_SENSOR_IO_PIN, HIGH);
const unsigned threshold = (microseconds_to_cm(duration));
if (duration == 0) {
digitalWrite(A0,LOW);
digitalWrite(A1,LOW);
digitalWrite(A2,LOW);
digitalWrite(A3,LOW);
digitalWrite(DIRA1,HIGH);
digitalWrite(DIRA2,HIGH);
digitalWrite(DIRB1,LOW);
digitalWrite(DIRB2,LOW);
}
else if(threshold > 25){
digitalWrite(DIRA1,HIGH);
digitalWrite(DIRA2,HIGH);
digitalWrite(DIRB1,LOW);
digitalWrite(DIRB2,LOW);
blink1();
//sonar1();
}//go forward if more than 25cm dist
else if (threshold <= 25 && threshold > 10){
digitalWrite(DIRA1,HIGH);
digitalWrite(DIRB2,HIGH);
digitalWrite(A3, HIGH);
digitalWrite(A1,LOW);
digitalWrite(A2,LOW);
digitalWrite(A0,LOW);
digitalWrite(DIRB1,LOW);
digitalWrite(DIRA2,LOW);
blink2();
//sonar2();
}//Between 25cm and 10cm, turn left
else if (threshold <= 10){
digitalWrite(DIRB1,HIGH);
digitalWrite(DIRB2,HIGH);
digitalWrite(DIRA1,LOW);
digitalWrite(DIRA2,LOW);
digitalWrite(A3, HIGH);
digitalWrite(A1,LOW);
digitalWrite(A2,LOW);
digitalWrite(A0,LOW);
//sonar3();
}//Less than 10cm reverse
delay(100);
}
}
}
// close the connection:
client.stop();
Serial.println("client disonnected");
}
}
unsigned long microseconds_to_cm(const unsigned long microseconds) {
return microseconds / 29 / 2;
}
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);
}
void blink1() {
digitalWrite(A0,LOW);
digitalWrite(A1,LOW);
digitalWrite(A2,LOW);
digitalWrite(A3,LOW);
digitalWrite(A1, HIGH);
delay(200);
digitalWrite(A1, LOW);
delay(200);
}
void blink2() {
digitalWrite(A0,LOW);
digitalWrite(A1,LOW);
digitalWrite(A2,LOW);
digitalWrite(A3,LOW);
digitalWrite(A2, HIGH);
delay(50);
digitalWrite(A2, LOW);
delay(50);
}
void sonar1() {
// play a note on pin 8 for 200 ms:
tone(8, 1047, 200);
delay(400);
}
void sonar2() {
// play a note on pin 8 for 200 ms:
tone(8, 1047, 200);
delay(200);
}
void sonar3() {
// play a note on pin 8 for 200 ms:
tone(8, 1047, 200);
delay(50);
}