r/programminghelp • • May 19 '18

Arduino Spazzing

I'm trying to run an arduino robot that can pick up ping pong balls (bc why not), and when I plug the robot into the power source, all the servos are spazzing and not going/staying in the correct position.

Note: Servos A and B are continuous, meanwhile C, D, and E are 180 degree servos.

Code:

 //L293D
#include <Servo.h>

//Motors
const int motorAPin1  = 4;  // Pin 14 of L293D
 const int motorAPin2  = 5;  // Pin 10 of L293D
 const int motorBPin1  = 6;  // Pin 7 of L293D
 const int motorBPin2  = 7;  // Pin 2 of L293D


 //Servos
 Servo servoA;
 Servo servoB;
 Servo servoC;
 Servo servoD;
 Servo servoE;


 //Button
 const int buttonPin = 2;   // the number of the pushbutton pin
 boolean buttonState = false;       // variable for reading the pushbutton status

 boolean clawOpen = true;

 //Variables for Distance Sensor
const int trigPin = 13;
const int echoPin = 12;
long duration;
int distance;


void setup() {

  // put your setup code here, to run once:

  // initialize the pushbutton pin as an input:
  pinMode(buttonPin, INPUT);
  pinMode (A0, OUTPUT);

  //Set motor pins as outputs
  pinMode(motorAPin1, OUTPUT);
  pinMode(motorAPin2, OUTPUT);
  pinMode(motorBPin1, OUTPUT);
  pinMode(motorBPin2, OUTPUT);


  //Setting up the servos

  servoA.attach(3);//Continuous
  servoB.attach(8);//Continuous
  servoC.attach(10);//Middle 180
  servoD.attach(9);//Top 180
  servoE.attach(11);//Top 180


  //Distance Sensor

  pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
  pinMode(echoPin, INPUT); // Sets the echoPin as an Input


  //Setting default position for 180 degree servos

  servoC.write(90);
  servoD.write(90);
  servoE.write(90);

}

void loop() {

  // put your main code here, to run repeatedly:

  // read the state of the pushbutton value:
  buttonState = digitalRead(buttonPin);

  delayMicroseconds(2);
  // Sets the trigPin on HIGH state for 50 milliseconds
  digitalWrite(trigPin, HIGH);
  delay(50);
  digitalWrite(trigPin, LOW);

  // Reads the echoPin, returns the sound wave travel time in microseconds
  duration = pulseIn(echoPin, HIGH);

  // Calculating the distance in centimeters
  distance = (duration / 2) * 29.1;

  if (buttonState) {

    analogWrite (A0, HIGH);
    delay(1000);
    servoA.write(60);
    servoB.write(120);
    delay(500);
    servoA.write(90);
    servoB.write(90);
    delay(500);
    servoA.write(180);
    servoB.write(0);
    delay(500);
    servoA.write(90);
    servoB.write(90);
    servoD.write(135);
    servoE.write(135);

  } 
  else{

    analogWrite (A0, LOW);
    servoA.write(90);
    servoB.write(90);
    servoC.write(90);
    servoD.write(90);
    servoE.write(90);

  }

}

The code isn't finished because I can't get an accurate statement of if what I have so far is finished because of the servos not consistently doing what I want.

4 Upvotes

13 comments sorted by

View all comments

Show parent comments

1

u/Digimonlord May 20 '18

Servo E moves when I hit the button, as it is supposed to

1

u/[deleted] May 20 '18

Your code interacts with servoE and servoD identically. Sounds like it's possible you have either a dodgy servo or dodgy pin.

I'd recommend you try perhaps swapping servoE and servoD around physically by swapping the pins they're attached to and observing what happens. If servoD, now on pin 11, still acts randomly, it is most likely broken. If servoE, now on pin 11, acts randomly, perhaps a dodgy pin or some weird voltage issues.