Getting motor to slow down before stop

Hey!

Arduino MEGA + Curtis 1212 Motor control

I have a motor that I can steer back and forth, and program a stop (rotary encoder sits on the motor shaft).
So when I pass the stop (pulse) I programmed, it stops at that position.
So pretty good ???

The problem is that the engine twists when it reaches the right position. Wherever I would like to be able to set it down to a lower one
speed before it stops, so that it becomes a little softer stop and which becomes a little more controlled.
Type that it goes down in speed in two steps, or maybe even three steps would be good. :o

Curtis engine control is controlled by a voltage signal, so 0V is full spot for one holds, 2.5V is still and 5V is full island in the other direction.
I can control the maximum speed with a number from 0 (0V) to 250 (5V), with PWM from the MEGA.

So would need something in the code that says that when it is eg. 45 pulses from programmed position so it lowers speed
before it stops at the position it should stay on. :drooling_face:

The code where the speed of full island forward looks like the following.
("Moving forward slowly" is only for when that button is pressed, and does not affect the programmed stop).

{
    int valfast = digitalRead(HighSpeed1Pin);               // Going forward fast
    int valslow = digitalRead(SlowSpeed1Pin);             // Going forward slow
    if (valfast == HIGH || valslow == HIGH)               // Check if button is pressed
    {
        if(valfast == HIGH)
            analogWrite(controlPin2, 250);                     // 250 är PWM, motor 100%
        else if(valslow == HIGH)
            analogWrite(controlPin2, 200);                     // Custom nr for slower speed

This is the code that stops the engine from being stored

  • 45 and +45 is that it stops the engine when it is 45 pulses away from the stop, this because the engine has a little delay so to get it
    stay exactly where I want it needed. But it even stays too hard. With a controlled and programmed braking I do not believe these
    numbers would be needed.
 {
    if ( UserStopPulseCount != 0 && PulseCount >= UserStopPulseCount - 45 && PulseCount <= UserStopPulseCount + 45 )
    {
      digitalWrite(controlPin1, LOW);
      digitalWrite(controlPin2, LOW);
      if(okgo==0){
      lcd.setCursor(0, 1);
      lcd.print("Users Stop");
      }
      int valA = digitalRead(HighSpeed1Pin); 
      int valB = digitalRead(HighSpeed2Pin);
      while (valA+ valB>0 && okgo==0){
      Serial.println("Users programmed STOP activated");
     // Serial.println("HighSpeed1Pin %3d HighSpeed2Pin %f",HighSpeed1Pin,HighSpeed2Pin);
      delay(100);
      valA = digitalRead(HighSpeed1Pin); 
      valB = digitalRead(HighSpeed2Pin);
      }
      okgo=1;