r/programminghelp • u/Digimonlord • 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.
1
May 20 '18
Alternatively, is it possible that if (buttonState) always evalutaes to true when buttonState = digitalRead(buttonPin).
Looking at the reference docs it states digitalRead() returns HIGH or LOW. Does the following evaluate to false (HIGH && LOW)?
Is it possible that this should infact read if (digitalRead(buttonPin) == HIGH) or are they identical?
1
u/Digimonlord May 20 '18
From my limited knowledge of how the Arduino functions, it seems that HIGH is equivalent to true and LOW is the same as false
1
May 20 '18
Ah, ok - my apologies.
When you say the servos are spazzing, is there any pattern? Are all servos acting randomly or is
servoCstaying still etc?1
u/Digimonlord May 20 '18
Servo C is staying still, It is servo D that is moving when the button isnt pressed.
1
May 20 '18
Only
servoDis moving, nothing else e.g.servoE? Is it moving between two places or moving randomly or moving once and then stopping?1
u/Digimonlord May 20 '18
Its moving randomly and then trying to reset to what I believe is pos 90
1
May 20 '18
and
servoEisn't moving at all?1
u/Digimonlord May 20 '18
Servo E moves when I hit the button, as it is supposed to
→ More replies (1)
1
May 20 '18
If I was you, first thing I'd do is get rid of most of the code and have something like the below:
void setup() {
Serial.begin(9600);
}
void loop() {
buttonState = digitalRead(buttonPin);
Serial.println(buttonState);
delay(500);
}And just make sure that the buttonPin isn't floating, returning HIGH or LOW randomly due to issues with perhaps the connection etc. This way you can rule that possibility out and look at other parts of the code.
1
1
u/[deleted] May 20 '18
I haven't had any experience with arduino's but this line looks curious...
delayMicroseconds(2);What's this for? 2 microseconds is an incredibly short time (2 millionths of a second), could it be that your while loop is executing much faster than you expect due to the aforementioned line?