[SOLVED] Arduino with Ethernet Shield: Trying to have button click blink an LED.

Hello,

I am fairly new to Arduino programming. I am modifying some code I copied from www.randomnerdtutorials.com and am stumped on how to approach what I am trying to do.

The code creates a little HTML page and I have it port forwarded so I can reach it from other devices. The page has some buttons, two buttons turn different (red or green) LEDs ON and the other two buttons turn them OFF.

My goal:

I would like to have one button, when clicked, blink the red LED infinitely until the OFF button is clicked.

My problem:

I don’t know how to repeat the blink after the IF statement.

Thank you in advance for any help in this situation. I am taking C++ programming this upcoming term in college and hope to get a little more comfortable with Arduino programming :slight_smile:

The code I am using so far:

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

void setup() {
 // Open serial communications and wait for port to open:
  Serial.begin(9600);
   while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo only
  }
  pinMode(led, OUTPUT);
  pinMode(led1, OUTPUT);
  // start the Ethernet connection and the server:
  Ethernet.begin(mac, ip, gateway, subnet);
  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("<HTML>");
           client.println("<HEAD>");
           client.println("<meta name='apple-mobile-web-app-capable' content='yes' />");
           client.println("<meta name='apple-mobile-web-app-status-bar-style' content='black-translucent' />");
           client.println("<link rel='stylesheet' type='text/css' href='http://randomnerdtutorials.com/ethernetcss.css' />");
           client.println("<TITLE>Beyerlin Home Automation System</TITLE>");
           client.println("</HEAD>");
           client.println("<BODY>");
           client.print("<body style=background:gray>");
           client.println("<H1>Beyerlin Home Automation Service</H1>");
           client.println("<hr />");
           client.println("
");  
           client.println("<H2>User Interface Control Panel</H2>");
           client.println("
");  
           client.println("<a href=\"/?button1on\"\">GREEN LED ON</a>");
           client.println("<a href=\"/?button1off\"\">GREEN LED OFF</a>
");   
           client.println("
");     
           client.println("
"); 
           client.println("<a href=\"/?button2on\"\">RED LED ON</a>");
           client.println("<a href=\"/?button2off\"\">RED LED OFF</a>
");     
           client.println("<p>Email: reverbflange@gmail.com for tech support.</p>");  
           client.println("
"); 
           client.println("</BODY>");
           client.println("</HTML>");
     
           delay(1);
           //stopping client
           client.stop();
           //controls the Arduino if you press the buttons
           
           
           if (readString.indexOf("?button1on") >0)
           {
               digitalWrite(led, HIGH);
           }
           if (readString.indexOf("?button1off") >0)
           {
               digitalWrite(led, LOW);
           }

           if (readString.indexOf("?button2on") >0)
           {  
            digitalWrite(led1, HIGH);  
            delay(500);              
            digitalWrite(led1, LOW);    
            delay(500);  
           }
           
           if (readString.indexOf("?button2off") >0)
           {
               digitalWrite(led1, LOW);
           }
           
           readString="";
        }
      }
    }
  }
}

Greetings! No soy bueno con el ingles asi que espero que uses translate de google, jajaja. Alli esta el codigo probado, solo subelo a tu Arduino

#include <SPI.h>
#include <Ethernet.h>
int led = 4;
int led1 = 5;
 
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };   //physical mac address
byte ip[] = { 10,0,0,14 };                      // ip in lan (that's what you need to use in your browser. ("192.168.1.178")
byte gateway[] = { 10,0,0,1 };                   // internet access via router
byte subnet[] = { 255, 255, 255, 0 };                  //subnet mask
EthernetServer server(666);                             //server port     
String readString;
boolean red_blink = false;
unsigned long my_time;


void setup() {
 // Open serial communications and wait for port to open:
  Serial.begin(9600);
   while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo only
  }
  pinMode(led, OUTPUT);
  pinMode(led1, OUTPUT);
  // start the Ethernet connection and the server:
  Ethernet.begin(mac, ip, gateway, subnet);
  server.begin();
  Serial.print("server is at ");
  Serial.println(Ethernet.localIP());
}


void loop() {
  if (red_blink== true){
    if (millis()-my_time > 500){
      digitalWrite(led1,!digitalRead(led1));
      my_time= millis();
    }
  }
  else
  {
      digitalWrite(led1,LOW);
  }
  // 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("<HTML>");
           client.println("<HEAD>");
           client.println("<meta name='apple-mobile-web-app-capable' content='yes' />");
           client.println("<meta name='apple-mobile-web-app-status-bar-style' content='black-translucent' />");
           client.println("<link rel='stylesheet' type='text/css' href='http://randomnerdtutorials.com/ethernetcss.css' />");
           client.println("<TITLE>Beyerlin Home Automation System</TITLE>");
           client.println("</HEAD>");
           client.println("<BODY>");
           client.print("<body style=background:gray>");
           client.println("<H1>Beyerlin Home Automation Service</H1>");
           client.println("<hr />");
           client.println("
");  
           client.println("<H2>User Interface Control Panel</H2>");
           client.println("
");  
           client.println("<a href=\"/?button1on\"\">GREEN LED ON</a>");
           client.println("<a href=\"/?button1off\"\">GREEN LED OFF</a>
");   
           client.println("
");     
           client.println("
"); 
           client.println("<a href=\"/?button2on\"\">RED LED ON</a>");
           client.println("<a href=\"/?button2off\"\">RED LED OFF</a>
");     
           client.println("<p>Email: reverbflange@gmail.com for tech support.</p>");  
           client.println("
"); 
           client.println("</BODY>");
           client.println("</HTML>");
     
           delay(1);
           //stopping client
           client.stop();
           //controls the Arduino if you press the buttons
           
           
           if (readString.indexOf("?button1on") >0)
           {
               digitalWrite(led, HIGH);
           }
           if (readString.indexOf("?button1off") >0)
           {
               digitalWrite(led, LOW);
           }

           if (readString.indexOf("?button2on") >0)
           {  
              red_blink= true;
               my_time= millis();
           }
           
           if (readString.indexOf("?button2off") >0)
           {
               red_blink= false;
           }
           
           readString="";
        }
      }
    }
  }
}

Thank you very much for the reply, that worked perfect.

Now if I only understood why you did what you did :slight_smile:

It looks like you set up the blink early on in the code, then referred back to it if the button was clicked?

Either way, thank you again! And google translate is a life saver sometimes :slight_smile:

Ok, como te dije no manejo muy bien el ingles, te explicare en español.

El código se puede colocar en cualquier parte del sketch que no se vea afectado por la espera del cliente. En este caso la forma mas rápida fue al inicio del ciclo. Así controla el led antes de ver que sucede con las peticiones de la conexión ethernet. Como lo tenias en el sketch original el el código tenia que esperar una acción del botón de la pagina. En cambio al colocarlo fuera el led es controlado por el tiempo transcurrido y solo se controla el encendido / apagado desde la pagina.

SI te fue de ayuda modifica el titulo y coloca SOLVED

That makes sense. Thank you very much!