I am facing a issue with esp8266 ,I have two button "Pattern1" and "Pattern2"

I am facing a issue with esp8266 ,I have two button “Pattern1” and “Pattern2” , I want to run 2 LED pattern by using esp8266 and web browser, If I click on Pattern1 button the first pattern of both LED’s should run in loop continuously,and when I click Pattern2 button the second pattern should run in loop continuously until I click any other button.

My Code:

#include <ESP8266WiFi.h>

const char* ssid = “SSID”;
const char* password = “SSID_PASS”;

WiFiServer server(80);

#define LED1 D3
#define LED2 D4

void setup() {
pinMode(LED1, OUTPUT);
pinMode(LED2, OUTPUT);

digitalWrite(LED1, LOW);
digitalWrite(LED2, LOW);

Serial.begin(115200);
Serial.print("Connecting to ");
Serial.println(ssid);

WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);

while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}

Serial.println("");
Serial.println(“NodeMCU is connected to WiFi”);
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
server.begin();
delay(3000);
}

void loop() {
WiFiClient client;
client = server.available();

if (client == 1) {
String request = client.readStringUntil(’\n’);
client.flush();
Serial.println(request);

if (request.indexOf("pattern01") != -1)
{ pattern1();}

if (request.indexOf("pattern02") != -1)
{ pattern2();}


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

client.println("<!DOCTYPE HTML>");
client.println("<html>");
client.println("<head>");
client.println("<title> Led Pattern </title>");
client.println("</head>");
client.println("<body align= \"center\" >");
client.println("<h3> <font size = \"20\" face = \"Verdana\" >  Led Pattern Continue Loop Control  </font> </h3> ");
client.println("<p> <a href=\"/pattern01\"> <button style=\"height:60px; background-color: #195B6A; width:200px; cursor: pointer\"><b> Pattern 1 </b></button> <a> </font></p> ");
client.println("<p> <a href=\"/pattern02\"> <button style=\"height:60px; background-color: #195B6A; width:200px; cursor: pointer\"><b> Pattern 2 </b></button> <a> </font></p> ");
client.println("</body>");
client.println("</html>");

Serial.println("Client disonnected");
Serial.println("------------------");
Serial.println("                  ");

}
}

void pattern1(){
digitalWrite(LED1,HIGH);
delay(50);
digitalWrite(LED2,LOW);
delay(50);
digitalWrite(LED1,HIGH);
delay(50);
digitalWrite(LED1,LOW);
delay(50);
digitalWrite(LED1,HIGH);
delay(50);
digitalWrite(LED1,LOW);
delay(50);
digitalWrite(LED2,HIGH);
delay(50);
digitalWrite(LED2,LOW);
delay(50);
digitalWrite(LED2,HIGH);
delay(50);
digitalWrite(LED2,LOW);
delay(50);
digitalWrite(LED2,HIGH);
delay(50);
digitalWrite(LED2,LOW);
delay(50);
digitalWrite(LED1,HIGH);
delay(50);
digitalWrite(LED1,LOW);
delay(50);
digitalWrite(LED1,HIGH);
delay(50);
digitalWrite(LED1,LOW);
delay(50);
digitalWrite(LED1,HIGH);
delay(50);
digitalWrite(LED1,LOW);
delay(50);
digitalWrite(LED2,HIGH);
delay(50);
digitalWrite(LED2,LOW);
delay(50);
digitalWrite(LED2,HIGH);
delay(50);
digitalWrite(LED2,LOW);
delay(50);
digitalWrite(LED2,HIGH);
delay(50);
digitalWrite(LED2,LOW);
delay(50);
// I want to start the loop “Pattern 1” again
}

