Infinite loop while using the Ramp.h

I ran into a bug where my ramp some time get stuck in infinite. I can't replicate the bug consistently, it just happen randomly and I'm not sure how to fix this bug from.

void Move_Arm_RAMP(int degrees_A0, int degrees_A1, int degrees_A2, int degrees_A3, int degrees_A4){ 
  int In_time=1000;
  uint8_t Ramping=1;
  uint16_t pulseLength_A0 = map(degrees_A0, 0, 180, Mininum_Pulse, Max_Pulse);
  uint16_t pulseLength_A1 = map(degrees_A1, 0, 180, Mininum_Pulse, Max_Pulse);
  uint16_t pulseLength_A2 = map(degrees_A2, 0, 180, Mininum_Pulse, Max_Pulse);
  uint16_t pulseLength_A3 = map(degrees_A3, 0, 180, Mininum_Pulse, Max_Pulse);
  uint16_t pulseLength_A4 = map(degrees_A4, 0, 180, Mininum_Pulse, Max_Pulse);
  /////////////////////
  uint16_t pulseLength_P_A0 = map(Axis_angle[0], 0, 180, Mininum_Pulse, Max_Pulse);
  uint16_t pulseLength_P_A1 = map(Axis_angle[1], 0, 180, Mininum_Pulse, Max_Pulse);
  uint16_t pulseLength_P_A2 = map(Axis_angle[2], 0, 180, Mininum_Pulse, Max_Pulse);
  uint16_t pulseLength_P_A3 = map(Axis_angle[3], 0, 180, Mininum_Pulse, Max_Pulse);
  uint16_t pulseLength_P_A4 = map(Axis_angle[4], 0, 180, Mininum_Pulse, Max_Pulse);

  RampA0.go(pulseLength_P_A0);  // set value to directly to 5000
  RampA1.go(pulseLength_P_A1);
  RampA2.go(pulseLength_P_A2);
  RampA3.go(pulseLength_P_A3);
  RampA4.go(pulseLength_P_A4);
  RampA0.go(pulseLength_A0, In_time, LINEAR, LOOPFORWARD);
  RampA1.go(pulseLength_A1, In_time, LINEAR, LOOPFORWARD);
  RampA2.go(pulseLength_A2, In_time, LINEAR, LOOPFORWARD);
  RampA3.go(pulseLength_A3, In_time, LINEAR, LOOPFORWARD);
  RampA4.go(pulseLength_A4, In_time, LINEAR, LOOPFORWARD);  
  /////////////////////
  Axis_angle[0]=degrees_A0;
  Axis_angle[1]=degrees_A1;
  Axis_angle[2]=degrees_A2;
  Axis_angle[3]=degrees_A3;
  Axis_angle[4]=degrees_A4;
  
  while(Ramping){
    pwm.setPWM(Axis[0], 0, RampA0.update());
    pwm.setPWM(Axis[1], 0, RampA1.update());
    pwm.setPWM(Axis[2], 0, RampA2.update());
    pwm.setPWM(Axis[3], 0, RampA3.update());
    pwm.setPWM(Axis[4], 0, RampA4.update());
    if(RampA0.getValue()==pulseLength_A0 && RampA1.getValue()==pulseLength_A1 && RampA2.getValue()==pulseLength_A2 && RampA3.getValue()==pulseLength_A3 && RampA4.getValue()==pulseLength_A4){
      Ramping=0;
    }
    time(5);
  }

}

sorry if my code look bad, I'm still learning

The problem is probably here:

The getValue() function sometimes can return an answer with a small error of 1-2 units.
But if any of these conditions are not met exactly, the program will end up in an infinite loop.

1 Like

ah I see, thanks.
Is there any way to improve that small error?

I haven't a much experience with Ramps...
I think you could compare the values not to exact match.

1 Like

OK so give the conditions some leeway