Hi all! I've been bending my mind backwards trying to figure out what's going on. I'm VERY new to coding (started with a crash course ~2 weeks ago), so please bear with me and ask for additional information where necessary.
I'm trying to use my Arduino uno R3 + Ethernet shield R2 (with Wiznet w5500 chip) as a server to toggle a motor's direction and speed remotely.
When I tested virtually the same code with a few changes using a simple LED project it worked perfectly fine and I was able to have the network assign my Arduino an IP address without issue. I altered the lower if statements to be relevant for a DC motor project while leaving the server code unchanged, and suddenly my Arduino will only ever be assigned an IP of 0.0.0.0... What's worse is the previously working LED code now also has the same issue. I tried manually assigning the IP address using ethernet.begin(mac,ip,myDns,subnet) and manually entering each of these with no success.
Could anyone please take a look and let me know if there's something silly I'm overlooking? (Code provided below)
---------------------------------Start code------------------------------------
#include <SPI.h>
#include <Ethernet.h>
int mSpeedOut=3;
int d1=4;
int d2=5;
int mSpeed=0;
int dt=150;
String readString;
byte mac[] = { 0xA8, 0x61, 0x0A, 0xAE, 0x75, 0xA1 }; //The mac-address of the arduino Ethernet shield Rev. 2
EthernetServer server(80); //80 is a frequently used server no. for LAN connections.
void setup() {
Serial.begin(9600);
pinMode(mSpeedOut,OUTPUT);
pinMode(d1,OUTPUT);
pinMode(d2,OUTPUT);
Ethernet.begin(mac);
server.begin();
Serial.print("Please connect to the following IP address to continue: ");
Serial.println(Ethernet.localIP()); //This will fetch the designated IP of the ethernet shield
}
void loop() {
EthernetClient client = server.available();
if (client) {
while (client.connected()) {
if (client.available()) {
char c = client.read();
Serial.print(c);
if (readString.length() < 100) {
readString += c;
}
//This package cointains the HTML code used to create the server page
if (c == '\n'){
Serial.print(readString);
client.println("<HTTP/1.1 200 OK>");
client.println("<Connection-Type: text/html>");
client.println("<Connection: close>");
client.println("");
client.println("<!DOCTYPE html>");
client.println("<html>");
client.println("<head>");
client.println("<title>Arduino Webserver</title>");
client.println("</head>");
client.println("<body>");
client.println("<a href=\"/?DecSpeed\"\"><button>Speed -5%</button></a>");
client.println("<a href=\"/?Neutral\"\"><button>Reset</button></a>");
client.println("<a href=\"/?IncSpeed\"\"><button>Speed +5%</button></a>");
client.println("<body style=background-color:powderblue>");
delay(1);
client.stop();
if (readString.indexOf("?Neutral")>0) {
mSpeed=0;
}
if (readString.indexOf("?DecSpeed")>0) {
mSpeed=mSpeed-13;
}
if (readString.indexOf("?IncSpeed")>0) {
mSpeed=mSpeed+13;
}
if(mSpeed<0){
digitalWrite(d1,HIGH);
digitalWrite(d2,LOW);
analogWrite(mSpeedOut,-mSpeed);
}
if(mSpeed==0){
analogWrite(mSpeedOut,0);
}
if(mSpeed>0){
digitalWrite(d2,HIGH);
digitalWrite(d1,LOW);
analogWrite(mSpeedOut,mSpeed);
}
readString = "";
}
}
}
}
}
-----------------------------------End code------------------------------------