void pattern2(){
digitalWrite(LED1,HIGH);
digitalWrite(LED2,HIGH);
delay(50);
digitalWrite(LED1,LOW);
digitalWrite(LED2,LOW);
delay(50);
digitalWrite(LED1,HIGH);
digitalWrite(LED2,HIGH);
delay(50);
digitalWrite(LED1,LOW);
digitalWrite(LED2,LOW);
delay(50);
digitalWrite(LED1,HIGH);
digitalWrite(LED2,HIGH);
delay(50);
digitalWrite(LED1,LOW);
digitalWrite(LED2,LOW);
delay(50);
digitalWrite(LED1,HIGH);
digitalWrite(LED2,HIGH);
delay(50);
digitalWrite(LED1,LOW);
digitalWrite(LED2,LOW);
delay(50);
digitalWrite(LED1,HIGH);
digitalWrite(LED2,HIGH);
delay(50);
digitalWrite(LED1,LOW);
digitalWrite(LED2,LOW);
delay(50);
digitalWrite(LED1,HIGH);
digitalWrite(LED2,HIGH);
delay(50);
digitalWrite(LED1,LOW);
digitalWrite(LED2,LOW);
delay(50);
digitalWrite(LED1,HIGH);
digitalWrite(LED2,HIGH);
delay(50);
digitalWrite(LED1,LOW);
digitalWrite(LED2,LOW);
delay(50);
digitalWrite(LED1,HIGH);
digitalWrite(LED2,HIGH);
delay(50);
digitalWrite(LED1,LOW);
digitalWrite(LED2,LOW);
delay(50);
digitalWrite(LED1,HIGH);
digitalWrite(LED2,HIGH);
delay(50);
digitalWrite(LED1,LOW);
digitalWrite(LED2,LOW);
delay(50);
digitalWrite(LED1,HIGH);
digitalWrite(LED2,HIGH);
delay(50);
digitalWrite(LED1,LOW);
digitalWrite(LED2,LOW);
delay(50);
digitalWrite(LED1,HIGH);
digitalWrite(LED2,HIGH);
delay(50);
digitalWrite(LED1,LOW);
digitalWrite(LED2,LOW);
delay(50);
digitalWrite(LED1,HIGH);
digitalWrite(LED2,HIGH);
delay(50);
digitalWrite(LED1,LOW);
digitalWrite(LED2,LOW);
delay(50);
}

If your problem is that once a pattern has run once it stops then instead of

if (request.indexOf("pattern01") != -1)
{ pattern1();}

calling pattern1() have it set a boolean variable indicating that pattern1 should be run to true. Maybe name it patten1Running and set it initially to false

Later in loop() check the value of the boolean and if it is true then call the pattern1() function. Don’t forget to set the boolean to false if pattern2 is selected

Do the same for both patterns and you will be able to switch between them

Note that when either of the patterns is running the 8266 will spend most of its time doing nothing but executing the delay()s during which the 8266 can do literally nothing else, including responding to a request to change pattern. Is that OK with you ?

#define LED1 D3
#define LED2 D4

These pins are also know as GPIO 0 & GPIO 2 (if you are using a nodeMCU that is) How do you have the LED’s connected ? Keep in mind that you have to connect them ‘Active Low’ which means that at startup they are pulled ‘HIGH’ so the ESP starts up in normal mode. If GPIO 0 (D3) is pulled low at startup it will go into flash mode. If GPIO2 (D4) is pulled ‘LOW’ it doesn’t start up properly at all. D4 (GPIO 2) has the Built in led connected as well, and that is connected ‘active low’

Dear Arduino Team,
Greetings!!!

Thank you for your reply.
Yes I put both LED to active LOW.

void setup() {
pinMode(LED1, OUTPUT);
pinMode(LED2, OUTPUT);

digitalWrite(LED1, LOW);
digitalWrite(LED2, LOW);

Dear Arduino Team,
Greetings!!!

Thank you for your reply.
I have been trying to do this from the last one week., this is my request, could you please send me the code for my project.

I attached my script here.

Thanks in advance !!!

(Attachment Led Pattern Continue Loop Control_Original.txt is missing)

Led Pattern Continue Loop Control_Original.txt.ino (6.16 KB)

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.