Bicycle launch control code help

Hello,

i am trying to add soft start button to 24v 350 watt DC brushed motor on my bicycle via arduino.

the code works great (found it after searching many hours...:slight_smile:

The problem is: I have to wait for fading effect to be completed in order to activate/de-activate push button :frowning:

this disables the control of motor quickly depending on the conditions.

i have found out that adding boolean to somewhere of the code can solve the problem.

Can you retouch the code given below or share a better code?

thank you in advance

/*
 Project name: bicycle launch control

 
 */

// constants won't change their value
const int buttonPin = 2;     // the number of the pushbutton pin
const int ledPin = 9;        // the number of the LED pin
const int fadingDelay = 50;  // the speed of LED fading (higher value = slower)

// variables will change:
int buttonState = 0;         // variable for reading the pushbutton status
boolean fadingState = false; // determines whether the LED needs to be turned on or off

void setup() {
  // initialize the LED pin as an output:
  pinMode(ledPin, OUTPUT);
  // initialize the pushbutton pin as an input:
  pinMode(buttonPin, INPUT);
}

void loop() {
  // read the state of the pushbutton value:
  buttonState = digitalRead(buttonPin);

  // check whether the button was pressed
  if (buttonState == HIGH) {
    // check the current LED status (on/off)
    if (fadingState == false) {
      // turn on the LED
      for (int i = 50; i <= 255; i += 5) {
        analogWrite(ledPin, i);
        delay(fadingDelay);
      }
    } else {
      // turn off the LED
      for (int i = 150; i >= 0; i -= 5) {
        analogWrite(ledPin, i);
        delay(fadingDelay);
      }      
    }
    fadingState = !fadingState;  // save the current LED state (on/off)
  }
}

Don't use delay(), as the processor does nothing else during delay().

Use millis() instead, as described in this tutorial: https://www.baldengineer.com/blink-without-delay-explained.html