# Softstart/softstop code

**URL:** <https://forum.arduino.cc/t/softstart-softstop-code/940320>\
**Category:** Programming\
**Created:** [December 28, 2021, 2:53pm UTC](https://forum.arduino.cc/t/softstart-softstop-code/940320 "2021-12-28T14:53:48Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![trucker0werner](https://avatars.discourse-cdn.com/v4/letter/t/6f9a4e/32.png) [@trucker0werner](https://forum.arduino.cc/u/trucker0werner)\
**Post date:** [December 28, 2021, 2:53pm UTC](https://forum.arduino.cc/t/softstart-softstop-code/940320/1 "2021-12-28T14:53:48Z")

</div>

Busy building code for controlling motorspeed.  
My wish is to run code that can change pwm speed whit softstart and softstop.

 ![image](https://europe1.discourse-cdn.com/arduino/original/4X/b/1/4/b1468ac6a1d6a0c9fdfc101a8a4780b80897345c.png)  
changing speed over time like the straight line is possible by  
speed=50  
newspeed=75  
tempspeed=speed+1  
this loop continues till tempspeed is 75.  
disadvanges of this code is that it takes to long to get there

or like the other line you use  
FOR command ```  
for (initialization; condition; increment)

```cpp
disadvanges of this one it only slowly speed up and its a loop

But i want to get it like red line, (up but also the same downwards)
didn't find a correct code of example for

So is there a good way to get some code to get to there. 
that goes from speed to newspeed
```

---

<div class="post-metadata">

**Author:** ![MicroBahner](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/microbahner/32/238923_2.png) [@MicroBahner](https://forum.arduino.cc/u/MicroBahner)\
**Post date:** [December 28, 2021, 3:04pm UTC](https://forum.arduino.cc/t/softstart-softstop-code/940320/2 "2021-12-28T15:04:32Z")

</div>

> [@trucker0werner](#):
>
> speed=50  
> newspeed=75  
> tempspeed=speed+1  
> this loop continues till tempspeed is 75.  
> disadvanges of this code is that it takes to long to get there

Why should it take too long? It depends on your sketch how often you increase the speed and whether you increase only by 1 or make bigger steps to increase faster.

---

<div class="post-metadata">

**Author:** ![johnwasser](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/johnwasser/32/214270_2.png) [@johnwasser](https://forum.arduino.cc/u/johnwasser)\
**Post date:** [December 28, 2021, 3:05pm UTC](https://forum.arduino.cc/t/softstart-softstop-code/940320/3 "2021-12-28T15:05:44Z")

</div>

So you want to start a move slowly, accelerate to full speed (whatever that is) and then decelerate to a stop. Is the goal to reach a particular destination position? How do you measure your position? Since you mention PWM it appear you are using something like a DC motor. Is that true?

---

<div class="post-metadata">

**Author:** ![wetterstr](https://avatars.discourse-cdn.com/v4/letter/w/f14d63/32.png) [@wetterstr](https://forum.arduino.cc/u/wetterstr)\
**Post date:** [December 30, 2021, 7:57pm UTC](https://forum.arduino.cc/t/softstart-softstop-code/940320/4 "2021-12-30T19:57:22Z")

</div>

Conceptually, you can use a proportional control algorithm, i.e. use the difference between the current value and the target value to increase/decrease the speed increments.

Let Curr be the current speed, Start the beginning speed, Target the target speed, and Delta by how much to increase speed. Increase Delta if Curr \< (Target-Start)/2, and force Delta=0 if Current \>= Target.

Example: Start=0, Target=10, Delta=0  
Curr=Start, (Target-Start)/2=5  
Curr:=0, Diff=10, Delta=1 (Curr\<5)  
Curr= 1, Diff=9, Delta=2 (Curr\<5)  
Curr=3, Diff=7, Delta=3 (Curr\<5)  
Curr=6, Diff=4, Delta=2 (Curr\>5)  
Curr=8, Diff=2, Delta=1 (Curr\>5)  
Cur=9, Diff=1, Delta=1 (Curr\>5)  
Curr=10, Diff=0, Delta=0

This only demonstrates the principle, you may have to deal w ith overshoots by setting Delta to a negative value.

---

<div class="post-metadata">

**Author:** ![JCA34F](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/jca34f/32/340148_2.png) [@JCA34F](https://forum.arduino.cc/u/JCA34F)\
**Post date:** [December 31, 2021, 4:59am UTC](https://forum.arduino.cc/t/softstart-softstop-code/940320/5 "2021-12-31T04:59:48Z")

</div>

Search: "S curve acceleration".

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [June 29, 2022, 5:00am UTC](https://forum.arduino.cc/t/softstart-softstop-code/940320/6 "2022-06-29T05:00:35Z")

</div>

Closed on June 29, 2022, 5:00am UTC

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.
