# Momentum simulation.

I am trying to make a model Carnival swing ride with either a DC motor or a Stepper motor. Whilst I have found code examples for how to simulate the actual swinging with inertia, I am too new at this to understand how to change the amplitude of the swing arc in steps from stand still to full 70 degrees each side say in 7 steps. Any help would be most appreciated.

The concept should be very simple, but I suspect a lot will depend on the mechanical characteristics.

Logically it is easiest to demonstrate if you imagine using a servo to move the swing (but it may or may not look right).

To get a servo to move from (say) 10deg to 60deg slowly needs a piece of code something like this

byte left = 10; // left extent of swing
byte right = 60; // right extent of swing

``````for (byte n = left; n <= right; n = n + 5) {
myservo.write(n);
delay(20) ; // milliseconds between steps
}
``````

And you would need the equivalent to make it go from 60 back to 10. Those two loops would correspond with a single swing.

If you want the size of the swings to change then you need to incorporate that pair of loops into another loop that alters the values for left and right.

If you are using another sort of motor you would put the code for driving that in place of the servo code.

Have you considered allowing the swing to move freely (as real swings do) and providing energy to it by pulling a “string” through a “spring” - perhaps using a servo. The spring would soften the jerk and a longer “pull” would provide more energy. If the pull is immediately removed (i.e. servo arm up ASAP) the swing will oscillate with gravity.

…R

Robin,
Oops, sorry for delay in replying, got a lot on at present.
I hadn't thought of using a servo as the motor source - must give it a try.

Thanks will get back once I have had a go.

Driving a swinging load on an axle is a case for torque-control rather than velocity control.

For a DC motor that means controlling the motor current, not voltage. (most simple
motor drivers have no support for this).

You apply torque in the right direction at the right times to build up the swing.

For rotation the correct term is moment of inertia (MoI), not momentum, and

angular acceleration = torque / MoI