Looping lines of code

Hi! I'm very new to Arduino. I was wondering if I could get and help with looping certain lines of code. My project is a robotic arm. I have thes
e 3 lines of code:

servo.write(servoPos);
servoPos = servoPos - 1;
delay(30);

Basically, it just tells the servo to move one degree. This is just so its slowed and doesn't flip the whole robot. The problem is that I have to repeat these 3 lines of code 45 times to get to the desired angle. My main question is if there is a simpler why to repeat this 45 times, instead of using 135 lines of code, for 1 servo. I have to do it like 3 more times. Feel free to criticize my code, it would even help if you did.

Look into the for( ) loop

Doing stuff repetitively is a favorite among computers, including Arduinos.

You can use the for construction for that.

Check this up: https://www.arduino.cc/reference/en/language/structure/control-structure/for/ and give it a try

I'll try it out

It worked, thanks!

1 Like

Here's a loop thing with a servo thing...

here's one without looping, stepping instead..


#include <Servo.h>

Servo servo;
int ServoPos = 180;
int DesiredPos = 0;
unsigned long lastStep = 0;
int intervalServo = 30;


void setup() {
  servo.attach(9);
  //move to 180..
  servo.write(ServoPos);
  //set our new desired position..
  DesiredPos = ServoPos - 45;
}

void loop() {
  //step to our new postion..
  ServoStep();

}

void ServoStep() {
  //timer check
  if (millis() - lastStep >= intervalServo) {
    lastStep = millis();
    //if we're not there yet..
    if (ServoPos != DesiredPos) {
      servo.write(ServoPos);//step
      if (DesiredPos<ServoPos)
      ServoPos--;//subtract 1
      else
      ServoPos++;
    }
  }
}

Sim here..

Yes! Thsi is exactly what I need! Tysm!

So I made this small bit of code, it works smother that just copy-pasting the same 3 lines over and over again:

void servo(){
  read = digitalRead(2);
  
  if (0 == read){
    for (servoPos = 90; 0 == read; 45){
      servo.write(servoPos);
      servoPos--;
      delay(30);
    }
  }
}

Let me know if I can improve this in any way

That loop will:

  • move the servo by degrees from 90 to 0.

  • continue sending servo commands that are ignored until

  • servoPos eventually (~33 minutes if servoPos is an int) wrap around at which point

  • the servo will fly over to 180, where it will begin crawling to 0

Rinse and repeat. Forever.

You need to take (another) look at how the for statement is meant to be used.

a7

1 Like

does not compile..
function name servo() is the same as name of your servo..
and did you notice how read changes color, use a different name for you variable, like myRead..

but yes, if it did compile, would not do what you want..

a for loop is good when you know exactly how many iterations (steps) you need..
a while loop is good when you don't know how many iterations but know the final goal..
and don't forget, you're already in a big loop called "loop()"..

a for would look like this..

//iterate 45 times..
for (int i = 0;  i < 45;  i++){
      servo.write(servoPos);
      servoPos--;
      delay(30);
    }

a while loop like this..

//iterate till destination is reached..
while (servoPos != destinationPos)
{
      servo.write(servoPos);
      if (servoPos>destinationPos) {servoPos--;} else{ servoPos++;}
      delay(30);
    }

and finally, using delay can be bad as it will hold up your main "loop()" unit it finishes..
the more delays you use the harder it will be to not use them..
this is why I showed the asynchronous (stepping) approach at first, it would be better if you can grasp it..

hope this helps..
~q

I will try my hardest, all of you would know 1000x better than I would.

I have this now, and added a switch instead of adding it later in the project.

#include <Servo.h>

//--------------------------------------------------------------------------------------------------------------------------
Servo servo;
int read;
//--------------------------------------------------------------------------------------------------------------------------

void setup() {
  servo.attach(13);

  pinMode(2, INPUT_PULLUP);
  
  servo.write(90);
}

//--------------------------------------------------------------------------------------------------------------------------------------------------------

void loop(){
  read = digitalRead(2);
  
  if (0 == read){
    for (int i = 90; i >= 45; i--){
      servo.write(i);
      delay(50);
    }
// This makes it not return to 90.
    for (int i = 45; i <= 45; i = 45){
      servo.write(i);
    }
  }
}

That's just fancy talk for "die".

An infinite loop, they call it.

Same effect as

  servo.write(45);

hang :
  goto hang;

Nothing worth observing will happen after it hits that.

a7

1 Like

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