Code control hybrid stepper motor with arduino + 1602 display + push buttons

 #include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);
#define Bt_F A0
#define Bt_S A1
#define Bt_B A2
#define dirPin 2
#define stepPin 3
#define stepsPerRevolution 1000
int Mode=0;
void setup() {
  // Declare pins as output:
  pinMode(stepPin, OUTPUT);
  pinMode(dirPin, OUTPUT);
  pinMode(Bt_F, INPUT_PULLUP);
  pinMode(Bt_S, INPUT_PULLUP);
  pinMode(Bt_B, INPUT_PULLUP);
 
  lcd.begin();
 
}
void loop() {
  if(digitalRead (Bt_F) == 0) {Mode = 1;}
  if(digitalRead (Bt_S) == 0) {Mode = 0;}
  if(digitalRead (Bt_B) == 0) {Mode = 2;}
  lcd.setCursor(0,1);
  if(Mode == 1) {lcd.print(" Forward ");}
  if(Mode == 0) {lcd.print(" Stop ");}
  if(Mode == 2) {lcd.print(" Revers ");}

 if(Mode == 1) {
  // Set the spinning direction clockwise:
  digitalWrite(dirPin, HIGH);
  // Spin the stepper motor 1 revolution slowly:
  for (int i = 0; i < stepsPerRevolution; i++) {
    // These four lines result in 1 step:
    digitalWrite(stepPin, HIGH);
    delayMicroseconds(50);
    digitalWrite(stepPin, LOW);
    delayMicroseconds(50);
  }
 }


 if(Mode == 2) {
  // Set the spinning direction clockwise:
  digitalWrite(dirPin, LOW);
  // Spin the stepper motor 1 revolution slowly:
  for (int i = 0; i < stepsPerRevolution; i++) {
    // These four lines result in 1 step:
    digitalWrite(stepPin, HIGH);
    delayMicroseconds(50);
    digitalWrite(stepPin, LOW);
    delayMicroseconds(50);
  }
 }
}

Comments