#include <Stepper.h>
#include <TM1637Display.h>
const int stepsPerRevolution = 200; // Change this to fit the number of steps per revolution for your motor
const int switchPin = 2; // Define the pin connected to the switch (adjust as needed)
// Initialize the stepper library on pins 2 through 4
Stepper myStepper(stepsPerRevolution, 2, 4);
// Define the CLK and DIO pins for the TM1637 display
const int CLK = 7; // Clock pin
const int DIO = 8; // Data pin
TM1637Display display(CLK, DIO);
void setup() {
pinMode(switchPin, INPUT_PULLUP); // Configure the switch pin as input with internal pull-up resistor
display.setBrightness(0x0f); // Set display brightness (0x0 to 0x0f)
}
void loop() {
// Read the sensor value
int sensorReading = analogRead(A2);
// Map it to a range from 0 to 100
int motorSpeed = map(sensorReading, 0, 1023, 0, 100);
// Display the speed percentage on the 4-digit display
display.showNumberDec(motorSpeed, false); // Display number, false means no leading zeros
// Read the switch state
int switchState = digitalRead(switchPin);
// Set the motor direction based on the switch state
if (switchState == LOW) {
myStepper.step(stepsPerRevolution / 100); // Clockwise
} else {
myStepper.step(-stepsPerRevolution / 100); // Counter-clockwise
}
// Set the motor speed and step
if (motorSpeed > 0) {
myStepper.setSpeed(motorSpeed);
}
delay(100); // Optional delay to reduce flickering and update rate
}
i am using this code to control a steper motor using a potentiometer and displaying the speed percentage on an 4-digit display but i seems like the arduino "el lego uno " that i am using can't multitask if the speed is dispalyed the motor is not moving(wich is the case in this code" but if i am using the the following code (without display) the motor move in both directions perfectly and i really want to accomplich both tasks controlling the stepper motor and display the speed on the 4-digit screen `#include <Stepper.h>
const int stepsPerRevolution = 200; // Change this to fit the number of steps per revolution for your motor
const int switchPin = 2; // Define the pin connected to the switch (adjust as needed)
// Initialize the stepper library on pins 2 through 4
Stepper myStepper(stepsPerRevolution, 2, 4);
void setup() {
pinMode(switchPin, INPUT_PULLUP); // Configure the switch pin as input with internal pull-up resistor
}
void loop() {
// Read the sensor value
int sensorReading = analogRead(A2);
// Map it to a range from 0 to 100
int motorSpeed = map(sensorReading, 0, 1023, 0, 100);
// Read the switch state
int switchState = digitalRead(switchPin);
// Set the motor direction based on the switch state
if (switchState == LOW) {
myStepper.step(stepsPerRevolution / 100); // Clockwise
} else {
myStepper.step(-stepsPerRevolution / 100); // Counter-clockwise
}
// Set the motor speed and step
if (motorSpeed > 0) {
myStepper.setSpeed(motorSpeed);
}
}`