Hi
I have developed a function for my Arduio-peripheral ESP8266-01 project which avoids any blocking code; delay(), While() etc. The function, when called from various locations in the main program provides visual operational status of the ESP TCP server and client. For example, 3 Red LED on:off cycles followed by 10 cycles of LED off indicates if no Client data requests are received by the server.
In the main loop a master free running 'clock' provides the alternating logical states to the function 100mS mark/space. The function accepts parameters for the 'clock', 'mark' cycle count, 'space' cycle count and one of three LED colours. (I now realise the 'clock' does not need to be passed as a parameter since it is a global variable anyway).
Whilst this works well as a Test program, when I embed it and call it from my TCP Server program, within any function it appears that the function's state changes occur very slowly - in fact each time the function is called from within other functions -and within some functions may take minutes to complete! So, what I need is when the function is called or triggered once the function executes a specific number of led on/off cycles followed by a specific numer of off cycles (as per the input parameters) until it is called again. And, until it completes the pulse train cycles the function is inhibited from re-triggering. Can my function design / rationale allow this with modification. I really am at a loss despite many attempts of proving it can. I hope my description isn't too confusing. Any suggestions welcome.
/*ESP8266_LEDBlinkFunction_1B_Working.ino 27th April 2017
Based on:
(ESP8266_UnderstandingDelaysWithoutBlocking_02.in0)
Blinking a LED function: 080417
http://forum.arduino.cc/index.php?topic=230517.0
*/
//This works note if(statCounter==10). This was changed from
//statCounter=10 (single equal sign.
const long interval_2 = 50; //This gives a LED on/off cycle time of 200mSec
unsigned long previousMillis_2 = 0; //All timing variables using millis() should be unsigned long: Forum post
//my post Arduino Forum) 29/03/17
bool lastCountState = false;
bool countState = false;
bool ledState = false;
void pulse_LED(bool state, int markCount, int spaceCount, String ledColour);
String opLedColour; //Operational LED colour
//unsigned long currentMillis_2 = millis();
void setup() {
//pinMode(ledPin, OUTPUT);
//const int ledPin = 4; // the pin that the LED is attached to
pinMode(7, OUTPUT); //red LED
pinMode(8, OUTPUT); //green LED
pinMode(9, OUTPUT); //blue LED
Serial.begin(9600);
}
void loop() {
unsigned long currentMillis_2 = millis();
if (currentMillis_2 - previousMillis_2 >= interval_2)
{
previousMillis_2 = currentMillis_2;
if (countState == false)
{ countState = true; \
} else if (countState == true) {
countState = false;
//change state
}
}
pulse_LED(countState, 4, 30, "red"); //ie 8/2 or 4 LED pulse cycles (800mSec); 12/2 or
}// end loop
//The function has three input parameters. 'state' provides a continously alternating
//logical state change derived from the millis timer in the sketch' loop() function. Note that
//each state lasts for a preset period of 100mSec and this represents the HALF cycle time
//ie Led ON or Led OFF. A complete cycle is this 200mSec. mCount and sCount are integer
//variables which define the number of half cycle states for the LED pulse train and the
//inter-pulse train gap respectively.
//For mCount:8 and sCount:12 the following pulse train is generated.
// _-_-_-_-____________-_-_-_-_-____________
void pulse_LED(bool state, int mCount, int sCount, String colour) {
static int counter; // note static - persists
int ledPin;
if (colour == "red") {
ledPin = 7;
} else if (colour == "green") {
ledPin = 8;
} else if (colour =="blue") {
ledPin = 9;
}
if (state != lastCountState) {
if (counter < mCount) { //Toggle LED on each 500mS count
if (ledState == LOW) {
ledState = HIGH;
} else if (ledState == HIGH) {
(ledState = LOW);
}
digitalWrite(ledPin, ledState);
Serial.print("ledPin: ") + Serial.println(ledState);
}
if ((counter > mCount) && (counter < sCount)) {
digitalWrite(ledPin, LOW);
Serial.print("ledPin: ") + Serial.println(ledState);
}
if (counter > sCount) {
digitalWrite(ledPin, LOW);
ledState = LOW;
counter = 0;
return;
}
Serial.print("counter: ") + Serial.println(counter);
counter++;
lastCountState = state;
}
}//end pulse_LED
[\code]