Go Down

Topic: Servo not moving! (Read 420 times) previous topic - next topic

Steven33

Here is my code for a 4 legged robot with an ultra sonic sensor. I have removed the else statement for debugging purposes but I still couldnt figure out my issues. My servo1 will not move after it initiates. When i copy the walking file it works but when i add it into this loop, it wont move servo1. Please help.

Code: [Select]

/* Sweep
  by BARRAGAN <http://barraganstudio.com>
  This example code is in the public domain.

  modified 8 Nov 2013
  by Scott Fitzgerald
  http://www.arduino.cc/en/Tutorial/Sweep
*/

#include <Servo.h>

Servo servo1;// create servo object to control a servo
Servo servo2;
Servo servo3;
Servo servo4;
Servo servo5;
Servo servo6;
Servo servo7;
Servo servo8;


const int pingPin = 12;
long duration, inches;
int pos = 0;// variable to store the servo position

void setup() {

  servo1.attach(0);
  servo1.write(120);
  servo2.write(40);
  servo2.attach(2);
  servo3.write(130);
  servo3.attach(3);
  servo4.write(40);
  servo4.attach(4);
  servo5.write(110);
  servo5.attach(5);
  servo6.write(40);
  servo6.attach(6);
  servo7.write(130);
  servo7.attach(7);
  servo8.write(30);
  servo8.attach(8);
  Serial.begin(9600);
  delay(1000);
}

void loop() {

  distance_calc();
  delay(10);
  if ( inches >= 5 )

  {
    servo1.write(135);
    servo8.write(45);
    servo4.write(55);
    servo5.write(115);
    delay(15);

    for (pos = 90; pos >= 0; pos -= 2.3) {
      servo2.write(pos);
      servo7.write(150 - pos);
      servo3.write(75 + pos);
      servo6.write(90 - pos);
      delay(10);

    }

    servo1.write(120);
    servo8.write(30);
    servo4.write(28);
    servo5.write(93);
    delay(15);


    for (pos = 0; pos <= 90; pos += 2.0) {
      servo2.write(pos);
      servo7.write(150 - pos);
      servo3.write(60 + pos);
      servo6.write(90 - pos);
      delay(10);


    }


  }

  else
  {


  }
}
void distance_calc() {
  pinMode(pingPin, OUTPUT);
  digitalWrite(pingPin, LOW);
  delayMicroseconds(2);
  digitalWrite(pingPin, HIGH);
  delayMicroseconds(5);
  digitalWrite(pingPin, LOW);
  pinMode(pingPin, INPUT);
  duration = pulseIn(pingPin, HIGH);
  inches = microsecondsToInches(duration);

  Serial.print(inches);
  Serial.print("in, ");
  Serial.println();

  delay(10);
}

long microsecondsToInches(long microseconds) {

  return microseconds / 74 / 2;
}

slipstick

Pins 0 and 1 are used by Serial. So it's not a good idea to use them for anything else. Try moving servo1 from pin0 to a different pin and see what happens.

Steve

Steven33

Yep! That was it. Thanks a ton!  :)

Go Up