Vending machine isn't working

Hey,
I only recently just started to code and for my vending machine and I need my 360 degree continuous servo motors to spin for 2 seconds to drop the item by using a button. You should press the button and the motor should spin for 2 seconds and then stop by its self.

Here is my current code.

#include <Servo.h>
#include <ezButton.h>

// constants won't change
const int BUTTON_PIN = 7; // Arduino pin connected to button's pin
const int SERVO_PIN = 9; // Arduino pin connected to servo motor's pin

ezButton button(BUTTON_PIN); // create ezButton object that attach to pin 7;
Servo servo; // create servo object to control a servo

// variables will change:
int angle = 0; // the current angle of servo motor

void setup() {
Serial.begin(9600); // initialize serial
button.setDebounceTime(50); // set debounce time to 50 milliseconds
servo.attach(SERVO_PIN); // attaches the servo on pin 9 to the servo object

servo.write(angle);
}

void loop() {
button.loop(); // MUST call the loop() function first

if(button.isPressed()) {

  servo.writeMicroseconds(2000); // rotate
  delay(2000);

}
}

Code looks ok, but you’ve told the servo to start running, but never tell it to stop.

how do I tell it to stop. what would I add

Try after delay(2000); just add servo.writeMicroseconds(1500); // stop rotate.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.