Lighting LEDs up via Ethernet shield

Hello, :slight_smile:
I am working on a project to control a tower light via Ethernet shield working as a server.
I do not understand why the LEDs light up before I send the command, I want them to be off before press ON and after I press OFF on my browser.
Any help would be greatly appreciated.

Here is the code that I have so far:

/*
Arduino with Ethernet Shield

*/

#include <SPI.h>
#include <Ethernet.h>
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; //physical mac address
byte ip[] = { 192, 168, 0, 178 }; // ip in lan (that's what you need to use in your browser. ("192.168.0.178")
byte gateway[] = { 192, 168, 0, 1 }; // internet access via router
byte subnet[] = { 255, 255, 255, 0 }; //subnet mask
EthernetServer server(80); //server port
String readString;

int led;

void setup() {
// Open serial communications and wait for port to open:
Serial.begin(9600);

pinMode(5, OUTPUT);
pinMode(6, OUTPUT);
pinMode(7, OUTPUT);

Ethernet.begin(mac);
server.begin();
Serial.print("server is at ");
Serial.println(Ethernet.localIP());
}

void loop() {
// Create a client connection
EthernetClient client = server.available();
if (client) {
while (client.connected()) {
if (client.available()) {
char c = client.read();

//read char by char HTTP request
if (readString.length() < 100) {
//store characters to string
readString += c;
//Serial.print(c);
}

//if HTTP request has ended
if (c == '\n') {
Serial.println(readString); //print to serial monitor for debuging

client.println("HTTP/1.1 200 OK"); //send new page
client.println("Content-Type: text/html");
client.println();
client.println("");
client.println("");
client.println("Control Colonne Lumineuse");
client.println("");
client.println("");
client.println("

Control Colonne Lumineuse

");
client.println("
");
client.println("<a href="/?1ON"">LED1ON");
client.println("<a href="/?1OFF"">LED1OFF");
client.println("<a href="/?2ON"">LED2ON");
client.println("<a href="/?2OFF"">LED2OFF");
client.println("<a href="/?3ON"">LED3ON");
client.println("<a href="/?3OFF"">LED3OFF");
client.println("
");
client.println("");
client.println("");

delay(1);
//stopping client
client.stop();
//controls the Arduino if you press the buttons

if (readString.indexOf("?1ON") > 0) {
led = 3;
digitalWrite(led, HIGH);
return;
}
if (readString.indexOf("?1OFF") > 0) {
led = 3;
digitalWrite(led, LOW);
return;
}

if (readString.indexOf("?2ON") > 0) {
led = 4;
digitalWrite(led, HIGH);
return;
}
if (readString.indexOf("?2OFF") > 0) {
led = 4;
digitalWrite(led, LOW);
return;
}

if (readString.indexOf("?3ON") > 0) {
led = 5;
digitalWrite(led, HIGH);
return;
}
if (readString.indexOf("?3OFF") > 0) {
led = 5;
digitalWrite(led, LOW);
return;
}
readString = "";
}

}
}
}
}

TowerLightcontrol.ino (3.34 KB)

Run this:

void setup() {
  pinMode(5, OUTPUT);
  pinMode(6, OUTPUT);
  pinMode(7, OUTPUT);
}
void loop() {}

Are the LEDs on or off?

why don't you ifdef 0 out the code inside your loop() to prevent it from running to make sure your initialization is doing what you expect

aarg:
Run this:

void setup() {

pinMode(5, OUTPUT);
  pinMode(6, OUTPUT);
  pinMode(7, OUTPUT);
}
void loop() {}




Are the LEDs on or off?

It turn off for 2 sec and then it turn on again

Tower_Dh:
It turn off for 2 sec and then it turn on again

Then the logic for your LED's is probably backwards due to the wiring - LEDs are probably connected with the anode to positive supply. That turns them ON when the output is LOW.

gcjr:
why don't you ifdef 0 out the code inside your loop() to prevent it from running to make sure your initialization is doing what you expect

what do you mean by "ifdef 0"

using a #ifdef is an easy way to conditionally add or select code. see the link i posted

instead of removing the code inside your loop() you could

void loop() {
#ifdef 0
     .... code you want to temporarily not use
#endif
}

you can also have #else and #elif 1

aarg:
Then the logic for your LED's is probably backwards due to the wiring - LEDs are probably connected with the anode to positive supply. That turns them ON when the output is LOW.

I have tried with the code below and it works.
Thanks a lot.

Arduino with Ethernet Shield

*/

#include <SPI.h>
#include <Ethernet.h>
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; //physical mac address
byte ip[] = { 192, 168, 0, 178 }; // ip in lan (that's what you need to use in your browser. ("192.168.0.178")
byte gateway[] = { 192, 168, 0, 1 }; // internet access via router
byte subnet[] = { 255, 255, 255, 0 }; //subnet mask
EthernetServer server(80); //server port
String readString;

int led;

void setup() {
// Open serial communications and wait for port to open:
Serial.begin(9600);

pinMode(5, OUTPUT);
pinMode(6, OUTPUT);
pinMode(7, OUTPUT);

digitalWrite(5, HIGH);
digitalWrite(6, HIGH);
digitalWrite(7, HIGH);

Ethernet.begin(mac);
server.begin();
Serial.print("server is at ");
Serial.println(Ethernet.localIP());
}

void loop() {
// Create a client connection
EthernetClient client = server.available();
if (client) {
while (client.connected()) {
if (client.available()) {
char c = client.read();

//read char by char HTTP request
if (readString.length() < 100) {
//store characters to string
readString += c;
//Serial.print(c);
}

//if HTTP request has ended
if (c == '\n') {
Serial.println(readString); //print to serial monitor for debuging

client.println("HTTP/1.1 200 OK"); //send new page
client.println("Content-Type: text/html");
client.println();
client.println("");
client.println("");

client.println("Control Colonne Lumineuse");
client.println("");
client.println("");
client.println("

Control Colonne Lumineuse

");
client.println("
");
client.println("<a href="/?1ON"">LED1ON");
client.println("<a href="/?1OFF"">LED1OFF");
client.println("
");
client.println("<a href="/?2ON"">LED2ON");
client.println("<a href="/?2OFF"">LED2OFF");
client.println("
");
client.println("<a href="/?3ON"">LED3ON");
client.println("<a href="/?3OFF"">LED3OFF");

client.println("
");
client.println("");
client.println("");

delay(1);
//stopping client
client.stop();
//controls the Arduino if you press the buttons

if (readString.indexOf("?1ON") > 0) {
digitalWrite(5, LOW);
}
if (readString.indexOf("?1OFF") > 0) {
led = 3;
digitalWrite(5, HIGH);
}

if (readString.indexOf("?2ON") > 0) {
digitalWrite(6, LOW);
}
if (readString.indexOf("?2OFF") > 0) {
led = 4;
digitalWrite(6, HIGH);
}

if (readString.indexOf("?3ON") > 0) {
led = 5;
digitalWrite(7, LOW);
}
if (readString.indexOf("?3OFF") > 0) {
led = 5;
digitalWrite(7, HIGH);
}

readString = "";
}

}
}
}
}