I have a question about i++ and i+=

I tried to control servo's rotation speed with a library called "VarSpeedServo.h". I set the speed at 50 and it worked out perfectly when I change for(int i= Min; i<=Max; i++) to for(int i= Min; i<=Max; i+=30) (same for j). Can someone tell me why?

Sorry, I'm new to the programming.

Here is the code

#include <VarSpeedServo.h>

VarSpeedServo myservo;
int speed= 50;  

void setup() {
  myservo.attach(8);
}

void loop() {
  const int Max= 150;   
  const int Min= 30;    

  for(int i= Min; i<=Max; i+=30){
    myservo.write(i,speed,true);
  }

  for(int j= Max; j>=Min; j-=150){
    myservo.write(j,speed,true);
  }
}

i++ is the same as i+=1 and increments the value of i by one.
You have just changed the increment to 30, so all that should happen is that the servo runs faster.

Thank you for the explanation.

Sorry, I have one more question.

How do i+=1 and i+=30 affect the speed of servo?
I faced an error (the servo moved very slowly) when I wrote i++ with my code, but It ran perfectly at speed of 50 when I changed to i+=30.

Thank you for the explanation.

I tried to control servo's rotation speed with a library called "VarSpeedServo.h". I set the speed at 50 and it worked out perfectly when I change for(int i= Min; i<=Max; i+=1) to for(int i= Min; i<=Max; i+=30) (same for j).
How do i+=1 and i+=30 affect the speed of servo?
I faced an error (the servo's speed was not 50 and moved very slowly) when I wrote i++ in my code, but It ran perfectly at speed of 50 when I changed to i+=30.

Sorry, I'm new to the programming.

Here is the code

#include <VarSpeedServo.h>

VarSpeedServo myservo;
int speed= 50;  

void setup() {
  myservo.attach(8);
}

void loop() {
  const int Max= 150;   
  const int Min= 30;    

  for(int i= Min; i<=Max; i+=30){
    myservo.write(i,speed,true);
  }

  for(int j= Max; j>=Min; j-=150){
    myservo.write(j,speed,true);
  }
}

Because the for loop completes its work quicker, so it gets to the end in fewer iterations of the loop.

Note, you should give a servo time to actually move, this is normally done using a delay. I can only assume that the VarSpeedServo.h library does this for you.

I have merged your cross-posts @kenobi777.

Cross-posting is against the Arduino forum rules. The reason is that duplicate posts can waste the time of the people trying to help. Someone might spend a lot of time investigating and writing a detailed answer on one topic, without knowing that someone else already did the same in the other topic.

Repeated cross-posting can result in a suspension from the forum.

In the future, please only create one topic for each distinct subject matter. This is basic forum etiquette, as explained in the "How to get the best out of this forum" guide. It contains a lot of other useful information. Please read it.

Thanks in advance for your cooperation.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.