int motorPin1 = 4; // MSI1
int motorPinStep = 3; // Step
int motorPin2 = 2; // Direction
int enablePin = 9; // Enable
int buttonPin = 11; // Input Button
int buttonState = 0;
int lastButtonState = 0;
void setup() {
// Initialize motor control pins as outputs
pinMode(motorPin1, OUTPUT);
pinMode(motorPin2, OUTPUT);
pinMode(enablePin, OUTPUT);
// Initialize button pin as input
pinMode(buttonPin, INPUT_PULLUP);
// Read the initial state of the button
lastButtonState = digitalRead(buttonPin);
}
void loop() {
// Read the state of the button
buttonState = digitalRead(buttonPin);
// Check if the button is pressed
if (buttonState == LOW) {
// Activate the motor
digitalWrite(motorPin1, HIGH);
// digitalWrite(motorPin2, LOW);
analogWrite(enablePin, 255); // Set motor speed to maximum
} else {
// Deactivate the motor
digitalWrite(motorPin1, LOW);
// digitalWrite(motorPin2, LOW);
analogWrite(enablePin, 0); // Set motor speed to 0
}
// Store the last button state
lastButtonState = buttonState;
}