Close

7th week

A project log for The Drummer

We want to create a remote/automatic drummer, the goal is to simplify the use of the instrument by making it follow a scheme and exec

edgarfebvinedgar.febvin 12/08/2024 at 19:500 Comments

Between sessions we've been working on the motor assembly and we finally succeded the rotation. With a test code that would work on full step mode we achieved our first objective. 

here is the first code that worked: 

// Include the AccelStepper library:
#include "AccelStepper.h"

// Define stepper motor connections and motor interface type. 
// Motor interface type must be set to 1 when using a driver
#define dirPin 2
#define stepPin 3
#define enablePin 4
#define motorInterfaceType 1

// Create a new instance of the AccelStepper class:
AccelStepper stepper = AccelStepper(motorInterfaceType, stepPin, dirPin);

void setup() {
  // Set the Enable pin as an output:
  pinMode(enablePin, OUTPUT);
  
  // Enable the motor driver (active LOW):
  digitalWrite(enablePin, LOW);

  // Set the maximum speed in steps per second:
  stepper.setMaxSpeed(1000);  // Adjust as needed for your motor
}

void loop() {
  // Set the speed in steps per second:
  stepper.setSpeed(1000);  // Adjust for desired speed (max: setMaxSpeed value)
  
  // Run the motor at a constant speed:
  stepper.runSpeed();
}

The objective was then to find if if was possible to stop the rotation using a button. In this case we used the button on the Arduino: it worked but only for a couple of seconds right before rotating again. Here is the code with button :

#include "AccelStepper.h"

// Define stepper motor connections and motor interface type
#define dirPin 2
#define stepPin 3
#define enablePin 4
#define buttonPin 5
#define motorInterfaceType 1

// Create a new instance of the AccelStepper class
AccelStepper stepper = AccelStepper(motorInterfaceType, stepPin, dirPin);

// Variable to track motor state
bool motorRunning = true;

// Variables for button state and debounce
bool lastButtonState = HIGH;   // Previous state of the button
bool currentButtonState = HIGH;
unsigned long lastDebounceTime = 0;
const unsigned long debounceDelay = 50; // Debounce time in milliseconds

void setup() {
  // Set pin modes
  pinMode(enablePin, OUTPUT);
  pinMode(buttonPin, INPUT_PULLUP); // Button with internal pull-up

  // Enable the motor driver (active LOW)
  digitalWrite(enablePin, LOW);

  // Set motor speed limits
  stepper.setMaxSpeed(1000);  // Maximum speed (steps per second)
  stepper.setSpeed(800);      // Constant speed
}

void loop() {
  // Read the button state
  bool reading = digitalRead(buttonPin);

  // Check if the button state has changed (debouncing)
  if (reading != lastButtonState) {
    lastDebounceTime = millis(); // Reset debounce timer
  }

  if ((millis() - lastDebounceTime) > debounceDelay) {
    // If the button state has stabilized and it's LOW (pressed)
    if (reading == LOW && lastButtonState == HIGH) {
      motorRunning = !motorRunning; // Toggle motor running state
      digitalWrite(enablePin, motorRunning ? LOW : HIGH); // Enable/disable motor
    }
  }

  lastButtonState = reading; // Update last button state

  // Run the motor only if motorRunning is true
  if (motorRunning) {
    stepper.runSpeed();
  }
}

Discussions