Skip to main content
comment about state=0
Source Link
Kingsley
  • 773
  • 6
  • 13

You never set state back from 1, so really the button push is starting an repeated call for taskExecute();

The variables previousMillis (1-3) are only evernot properly initialised to zero. So if you wait a while before pressing the button, that delay time becomes part of the delaytime that the LED is illuminated for.

If your taskExecute() function sets them before the loop, this will fix that:

void taskExecute(int tMotor1, int tMotor2, int tMotor3) { int ledState1 = HIGH; int ledState2 = HIGH; int ledState3 = HIGH; digitalWrite(ledPin1, ledState1); digitalWrite(ledPin2, ledState2); digitalWrite(ledPin3, ledState3); unsigned long time_now = millis();  previousMillis1 = time_now;  previousMillis2 = time_now;  previousMillis3 = time_now; do { ... } while (ledState1 == HIGH || ledState2 == HIGH || ledState3 == HIGH); state = 0; } 

On a coding-style note - I did not initially see the resetting of the global variable state=0 at the end of the taskExecute() function. It might be better to move this up to underneath the initial call to taskExecute() so all the state handling is in one place.

You never set state back from 1, so really the button push is starting an repeated call for taskExecute();

The variables previousMillis (1-3) are only ever initialised to zero. So if you wait a while before pressing the button, that delay time becomes part of the delay.

If your taskExecute() function sets them before the loop, this will fix that:

unsigned long time_now = millis(); previousMillis1 = time_now; previousMillis2 = time_now; previousMillis3 = time_now; 

The variables previousMillis (1-3) are not properly initialised. So if you wait a while before pressing the button, that delay time becomes part of the time that the LED is illuminated for.

If your taskExecute() function sets them before the loop, this will fix that:

void taskExecute(int tMotor1, int tMotor2, int tMotor3) { int ledState1 = HIGH; int ledState2 = HIGH; int ledState3 = HIGH; digitalWrite(ledPin1, ledState1); digitalWrite(ledPin2, ledState2); digitalWrite(ledPin3, ledState3); unsigned long time_now = millis();  previousMillis1 = time_now;  previousMillis2 = time_now;  previousMillis3 = time_now; do { ... } while (ledState1 == HIGH || ledState2 == HIGH || ledState3 == HIGH); state = 0; } 

On a coding-style note - I did not initially see the resetting of the global variable state=0 at the end of the taskExecute() function. It might be better to move this up to underneath the initial call to taskExecute() so all the state handling is in one place.

Source Link
Kingsley
  • 773
  • 6
  • 13

You never set state back from 1, so really the button push is starting an repeated call for taskExecute();

The variables previousMillis (1-3) are only ever initialised to zero. So if you wait a while before pressing the button, that delay time becomes part of the delay.

If your taskExecute() function sets them before the loop, this will fix that:

unsigned long time_now = millis(); previousMillis1 = time_now; previousMillis2 = time_now; previousMillis3 = time_now;