There's no delay (but should be...) should or can an interrupt be used?

The goal is to have a delay that can vary depending on how long pin 11 is on (HIGH) and the status of pin 3 (latching button). That delay would be between the moment pin 11 goes off (LOW) and before pin 9 goes (HIGH). The delay would max out at 2100ms after pin 11 has been on for 3 seconds or more. Anytime while in the delay, pin 11 could (maybe Interrupt?) go HIGH within 10-20ms or the nearest and shortest time possible. If pin 11 is set to HIGH for less Than 3 seconds, the delay would be shorter and changed depending on how long and 11 was HIGH for.

Depending on the state of the latching button (pin 3), the appropriate pins will go high and low (pins 9 & 11). The issue is that there's supposed to be a delay between pin 11 going low and pin 9 going high and there is no delay.

Any help would be greatly appreciated!

Roto_insta_speed_change.ino (2.64 KB)

Please include short programs in your Post so people don't have to download them.

int pedalPin = 3;
int slowRelay = 9;
int fastRelay = 11;

unsigned long currentTime = millis;
unsigned long fastTime; // time that fast relay 1st went on
unsigned long rampDown; // time it takes to ramp down from fast to slow

unsigned long iSlowShort;
unsigned long iSlow ;

void setup() {
  pinMode(pedalPin, INPUT); // toggle (Latching foot switch pedal)
  pinMode(slowRelay, OUTPUT); //slow relay
  pinMode(fastRelay, OUTPUT); //fast relay

  digitalWrite(slowRelay, LOW); // turns off the slow motor relay when first powered on
  digitalWrite(fastRelay, LOW); // truns off the fast motor relay when first powered on


  // {HERE} is good place for a "while loop" to check if pedal is connected and keep both motors off!!!!!!!!!!!!!!!!!!
  // maybe think about flashing an led behind the pedal jack to indecate the problem

}

void loop() {


  if (digitalRead(pedalPin) == LOW)//foot switch is off
  {
    digitalWrite(slowRelay, HIGH); //slow relay to slow motor is turned off
    digitalWrite(fastRelay, LOW); //fast relay truns on fast motor
    currentTime = fastTime; // documents when the fast motor was lasted turned on
    while (digitalRead(pedalPin) == LOW) {//waits untill peldal changes states and NOT allow fastTIme to be updated
      if (digitalRead(pedalPin) == LOW) //keeps running this "while loop"
      { delay (10);
      }
      if (digitalRead(pedalPin) == HIGH) //stops running this "while loop"
      { break;
      }

    }

  }


  if (digitalRead(pedalPin) == HIGH)//foot switch is on (slow)
  {
    digitalWrite(fastRelay, HIGH);//fast motor and relay is turned off

    while ((digitalRead(pedalPin) == HIGH) && currentTime - fastTime > 3000) // fast motor has been on for at least 3 seconds and was running at max speed

    { for (int i = 2100; i <= 0; i - 10)// than delay (unless the pedal is back to off(set to fast)) for 2.1 seconds before turing the slow motor on
      { delay (10);
        if (digitalRead(pedalPin) == LOW)// checks if pedal has changed to off (fast) and will exit the loop if it has changed
        {
          break;
        }
      }
    }

    rampDown = currentTime - fastTime * (10 / 7); //finds the appropriate time it will take inertia of the fast motor to reach the slow motor speed

    while ((digitalRead(pedalPin) == HIGH) && currentTime - fastTime < 3000) //fast motor has been on under 3 seconds and never reached max speed

    {
      if (digitalRead(pedalPin) == LOW || rampDown <= 0) //if pedal is off (fast) or the timer (rampDown) has been ended, exit the loop
      {
        break;
      }
      delay(10); //timer
      rampDown - 10;//timer
    }
    digitalWrite(slowRelay, LOW);  //slow relay turns on slow motor
  }

}

...R

If you want a responsive program you should remove all the delay()s and all the WHILE loops. Allow loop() to do the repetition and use millis() to manage the timing without blocking.

If a time period should start when a button in pressed then you should record the value of millis() and later your program can check if an interval has elapsed like this

if (millis() - timeButtonPressed >= interval) {
  // do something
}

For examples of using millis() have a look at Several Things at a Time.

And see Using millis() for timing. A beginners guide if you need more explanation.

...R

Thank you Robin2! The info you shared definitely helps!