How to cap rpm acceleration speed on hobby gearmotor

Hello, what im trying to do is when i press the button the gearmotor will start accelerating from 0 to 255 rpm every second by 10 rpm. And after i let the button go the gearmotor will start going down by 10rpm every second until 0.

I cant find a solution for this the closest think i found was changing acceleration with potentiometer but even then it was too fast and vent overboard.

Is there even a solution for this or some way to do this ?

My code:

int pValue = 0;
int dValue;
int buttonPin2State = 0;
int buttonPin4State = 0;

void setup()
{
  pinMode(A0, INPUT);
  pinMode(5, OUTPUT);
  pinMode(6, OUTPUT);
  pinMode(7,OUTPUT);
  pinMode(8, INPUT_PULLUP);
  pinMode(9, INPUT_PULLUP);

}

void loop()
{
  pValue = analogRead(A0);
  dValue=map(pValue,0,1023,0,255);
  buttonPin2State = digitalRead(8);
  buttonPin4State = digitalRead(9);
  if(buttonPin2State == LOW)
  {
    digitalWrite(6,HIGH);
    digitalWrite(7,LOW);
  }
  else if (buttonPin4State == LOW)
  {
    digitalWrite(6,LOW);
    digitalWrite(7,HIGH);
  }
  else
  {
    digitalWrite(6,LOW);
    digitalWrite(7,LOW);
  }
  analogWrite(5,dValue);

}

Is there a linear relationship between the PWM value used to control the speed of the motor and its actual speed ?

If so you could amend the BlinkWithoutDelay example to add to the PWD value every 10 seconds whilst the button is pressed and back down again when it is not pressed

If the relationship is not linear you could still use the BWoD principle and look up the values to be written in an array or possibly by calculating them on the fly if you can write a suitable function

1 Like

You keep referring to acceleration, then discuss varying the “speed” of the motor. Which do you really want to monitor?
Paul

Hello
Please comment your sketch an put resonable names to pin numbers.
And the second step think about a motor control function like motor(int direction) using this enummeration enum{Stop,Left,Right}. At least you need a timer() function, easily derived from the BWoD example as already above mentioned.

How is the Arduino going to sense the motor speed? Without feedback there will be no way to match the exact acceleration curve.

Or did you mean 10/255ths power and 255/255ths power when you said “10 RPM” and “255 RPM”?

Hello
I´ve found a similar sketch in my sketch box and I´ve made some mods with respect to your opening post. The output can be used as control input for a RPM regulation.

const byte motorSpeedPin {5};
const byte motorLeftPin {6};
const byte motorRightPin {7};
const byte cntrlLeftPin {8};
const byte cntrlRightPin {9};
void motor (int motorSpeed) {
  switch (constrain(motorSpeed, -1, 1)) {
    case 0:
      digitalWrite(motorLeftPin, LOW);
      digitalWrite(motorRightPin, LOW);
      break;
    case 1:
      digitalWrite(motorLeftPin, HIGH);
      digitalWrite(motorRightPin, LOW);
      break;
    case -1:
      digitalWrite(motorLeftPin, LOW);
      digitalWrite(motorRightPin, HIGH);
      break;
  }
}
void setup() {
  Serial.begin(9600);
  pinMode(LED_BUILTIN, OUTPUT);
  pinMode(motorSpeedPin, OUTPUT);
  pinMode(motorLeftPin, OUTPUT);
  pinMode(motorRightPin, OUTPUT);
  pinMode(cntrlLeftPin, INPUT_PULLUP);
  pinMode(cntrlRightPin, INPUT_PULLUP);
  Serial.println (F("let´s go"));
}
void loop () {
  digitalWrite (LED_BUILTIN, (millis() / 500) % 2);
  static unsigned long tickTack = 0;
  static int motorSpeed;
  if (millis() - tickTack >= 100) {
    tickTack = millis();
    if (!digitalRead(cntrlLeftPin)) {
      motorSpeed++;
    } else if (digitalRead(motorLeftPin)) motorSpeed--;
    if (!digitalRead(cntrlRightPin)) {
      motorSpeed--;
    } else if (digitalRead(motorRightPin)) motorSpeed++;
    motorSpeed = constrain(motorSpeed, -255, 255);
    motor(motorSpeed);
    analogWrite(motorSpeedPin, abs(motorSpeed));
  }
}
1 Like
    case 1:
      digitalWrite(motorLeftPin,  HIGH);
      digitalWrite(motorRightPin, LOW);
      break;
    case -1:
      digitalWrite(motorRightPin, LOW);
      digitalWrite(motorRightPin, HIGH);
      break;

The -1 case probably should not set the same pin twice.

Thanks for the advice.
The sketch is corrected now.