Função infinita com interrupção por botão em pagina html e Ws2812

Boa tarde pessoal, estou fazendo um projeto com o EPS 12E e led ws2812, meu objetivo seria através de uma pagina html eu controlar com botões funções diferentes, meu problema é que quando eu aciono um botão pelo site, ele executa o código do led, porem não consigo desativar ou ativar outra enquanto o tempo dele não terminar e o loop seguir, já tentei tirar do loop a função e deu no mesmo, tentei também a função do while, alguém saberia como fazer algo assim? achei na internet umas funções com botão simples, mas eles também só deixar pular a função quando o tempo termina. Obrigado

Exibições: 227

Responder esta

Respostas a este tópico

Olá Bruno,

Não temos visão de seu código para poder dar uma opinião mais precisa.

Mas em linhas gerais acredito que seja necessário usar Tarefas (Task) dentro do RTOS do ESP12E.

Pesquise sobre como fazer "processamento paralelo" usando o FreeRTOS que é o suportado pelo Arduino via Espressif.

Talvez estes links lhe ajudem:

https://visualgdb.com/tutorials/esp8266/relay/

https://randomnerdtutorials.com/esp32-esp8266-web-server-physical-b...

Abraços.

Hey Rodrigo, obrigado pela dica! vou dar uma pesquisada sobre o tema. Acredito que exista uma solução simplificada disso, para por exemplo o mesmo led em um arduino uno poder pular o efeito sem esperar ele terminar...mesmo assim agradeço sua resposta! Abraços. (adicionei o código)
como neste link (tem os casos pulados por botão, mas só é possível quando acaba o tempo de execução de cada parte):
 https://create.arduino.cc/projecthub/Oniichan_is_ded/neopixel-x-ard... 


esqueci de colocar o codigo, hehe

#include <ESP8266WiFi.h>
#include <Adafruit_NeoPixel.h>

const char* ssid = "*******";
const char* password = "*******";

int luz3 = D3;

Adafruit_NeoPixel strip = Adafruit_NeoPixel(2, luz3, NEO_GRB + NEO_KHZ800);
uint8_t myColors[][3] = {{232, 0, 5}, // red
{0, 200, 0}, // yellow

};
#define FAVCOLORS sizeof(myColors) / 3
WiFiServer server(80);

int statusLuz3 = LOW;


void setup(){
Serial.begin(115200);

Serial.print("Conectado á ");
Serial.println(ssid);

WiFi.begin(ssid, password);

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

server.begin();
Serial.println("Servidor iniciado");

IPAddress ip(***,***,*,*); 
IPAddress gateway(***,***,*,*); 
IPAddress subnet(255,255,255,0);

Serial.print("Use a seguinte URL para acessar: ");
Serial.print("http:// ");
Serial.print(ip);
Serial.println("/");

WiFi.config(ip, gateway, subnet);

strip.begin();
strip.setBrightness(150);
strip.show(); // Initialize all pixels to 'off'
}


void loop(){
WiFiClient client = server.available();
if(!client){
return;
}

String request = client.readStringUntil('\r');
Serial.println(request);
client.flush();

if (request.indexOf("/ON3")!= -1){
statusLuz3 = HIGH;
flashRandom(10, 50);  // ----------------------------------------- chama a função
}
if (request.indexOf("/OFF3")!= -1){
digitalWrite(luz3, LOW);
statusLuz3 = LOW;
}


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>Automação Residencial</title>");
client.println("<meta charset=\"utf-8\">");
client.println("<meta name=\"viewport\" content=\"initial-scale=1.0, user-scalable=no\">");
client.println("</head>");
client.println("<body bgcolor= \"#B0E0E6\">");
client.println("<center>");


client.print("Estado do LED3: ");
if (statusLuz3 == HIGH){
client.print("Ligado");
}
else {
client.print("Desligado");
}

client.println("<br><br>"); // pula linha
client.println("<a href=\"/ON3\"\"><button>Liga </button></a>");
client.println("<a href=\"/OFF3\"\"><button>Desliga </button></a>");
client.println("<br><br>"); // pula linha
client.println("</center>");
client.println("</body>");
client.println("</html>");

delay(1);
Serial.println("Cliente desconectado");
Serial.println("");
}

void flashRandom(int wait, uint8_t howmany) {

for(uint16_t i=0; i<howmany; i++) {
// pick a random favorite color!
int c = random(FAVCOLORS);
int red = myColors[c][0];
int green = myColors[c][1];
int blue = myColors[c][2];

// get a random pixel from the list
int j = random(strip.numPixels());

// now we will 'fade' it in 5 steps
for (int x=0; x < 5; x++) {
int r = red * (x+1); r /= 5;
int g = green * (x+1); g /= 5;
int b = blue * (x+1); b /= 5;

strip.setPixelColor(j, strip.Color(r, g, b));
strip.show();
delay(wait);
}
// & fade out in 5 steps
for (int x=5; x >= 0; x--) {
int r = red * x; r /= 5;
int g = green * x; g /= 5;
int b = blue * x; b /= 5;

strip.setPixelColor(j, strip.Color(r, g, b));
strip.show();
delay(wait);
}
}

}

RSS

© 2024   Criado por Marcelo Rodrigues.   Ativado por

Badges  |  Relatar um incidente  |  Termos de serviço