uint16_t type subtraction

Hey,

i would like to to drive 6 servo motor with arduino mega. I would like to implement a smooth transmission between the old and new values (intead of a sudden step a linear function).

But deltaValue[servonum] vector has strange values, and I dont know why. it should be problem with the uint16_t type?

Could help me somebody?
Thanks!

uint16_t pulselen[2][6] = {{0,0,0,0,0,0},{0,0,0,0,0,0}}; //[2] old values, [1] current values

//....
//compute the PWM values for a servo motor, it works
//....

    long int deltaValue[6];
    int smallSteps=0;
    for (servonum = 0; servonum<6; servonum++){
          
          deltaValue[servonum] = (int) pulselen[2][servonum] - (int)pulselen[1][servonum];
          smallSteps=0;
             while( smallSteps>1 ){
             
              int16_t pwmActual = pulselen[2][servonum] + (deltaValue[servonum] * smallSteps);
              pwm0.setPWM(servoArray[servonum],0,pwmActual);
             
             
              smallSteps=smallSteps+0.1;
            }
         }
smallSteps=0;
             while( smallSteps>1 ){

What do you think the chances are that anything in the while loop will ever execute?

How many elements does this statement define:

uint16_t pulselen[2][6] = {{0,0,0,0,0,0},{0,0,0,0,0,0}}; //[2] old values, [1] current values

Hint: 2. You have defined pulselen[0][n] and pulselen[1][n]

Now look at this statement:

          deltaValue[servonum] = (int) pulselen[2][servonum] - (int)pulselen[1][servonum];

Could something go wrong here?

Your overshooting your array :wink: Try:

 pulselen[1][servonum] - (int)pulselen[0][servonum]

Didn't look any further after that...

Thanks, the overshooting was the problem ...

 int smallSteps=0;
 ...
 smallSteps=smallSteps+0.1;

Very small steps indeed...

@oqibidipo: Whoosh! The sound one hears when something just went over their head. :slight_smile:

oqibidipo:

 int smallSteps=0;

...
smallSteps=smallSteps+0.1;




*Very* small steps indeed...

No worries as there is zero chance that line ever gets executed