Go Down

Topic: Function to Blink a led 5 times only when called - state machine issue? (Read 193 times) previous topic - next topic

Mike_Cymru

Hi
My question relates to how a led blink function which will blink a led for 5 times (see below) can be called from within another function and perform this task and exit awaiting a subsequent call.  I have spent hours investigating flags and trying to understand 'the state machine' in this regard but without success.

The project, when complete will allow programmable square wave pulse trains of varying duration and frequency.

When called, the void function 'ledFlasher()', within the main program loop() continously blinks the led as opposed to 5 times and stop awaiting the next call.  I'm sure there is a simple solution out there.
 

Code: [Select]

void loop(){

  composite_PW(t_compPW);

  handlePWMDutyCycles(pulseTrainEnable);

  readIRDecoder(); //Monitor IR command decoder

  ledFlasher();

} //End Loop



The calling function which detects a valid received IR code from an IR transmitter handset calls 'ledFlasher()' once as shown below:

Code: [Select]


void readIRDecoder() {
  if (irrecv.decode(&results)) {
    Serial.println(results.value, HEX);
    switch (results.value)

    {
 
      case 0xFFA25D: //Key1
        Serial.print("Key 1 Pressed(1): ") + Serial.println(results.value, HEX);
        results.value = 0;
        ledFlasher();
        break;



This 'readIRDecoder' code segment will eventually call another function but the issue is giving the user visual feedback that the module has correctly received a keypad keypress.  No delays are used in any function (millis() and micros() only, and, apart from visual user feedback the project is nearing completion.

The ledFlasher function code is given below:

Code: [Select]

void ledFlasher(){

      currentTime = millis();
     if (currentTime - waitStart >= shortPeriod)
      {
        digitalWrite(redLedPin, !digitalRead(redLedPin));
        waitStart = currentTime;
        flashCount++;
        if (flashCount == 10)
        {
          flashCount = 0;
         
        }
      }
}//End of ledFlasher


To avoid confusion I have only focused on the code that is relevant to the problem needing advice.

Hope a forum member can offer guidance.  I suspect I may need to use system flags in the program and pass them as function arguments in some way or interrogate them within the ledFlasher function..
Thanks in anticipation.



Grumpy_Mike

Quote
avoid confusion I have only focused on the code that is relevant to the problem needing advice.
Compleatly ignoring the forum guide lines.

The trick is to use either global variables or static variables.

However you need to post all your code r write a shorter version that cuts the crap but still illustrates your problem.

Mike_Cymru

OK Noted.  I will attempt to modify my code to use variables as suggested and if no joy will post up iaw forum rules.  Thanks

Go Up