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);
}
}