For loop soft start

Hello,
Im trying to write a soft start script for a motor.
im using a 12v LED strip at the moment with aid of a N-MOSFET, controlled via an analogue stick

I would like the motor to soft start when sensorvaluex >=800 and then stay HIGH whilst the stick remains in that position until it is returned to rest.

Im running in to issues at remain HIGH when n>=255

the for loop just keeps resetting once n=255

5 hours of different attempts im either getting a constant loop of for loops or a blinking state when i attempt to break;

Any pointers would be greatly appretiated

int led =9;
int sensorPinx = A0;
int sensorPiny = A1;
int sensorValuex = 0;
int sensorValuey = 0;


////////////////////////////////////////////

void softstart(){
int var=0;
int n=0;
for (n=0;n<255;n++){
   analogWrite(led,n);
   if (n>=255){
   n=0;
   var=1;
   break; 
   }
  delay(10);

     
  }
if (var=1){
   digitalWrite(led,HIGH); 
}
}

/////////////////////////////////////////////
void driveled(){
  if (sensorValuex > 800){
  softstart();
    } 
 
else{
  digitalWrite(led,LOW);
}

}
/////////////////////////////////////////////
void setup() {

  Serial.begin(9600);
pinMode(led, OUTPUT);
driveled();


}
void loop() {
sensorValuex = analogRead(sensorPinx);
Serial.println(sensorValuex);
delay(50);

driveled();

}
if (var=1){

Whoops

Appologies for my ignorance but i dont see the error

= for assignment of a value
== for comparison of 2 values

Thankyou, corrected but still no solution :frowning:

No idea what your sketch is doing but ... When n>255 you set n=0. Did you intend that ?

I followed the above instructions from the Arduino Reference to break the for loop under a set of intances

How could n be greater than or equal to 255 in this code

for (n=0;n<255;n++){
   analogWrite(led,n);
   if (n>=255){
       n=0;
       var=1;
       break; 
   }
  delay(10);
}

...R

its just one of my many modifications to the code to achieve a desireable result.

This is where im at currently

n serial prints correctly to 255 and then prints HELLO but then repeats the for loop but does not leave (led, HIGH) it can be held high with a delay but i only want the delay for as long as the position is held on the analogue stick

void softstart() {
  
    for (int n = 0; n <=255; n++) {
    analogWrite(led, n);
    Serial.println(n);
    if (n >= 255) {
      var = 1;
      n=0;
      break;
    }
    delay(20);

    }
      if (var == 1) {
      Serial.println("HELLO");
      digitalWrite(led, HIGH);
  }
}

Nimrodxv:
its just one of my many modifications to the code to achieve a desireable result.

It is a waste of your time to make modifications that cannot have any effect.

...R

Very constructive

SOLVED

The sensor value was not being updated during the softstart() and a return state of var was implemented to return the state to 0

Thankyou for your assitance :o

My code for future reference

int led = 9;
int sensorPinx = A0;
int sensorPiny = A1;
int sensorValuex = 0;
int sensorValuey = 0;
int var = 0;
int deadzone = 750;

////////////////////////////////////////////

void softstart() {
  if (var == 0) {
    for (int n = 0; n <= 255; n++) {
      analogWrite(led, n);
      Serial.println(n);
      delay(5);

      if (n >= 255) {
        var = 1;
        do {
          sensorValuex = analogRead(sensorPinx);
          digitalWrite(led, HIGH);
        } while (sensorValuex >= deadzone);
        var = 0;
      }

    }
  }

}
/////////////////////////////////////////////
void driveled() {
  if (sensorValuex > deadzone) {
    softstart();
  }

  else {
    digitalWrite(led, LOW);
  }

}
/////////////////////////////////////////////

void setup() {

  Serial.begin(9600);
  pinMode(led, OUTPUT);



}
void loop() {
  sensorValuex = analogRead(sensorPinx);
  //Serial.println(sensorValuex);
  delay(1);
  driveled();


}
    for (int n = 0; n <= 255; n++) {
      analogWrite(led, n);
      Serial.println(n);
      delay(5);

      if (n >= 255) {

The for loop iterates while n is less than or equal to 255. The if statement checking for n greater than 255 is pointless. n WILL be 255 when the loop ends, so UNCONDITIONALLY move that code in the body of the if statement AFTER the for loop.

Ok so you have this one LED working. But the sketch cannot do anything else. If you want it to have an emergency-stop button or maybe add a LED to show the motor status, you cannot get there from here.

The thing that makes it really difficult to maintain and modify in the future is the input is read in several places. If you change the sensor or anything, you have to track down all those places and modify them all.

Look at this code, for a very large Arduino program that took more than 2 years to write...

void loop() {
  readInputs();
  doCalculations();
  setOutputs();
}

This loop repeats at high speed. Several thousand times per second. It never stops in a for() loop for hundreds of milliseconds. It has too many other things to do while the motor spins up to speed.

Try to re-write your program to this type of layout. The soft-start should look at the milliseconds since the motor started to decide what to send to it. It should not have any delay(). Do it now while your code is small and it won't be so difficult to undo the next week's work when you find you can't do everything you want the old way.

Well spotted! :slight_smile:

This is just a small program for the testing purposes of a selection of bilge pumps (softstarting to avoid large current drains on my voltage regulators). When it comes to the ROV program the whole thing needs to be re-written anyway. The only concern was how to stop the soft start loop and i have learned how to do that with variable data types.