#include <Stepper.h>
// Define number of steps per revolution
const int stepsPerRevolution = 200; // Change this to fit the number of steps per revolution for your motor
// Define pin connections for the stepper motor
const int motorPin1 = A0; // IN1 on the ULN2003 driver
const int motorPin2 = A1; // IN2 on the ULN2003 driver
const int motorPin3 = A2; // IN3 on the ULN2003 driver
const int motorPin4 = A3; // IN4 on the ULN2003 driver
// Create a Stepper object
Stepper myStepper(stepsPerRevolution, motorPin1, motorPin2, motorPin3, motorPin4);
// Initialize pin pushbutton
const int buttonPin1 = 2;
const int buttonPin2 = 3;
const int buttonPin3 = 4;
// Initialize pin 7-segment
const int segA = 5;
const int segB = 6;
const int segC = 7;
const int segD = 8;
const int segE = 9;
const int segF = 10;
const int segG = 11;
const int segDP = 12;
const int DIG1 = 0; // DIG1 pin for 7-segment
const int DIG2 = 1; // DIG2 pin for 7-segment
// Initialize variables to store button states
int buttonState1 = 0;
int buttonState2 = 0;
int buttonState3 = 0;
int lastButtonState1 = 0;
int lastButtonState2 = 0;
int lastButtonState3 = 0;
// Variable to store current stepper position
int currentPosition = 0;
int currentDisplay = -1; // Variable to store current display number, -1 means off
// Function to reset the display
void resetDisplay() {
digitalWrite(segA, HIGH);
digitalWrite(segB, HIGH);
digitalWrite(segC, HIGH);
digitalWrite(segD, HIGH);
digitalWrite(segE, HIGH);
digitalWrite(segF, HIGH);
digitalWrite(segG, HIGH);
digitalWrite(segDP, HIGH);
}
// Function to display number on 7-segment
void displayNumber(int number) {
resetDisplay(); // Reset all segments to on
// Activate the correct digit
digitalWrite(DIG1, LOW); // Activate the first digit
digitalWrite(DIG2, HIGH); // Deactivate the second digit
// Set segments to display the correct number
switch (number) {
case 1:
digitalWrite(segB, LOW);
digitalWrite(segC, LOW);
break;
case 2:
digitalWrite(segA, LOW);
digitalWrite(segB, LOW);
digitalWrite(segD, LOW);
digitalWrite(segE, LOW);
digitalWrite(segG, LOW);
break;
case 3:
digitalWrite(segA, LOW);
digitalWrite(segB, LOW);
digitalWrite(segC, LOW);
digitalWrite(segD, LOW);
digitalWrite(segG, LOW);
break;
default:
resetDisplay();
break;
}
}
void setup() {
// Set the speed of the stepper motor
myStepper.setSpeed(60);
// Set pin pushbutton as input
pinMode(buttonPin1, INPUT);
pinMode(buttonPin2, INPUT);
pinMode(buttonPin3, INPUT);
// Set pin 7-segment as output
pinMode(segA, OUTPUT);
pinMode(segB, OUTPUT);
pinMode(segC, OUTPUT);
pinMode(segD, OUTPUT);
pinMode(segE, OUTPUT);
pinMode(segF, OUTPUT);
pinMode(segG, OUTPUT);
pinMode(segDP, OUTPUT);
pinMode(DIG1, OUTPUT);
pinMode(DIG2, OUTPUT);
// Initial display (all segments off)
resetDisplay();
}
void loop() {
// Read the pushbutton states
buttonState1 = digitalRead(buttonPin1);
buttonState2 = digitalRead(buttonPin2);
buttonState3 = digitalRead(buttonPin3);
if (buttonState1 == HIGH && lastButtonState1 == LOW) {
if (currentPosition == 1000) {
int stepsToMove = -1000;
for (int i = 0; i < -stepsToMove; i++) {
myStepper.step(-1);
currentPosition--;
if (currentPosition == 500) {
currentDisplay = 2;
displayNumber(currentDisplay); // Update the display at 500
}
}
currentPosition = 0;
currentDisplay = 1;
displayNumber(currentDisplay); // Update the display at 0
delay(200); // debounce delay
} else if (currentPosition == 500) {
int stepsToMove = -500;
myStepper.step(stepsToMove);
currentPosition = 0;
currentDisplay = 1;
displayNumber(currentDisplay); // Update the display
delay(200); // debounce delay
}
}
lastButtonState1 = buttonState1;
if (buttonState2 == HIGH && lastButtonState2 == LOW) {
int stepsToMove = 500 - currentPosition;
myStepper.step(stepsToMove);
currentPosition = 500;
currentDisplay = 2;
displayNumber(currentDisplay); // Update the display
delay(200); // debounce delay
}
lastButtonState2 = buttonState2;
if (buttonState3 == HIGH && lastButtonState3 == LOW) {
if (currentPosition == 0) {
int stepsToMove = 1000;
for (int i = 0; i < stepsToMove; i++) {
myStepper.step(1);
currentPosition++;
if (currentPosition == 500) {
currentDisplay = 2;
displayNumber(currentDisplay); // Update the display at 500
}
}
currentPosition = 1000;
currentDisplay = 3;
displayNumber(currentDisplay); // Update the display at 1000
delay(200); // debounce delay
} else if (currentPosition == 500) {
int stepsToMove = 500;
myStepper.step(stepsToMove);
currentPosition = 1000;
currentDisplay = 3;
displayNumber(currentDisplay); // Update the display
delay(200); // debounce delay
}
}
lastButtonState3 = buttonState3;
}