How to get a Sparkfun Servo Motor to Turn both Backwards and Forwards

edited November 2017 in Arduino

I am working on a maze robot project and I am at the stage where I am testing out my servo motors to make sure they work properly. I am using a Sparkfun ArduMoto Motor Driver Shield kit, which includes two servo motors that I will use for my wheels. The program is not complete yet. I do not have commands to turn left and right, my if statements, and more coding for my 3 ultrasonic sensors, but with the coding I have, I cannot get the motors to turn backwards, only forwards. I tried switching the HIGH and LOW commands for the digitalWrite partsin my Void statements, but that did not work. However, the speed difference from a variable of 100 to 255 in my void loop statement worked. So, it is reading and looping through the code properly, but the direction will not change. If anyone can see a problem or a solution to my coding I would appreciate it.

Attached is my code so far. (without turn left and right, my if statements, and more coding for my 3 ultrasonic sensors which are not quite needed yet at this point.)

const int sens_1_echoPin = 4; const int sens_1_trigPin = 5; const int sens_2_echoPin = 6; const int sens_2_trigPin = 7; const int sens_3_echoPin = 8; const int sens_3_trigPin = 9;

int m1direction = 12; int m2direction = 13; int motor1Speed = 3; int motor2Speed = 11;

float distance_sens_1; float distance_sens_2; float distance_sens_3; long duration1; long duration2; long duration3;

void setup() {

pinMode(sens_1_echoPin, INPUT); pinMode(sens_1_trigPin, OUTPUT); pinMode(sens_2_echoPin, INPUT); pinMode(sens_2_trigPin, OUTPUT); pinMode(sens_3_echoPin, INPUT); pinMode(sens_3_trigPin, OUTPUT);

pinMode(m1direction, OUTPUT); pinMode(m2direction, OUTPUT); pinMode(motor1Speed, OUTPUT); pinMode(motor2Speed, OUTPUT);

Serial.begin (9600); }

void goForward (int duration, int pwm) { digitalWrite (m1direction, HIGH); digitalWrite (m2direction, HIGH); analogWrite (motor1Speed, pwm); analogWrite (motor2Speed, pwm); delay(duration); analogWrite (motor1Speed, 0); analogWrite (motor2Speed, 0); }

void goBackward (int duration, int pwm) { digitalWrite (m1direction, LOW); digitalWrite (m2direction, LOW); analogWrite (motor1Speed, pwm); analogWrite (motor2Speed, pwm); delay (duration); analogWrite (motor1Speed, 0); analogWrite (motor2Speed, 0); }

void loop() { digitalWrite; goForward (5000, 100); delay (1000); digitalWrite; goBackward (5000, 255); delay (1000);

Tagged:

Answers

Sign In or Register to comment.