Why is this randomising code not working?

So I am making a randomising code for two servos for my target practice project. The randomising code works, as the serial data outputs a random string of 1 and 2. But only one servo is turning.

#include <Servo.h>

Servo myservo;

Servo myservo2;

long randNumber;

int pos = 0;
int pos2 = 0;

void setup()
{
   myservo.attach(9);
   myservo2.attach(10);
   randomSeed(analogRead(0));
   Serial.begin(9600);
}

void loop()
{
   randNumber = random(1,3);
   Serial.println(randNumber);
   if (randNumber == 1)
   {
      for(pos = 0; pos < 170; pos += 1)
      {
         myservo.write(pos);
         delay(15);
      }
      delay(1000);
     for(pos = 170; pos>=1; pos-=1)
     {
        myservo.write(pos);
        delay(15);
     }
   }
   if (randNumber == 2)
   {
      for(pos2 = 0; pos2 < 170; pos2 +=1)
      {
         myservo2.write(pos2);
         delay(15);
      }
      delay(1000);
      for (pos2 =0; pos2 >=1; pos2 -=1)
      {
         myservo2.write(pos2);
         delay(15);
      }
   }
}

Moderator edit:
</mark> <mark>[code]</mark> <mark>

</mark> <mark>[/code]</mark> <mark>
tags added.

The problem is not with your randomizing code, but rather with your servo-moving code.

What should I do?

robtherobot:
What should I do?

Swap the connections to the servos. Does the problem follow the servo or the pin ?

Put print statements in the servo moving code so that you know that it is being called

How are the servos powered ?

I have already swapped the pins and nothing happened and it is powered by gnd and 5v

      for (pos2 =0; pos2 >=1; pos2 -=1)

What do you think this will do?

Steve

It looks like it won’t work because I have a 180 degree servo and that is saying the servo to go bellow 0 degrees.

robtherobot:
It looks like it won't work because I have a 180 degree servo and that is saying the servo to go bellow 0 degrees.

Have another guess

I think it tells the servo to go down by one degree until pos2 is not bigger than one?

Yes but that won't happen because pos2 is set to 0 when the loop starts :slight_smile:

So do I change it to one?

I suggest 170 as in the other loops, unless that’s not what you want to do…

The original code has 170 degrees

So, what's the problem?

it is powered by gnd and 5v

That is a real problem. Power the servos with a separate power supply and connect the grounds, or you will destroy the Arduino.

You mean a battery or something? Just I have two 5V pins on my arduino.

I have two 5V pins on my arduino.

What is their current rating?
What is the current rating of the servo?

What do you mean by rating?

robtherobot:
What do you mean by rating?

There are documented limits for how much current you can get from an Arduino pin without damaging it. This rating/limit is often considerably less than some servos will try to take.

Steve

robtherobot:
What do you mean by rating?

There are documented limits for how much current you can get from an Arduino pin without damaging it. This rating/limit is often considerably less than some servos will try to take.

That's why you should use a separate supply for the servo(s). And yes that can be and very often is a battery.

Steve