#include <Arduino.h>
#include "A4988.h"
#include <Bounce2.h>
#define BTN1_PIN 17
#define BTN2_PIN 16
Bounce2::Button BTN1 = Bounce2::Button();
Bounce2::Button BTN2 = Bounce2::Button();
#define MOTOR_STEPS 400//0.9度步进电机 全步400
#define DIR_PIN 2
#define STEP_PIN 1
#define ENABLE_PIN 0 //LOW EN步进电机
A4988 stepper(MOTOR_STEPS, DIR_PIN, STEP_PIN);
void setup() {
Serial.begin(9600);
BTN1.attach(BTN1_PIN,INPUT_PULLUP );
BTN2.attach(BTN2_PIN,INPUT_PULLUP );
pinMode(ENABLE_PIN , OUTPUT);
digitalWrite(ENABLE_PIN, LOW);
stepper.begin(100, 16);//转速 和细分
}
bool OP=false;
void loop() {
BTN1.update();
BTN2.update();
if (BTN1.rose()){
Serial.println("BTN1");
digitalWrite(ENABLE_PIN, LOW);
if (OP==false){
stepper.rotate(360*2);
}
else{
stepper.rotate(-360*2);
}
}
if(BTN2.rose()){
Serial.println("BTN2");
if(digitalRead(ENABLE_PIN)==LOW){
digitalWrite(ENABLE_PIN, HIGH);
}
else{
digitalWrite(ENABLE_PIN, LOW);
}
}
}