u/EnglandUndead1

How do I get the DC motor to start after the servo finishes it's part?

Hey, I'm currently working on a rover which needs to push cubes off. I've got most of the code to work. My only issue is that after the servo pushes the block then returns to its original position I need the DC motor to start again but it just gets stuck repeating that section of the code. Is there anyway for me to fix this? Any help or advice would be greatly appreciated.

This is the code I have:

#include <Servo.h>

Servo myservo; // create servo object to control a servo

// twelve servo objects can be created on most boards

int led=5;

int buzzer=6;

const int buttonPin = 7;

int buttonState = 0; // variable for reading the pushbutton status

int pos = 0; // variable to store the servo position

int in1=9;

int in2=10;

// the setup function runs once when you press reset or power the board

void setup() {

// initialize digital pin LED_BUILTIN as an output.

pinMode(led, OUTPUT);

pinMode(buzzer, OUTPUT);

pinMode(buttonPin, INPUT);

pinMode(in1, OUTPUT);

pinMode(in2, OUTPUT);

myservo.attach(8); // attaches the servo on pin 9 to the servo object

}

// the loop function runs over and over again forever

void loop() {

digitalWrite(in1, HIGH);

digitalWrite(in2, LOW);

buttonState = digitalRead(buttonPin);

if (buttonState == LOW) {

//Motor stops when object detected

digitalWrite(in1, HIGH);

digitalWrite(in2, HIGH);

// turn LED on:

digitalWrite(led, HIGH);

delay(1000); // wait for a second

digitalWrite(led, LOW); // turn the LED off by making the voltage LOW

delay(1000); // wait for a second

digitalWrite(buzzer, HIGH);

delay(1000);

digitalWrite(buzzer, LOW);

delay(1000);

for (pos = 0; pos <= 180 ; pos += 1) {

myservo.write(pos);

delay(5);

}

delay(1000);

for (pos = 180; pos>= 0; pos -= 1) {

myservo.write(pos);

delay(5);

}

delay(1000);

digitalWrite(in1, HIGH);

digitalWrite(in2, LOW);

}

else {

digitalWrite(in1, HIGH);

digitalWrite(in2, LOW);

digitalWrite(led, LOW);

digitalWrite(buzzer, LOW);

}

}

reddit.com
u/EnglandUndead1 — 23 hours ago