Hi,
Im making an ESP controlled LED strip for my friends bike which uses a webserver to control the different patterns. the webserver has a single button which toggles through the different animations and 3 physical buttons for on the fly colour changes. my issue is im usingthis borrowed code and have modified it to my needs however i have more patterns than him now but the button on the website wont select the new patterns, when i type the request into the address bar however it appears. Any help is appreciated thanks.
#include <WiFi.h>
#include <FastLED.h>
#if defined(FASTLED_VERSION) && (FASTLED_VERSION < 3001000)
#warning "Requires FastLED 3.1 or later; check github for latest code."
#endif
#define DATA_PIN 13
#define LED_TYPE WS2812
#define COLOR_ORDER GRB
#define NUM_LEDS 30
CRGB leds[NUM_LEDS];
#define BRIGHTNESS 100
#define FRAMES_PER_SECOND 120
uint8_t red = 0;
uint8_t green = 0;
uint8_t blue = 0;
uint8_t gHue = 0;
int j = 0;
const int Button1 = 17;
const int Button2 = 16;
const int Button3 = 4;
int Button2State = 0;
int Button1State = 0;
int Button3State = 0;
int Count1;
int Count2;
int Count3;
int Value1;
int Value2;
int Value3;
#define NUM_NETWORKS 5
// Add your networks credentials here
const char* ssid = "Gayboys Pretty Lights";
const char* password = "Muffin";
WiFiServer server(80);
String header;
void setup() {
Serial.begin(115200);
FastLED.addLeds<LED_TYPE,DATA_PIN,COLOR_ORDER>(leds, NUM_LEDS).setCorrection(TypicalLEDStrip);
FastLED.setBrightness(BRIGHTNESS);
EVERY_N_MILLISECONDS( 20 ) { gHue++; }
FastLED.show();
Serial.print("Setting AP (Access Point)…");
WiFi.softAP(ssid, password);
IPAddress IP = WiFi.softAPIP();
Serial.print("AP IP address: ");
Serial.println(IP);
server.begin();
xTaskCreate(
taskLED, /* Task function. */
"taskLED", /* String with name of task. */
10000, /* Stack size in bytes. */
NULL, /* Parameter passed as input of the task */
2, /* Priority of the task. */
NULL); /* Task handle. */
xTaskCreate(
taskWifi, /* Task function. */
"taskWifi", /* String with name of task. */
10000, /* Stack size in bytes. */
NULL, /* Parameter passed as input of the task */
1, /* Priority of the task. */
NULL); /* Task handle. */
}
void Manual() {
Button1State = digitalRead(Button1);
Button2State = digitalRead(Button2);
Button3State = digitalRead(Button3);
if(Button1State == 1){
Count1 = ++Count1;
}
if(Count1 == 10){
Value1=!Value1;
Count1 = 0;
}
if(Button2State == 1){
Count2 = ++Count2;
}
if(Count2 == 10){
Value2=!Value2;
Count2 = 0;
}
if(Button3State == 1){
Count3 = ++Count3;
}
if(Count3 == 10){
Value3=!Value3;
Count3 = 0;
}
if(Value3 == 1){
for (int i=0; i<NUM_LEDS; i=i+1) {
leds[i] = CRGB::White;
}
}
else if(Value2 == 1){
for (int i=0; i<NUM_LEDS; i=i+1) {
leds[i] = CRGB::OrangeRed;
}
}
else if(Value1 == 1){
for (int i=0; i<NUM_LEDS; i=i+1) {
leds[i] = CRGB::Purple;
}
}
else{
for (int i=0; i<NUM_LEDS; i=i+1) {
leds[i] = CRGB::Black;
}
}
FastLED.show();
delay(40);
}
void red_shine() {
fadeToBlackBy( leds, NUM_LEDS, 7);
int pos = beatsin16( 20, 0, NUM_LEDS );
leds[pos] += CHSV( gHue, 255, 192);
FastLED.show();
}
void confetti() {
fadeToBlackBy( leds, NUM_LEDS, 10);
int pos = random16(NUM_LEDS);
leds[pos] += CHSV( gHue + random8(64), 200, 255);
FastLED.show();
delay(40);
}
void rainbow(){
fill_rainbow( leds, NUM_LEDS, gHue, 7);
FastLED.show();
delay(50);
}
void led_off() {
for (int i=0; i<NUM_LEDS; i=i+1) {
leds[i] = CRGB::Black;
}
delay(100);
FastLED.show();
}
void rainbow_pingpong()
{
static uint8_t hue = 0;
Serial.print("x");
for(int i = 0; i < NUM_LEDS; i++)
{
leds[i] = CHSV(hue++, 255, 255);
FastLED.show();
for(int i = 0; i < NUM_LEDS; i++) { leds[i].nscale8(250); }
delay(10);
}
Serial.print("x");
for(int i = (NUM_LEDS)-1; i >= 0; i--) {
leds[i] = CHSV(hue++, 255, 255);
FastLED.show();
for(int i = 0; i < NUM_LEDS; i++) { leds[i].nscale8(250); }
delay(10);
}
delay(100);
FastLED.show();
}
void big_lights()
{
fadeToBlackBy( leds, NUM_LEDS, 20);
byte dothue = 0;
for( int i = 0; i < 8; i++) {
leds[beatsin16( i+7, 0, NUM_LEDS-1 )] |= CHSV(dothue, 200, 255);
dothue += 32;
}
FastLED.show();
delay(50);
}
void Jacob_did_an_oopsie()
{
fill_solid(leds, NUM_LEDS, CRGB::OrangeRed );
delay(500);
FastLED.show();
fill_solid(leds, NUM_LEDS, CRGB::Black);
delay(500);
FastLED.show();
}
char* getModeName(uint8_t modeNo)
{
switch (modeNo) {
case 0:
return "Manual";
case 1:
return "red shine";
case 2:
return "confetti";
case 3:
return "rainbow";
case 4:
return "off";
case 5:
return "rainbow pingpong";
case 6:
return "big lights";
case 7:
return "Jacob did an oopsie";
}
return "error";
}
uint8_t modeRGB = 0;
void taskLED( void * parameter )
{
while (1) {
switch (modeRGB) {
case 0:
Manual();
break;
case 1:
red_shine();
break;
case 2:
confetti();
break;
case 3:
rainbow();
break;
case 4:
led_off();
break;
case 5:
rainbow_pingpong();
break;
case 6:
big_lights();
break;
case 7:
Jacob_did_an_oopsie();
break;
default:
Manual();
break;
}
}
Serial.println("Ending task LED");
vTaskDelete( NULL );
}
void loop()
{
while (1)
{
delay(1000);
}
}
static const char* htmlHead = R"rawText(
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="icon" href="data:,">
<style>
html {
font-family: Helvetica;
display: inline-block;
margin: 0px auto;
text-align: center;
}
.button {
background:
linear-gradient(to right, #FB6060 0%, #EC305D 100%);
border: none;
text-decoration: none;
margin: 2px;
padding: 25px 25px;
width: 300px;
border-radius: 12px;
color: white;
font-size: 30px;
cursor: pointer;
}
</style>
</head>
)rawText";
void taskWifi( void * parameter )
{
while (1)
{
WiFiClient client = server.available();
if (client)
{
Serial.println("New Client.");
String currentLine = "";
Serial.printf("connected: %d\n", (int)client.connected());
while (client.connected()) {
if (client.available())
{
char c = client.read();
header += c;
if (c == '\n')
{
if (currentLine.length() == 0) {
client.println("HTTP/1.1 200 OK");
client.println("Content-type:text/html");
client.println("Connection: close");
client.println();
if (header.indexOf("GET /0") >= 0)
{
modeRGB = 1;
}
if (header.indexOf("GET /1") >= 0)
{
modeRGB = 2;
}
if (header.indexOf("GET /2") >= 0)
{
modeRGB = 3;
}
if (header.indexOf("GET /3") >= 0)
{
modeRGB = 4;
}
if (header.indexOf("GET /5") >= 0)
{
modeRGB = 5;
}
if (header.indexOf("GET /6") >= 0)
{
modeRGB = 6;
}
if (header.indexOf("GET /4") >= 0)
{
modeRGB = 0;
}
client.println("<!DOCTYPE html><html>");
client.println(htmlHead);
client.println("<body><h1>ESP32 Web RGB strip control</h1>");
client.print("<p><a href=\"/");
client.print(modeRGB);
client.print("\"><button class=\"button\">");
client.print(getModeName(modeRGB));
client.println("</button></a></p>");
client.println("</body></html>");
client.println();
break;
} else {
currentLine = "";
}
} else if (c != '\r')
{
currentLine += c;
}
}
}
header = "";
client.stop();
Serial.println("Client disconnected.");
Serial.println("");
}
}
}