acceleration and deceleration a motor with pwm

hi, I can't run the motor with acceleration (smooth speed at start) and decelerate (smooth speed when I was it stopped).. all that when I push a button (start when I push button and stop when I release it)
i have used a pwm modulation and the acceleration is given by a constant k(that constant increases until n time steps which give by Ta/T. where Ta is the time of speed increases from 0 to max speed give by duty cycle and T is 1/f , f is the pwm frequency)
this is the sketch.... and the acceleration don't run.... when I upload this sketch in Arduino... the speed is the speed is instantaneous when I push the button..help me ..I'm going crazy :cry:

int MOT = 13; //Il MOTORE è connesso al pin 12
int PULS = 2; //Il pulsante è connesso al pin 2
int val = 0;
int f=200; // espresso in Hrtz
int T=1000/f;
int D=30; //D espresso in %
int Ta = 41000;
int n=Ta/T;
int k=0;
int ton = D
Tk/100;
int toff = T-ton;
void setup() {
pinMode(MOT, OUTPUT); //Il pin del MOTORE è un'uscita
pinMode(PULS, INPUT); //Il pin del pulsante è un'entrata
}
void loop() {
val = digitalRead(PULS); //Lettura del pulsante
if (k==n){goto label1;}
else{
k=k+1;
ton=D
k*T/n;
toff=T-ton;
}
label1:

if (val == 1) { //Se il valore del pulsante è 1

digitalWrite(MOT, HIGH);
delay(ton);
digitalWrite(MOT,LOW);
delay(toff);
}
else { //Altrimenti:
digitalWrite(MOT, LOW); //Spegni il MOTORE
}
}

OMG goto. That's probably a clue!

Various issues immediately spring to mind, you have a granularity of 1ms with delay, and T=5, so there are
only 6 steps possible in the range.

And most motor control circuits are not linear - for this you require synchronous rectification mode, not
fast-decay or slow-decay or mixed decay.

What is val for? It doesn't stop k increasing upto n while the motor's off.

BTW rewrite this:

  if (k==n){goto label1;}
else{
      k=k+1;
    ton=D*k*T/n;
 toff=T-ton;
 }
label1:

as the much much clearer:

  if (k != n)
  {
    k++;
    ton = D*k*T/n;
    toff = T-ton;
  }

You might want to double check than ton is never larger than T.

MarkT:
OMG goto. That's probably a clue!

Various issues immediately spring to mind, you have a granularity of 1ms with delay, and T=5, so there are
only 6 steps possible in the range.

And most motor control circuits are not linear - for this you require synchronous rectification mode, not
fast-decay or slow-decay or mixed decay.

What is val for? It doesn't stop k increasing upto n while the motor's off.

BTW rewrite this:

  if (k==n){goto label1;}

else{
     k=k+1;
   ton=DkT/n;
toff=T-ton;
}
label1:



as the much much clearer:


if (k != n)
 {
   k++;
   ton = DkT/n;
   toff = T-ton;
 }




You might want to double check than ton is never larger than T.

don't run.. :sweat_smile: :sweat_smile: :sweat_smile: this is real mistake.. :cold_sweat:
but I can't use goto?

Correct. Do not use goto.
In my classes at the U. of Akron, it was an automatic fail if you used goto.
That is how strongly the coding community feels about the perils of using goto.
Just don't.

Nothing to do with feeling, goto code is unintelligible and full of bugs and hard to optimize or reason about
and lacks referential transparity. We know its bad news.

However automatically generated code from a state-machine description often uses gotos - a special case
where human fallibility is excluded and there is a natural mapping from state-transition diagram to goto
code.