Hi all, first time posting so apologies for mistakes. I believe my problem is my global variable int=option isn't updating my switch-case in the loop() function. I'm using the global variable (int =option) to determine which case to use. I'm making a wireless light, that will mimic fireflies I'm using the ESP8266 as a server to control the LED from a web page. I have multiple flash functions. In the loop() I have each flash function call in a switch-case. But it will only run the function I select first and won't stop. I would like it to run each function until I click another button, and then run that button's function. If the update to my option is my problem how would I go about updating it? I've searched for solutions but haven't found any applicable, maybe I'm using the incorrect search criteria...
Here's my code: it's very linear, still learning so any pointers are much appreciated! Thank you so much in advance!
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
#include <Adafruit_NeoPixel.h>
#define PIN 15
const char* ssid = "suddenlink.net-69B0"; //enter ssid
const char* password = "244142014907"; //enter password
ESP8266WebServer server(80);
Adafruit_NeoPixel strip = Adafruit_NeoPixel(1, PIN, NEO_GRB + NEO_KHZ800);
String webPage="";// String to display
int gpio15_pin = 15;
int button = 0; //Initialize value to determine button press HIGH or LOW
int option = 0; //option: is used to differentiate and select one out of the 4 effects
int shine = 0; //brightness value
byte red = 0;
byte green = 0;
byte blue = 0;
int FlashCount = 0;//number of flashes in a cycle
int FlashSpeed = 0;//delay (ms) btween each individual flash
int FlashCyclePause = 0;//wait until next cycle start
int NUM_LEDS = 1;
boolean turnOn = false;
void setup()
{
webPage += "<h1>Firefly IoT Project</h1><p>Light <a href=\"LightOn\"><button>ON</button></a> <a href=\"LightOff\"><button>OFF</button></a></p>";
webPage += "<p>Firelfy Pyractonema angulata<a href=\"FirelfyPyractonemaAngulata\"><button>ON</button></a></p>";
webPage += "<p>Firefly Photinus carolinus<a href=\"FireflyPhotinusCarolinus\"><button>ON</button></a></p>";
webPage += "<p>Firefly Photuris versicolor<a href=\"FireflyPhoturisVersicolor\"><button>ON</button></a></p>";
webPage += "<p>Firefly Fake<a href=\"FireflyFake\"><button>ON</button></a></p>";
// preparing GPIOs
pinMode(gpio15_pin, OUTPUT);
digitalWrite(gpio15_pin, LOW);
strip.begin();
strip.show(); // Initialize all pixels to 'off'
delay(1000);
// You can open the Arduino IDE Serial Monitor window to see what the code is doing
Serial.begin(115200); // Serial connection from ESP-01 via 3.3v console cable
// Connect to WiFi network
WiFi.begin(ssid, password);
Serial.print("\n\r \n\rWorking to connect");
// Wait for connection
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
Serial.print(".");
}
server.on("/", []()
{
server.send(200, "text/html", webPage);
});
server.on("/LightOn", []()
{
server.send(200, "text/html", webPage);
digitalWrite(gpio15_pin, HIGH);
setAll(0x00, 0xff, 0x00);
strip.show();
delay(1000);
});
server.on("/LightOff", []()
{
server.send(200, "text/html", webPage);
digitalWrite(gpio15_pin, LOW);
setAll(0x00,0x00,0x00);
strip.show();
delay(1000);
});
server.on("/FirelfyPyractonemaAngulata", []()
{
server.send(200, "text/html", webPage);
//digitalWrite(gpio15_pin, HIGH);
option = 1;
delay(1000);
});
server.on("/FireflyPhotinusCarolinus", []()
{
server.send(200, "text/html", webPage);
//digitalWrite(gpio15_pin, HIGH);
option = 2;
delay(1000);
});
server.on("/FireflyPhoturisVersicolor", []()
{
server.send(200, "text/html", webPage);
//digitalWrite(gpio15_pin, HIGH);
option = 3;
delay(1000);
});
server.on("/FireflyFake", []()
{
server.send(200, "text/html", webPage);
//digitalWrite(gpio15_pin, HIGH);
option = 4;
delay(1000);
});
server.begin();
}
void loop()
{
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
server.handleClient();
if(gpio15_pin == HIGH)
{
turnOn = true;
}
else
{
turnOn = false;
}
if (turnOn == true)
{
//digitalWrite(gpio15_pin, HIGH);
switch(option)
{
case 1:// 1st effect : Firefly Pyractonema angulata
FirelfyPyractonemaAngulata();
break;
case 2:// 2nd effect : FireflyPhotinus carolinus
FireflyPhotinusCarolinus() ;
break;
case 3://3rd effect : Firefly Photuris versicolor
FireflyPhoturisVersicolor();
break;
case 4://4th effect : FireflyFake
FireflyFake();
break;
}
}
delay(1);
//gpio15_pin = LOW;
//digitalWrite(gpio15_pin, LOW);
}
void setPixel(int Pixel, byte red, byte green, byte blue)
{
strip.setPixelColor(Pixel, strip.Color(red, green, blue));
}
void setAll(byte red, byte green, byte blue)
{
for(int i = 0; i <= NUM_LEDS; i++ )
{
setPixel(i, red, green, blue);
}
strip.show();
}
//Sets the brightness. This takes a single argument, a number in the range 0 (off) to 255 (max brightness).
//For example, to set a strip to 1/4 brightness: (64).
void brightness(int shine)
{
strip.setBrightness(shine);
strip.show();
}
//generates random number for flash counts that are variable.
int randomNumber(int number, int max)
{
randomSeed(ESP.getCycleCount());
return random(number, max);
}
//
void FireflyMaleFlash(byte red, byte green, byte blue, int FlashCount, int FlashSpeed, int FlashCyclePause)
{
for (int j=0; j<FlashCount; j++)
{
setAll(red, green, blue);
strip.show();
delay(FlashSpeed);
setAll(0,0,0);
strip.show();
delay(FlashSpeed);
}
delay(FlashCyclePause);
}
void FireflyFemaleFlash(byte red, byte green, byte blue, int FlashCount, int FlashSpeed, int FlashCyclePause, int ResponsePause)
{
for (int j=0; j<FlashCount; j++)
{
delay(ResponsePause);
setAll(red, green, blue);
strip.show();
delay(FlashSpeed);
setAll(0,0,0);
strip.show();
delay(FlashSpeed);
}
delay(FlashCyclePause);
}
//Pyractonema angulata firefly amber
//effect1
void FirelfyPyractonemaAngulata()
{
int count = randomNumber(8,10);//male flash between 8-10x
FireflyMaleFlash(0xFF,0xC2,0x00, count, 100, 3700);
FireflyFemaleFlash(0xFF, 0xC3, 0x00, 1, 0, 166,1200);
}
//Photinus carolinus firelfy
//effect2
void FireflyPhotinusCarolinus()
{
int countMale = randomNumber(6,8);//male flash between 6-8x
int countFemale = randomNumber(2,6);//female flash between 2-6x
FireflyMaleFlash(0xFF,0xC2,0x00, countMale, 300, 10000);
FireflyFemaleFlash(0xFF, 0xC3, 0x00, countFemale, 100, 500,3000);
}
//Photuris versicolor firelfy
//effect3
void FireflyPhoturisVersicolor()
{
FireflyMaleFlash(0xad,0xff,0x2f, 6, 100, 4200);
//FireflyFemaleFlash(0xad, 0xff, 0x2f,?,?,?,?);
}
//Fake Flash firelfy
//effect4
void FireflyFake()
{
int countMale = randomNumber(6,10);//male flash between 6-10x
int countFemale = randomNumber(2,6);//female flash between 2-6x
FireflyMaleFlash(0xD1, 0xF5, 0x0B, countMale,80, 300);
FireflyFemaleFlash(0xFF, 0xC3, 0x00, countFemale, 60, 1000,2000);
}