Close

9th 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/18/2024 at 20:300 Comments

after some test with the non controlled version we decided to add a potentiometer to the assembly, the objective was to control the step of rotation of the rod attached to the motor. For this we used a mapping function that alignes the initial speed of the motor, considering the minimum and maximum values, with the different values of the potentiometer. Here is the code that we used for this mode.

#include "AccelStepper.h"

// Define stepper motor connections and motor interface type:
#define dirPin 2
#define stepPin 3
#define enablePin 4
#define ms1Pin 5
#define ms2Pin 6

#define ms3Pin 7
#define potPin A2 // Potentiometer pin
#define motorInterfaceType 1 // Step/direction control

AccelStepper stepper(motorInterfaceType, stepPin, dirPin);

void setup() {
  // Set the Enable pin and microstepping pins as outputs:
  pinMode(enablePin, OUTPUT);
  pinMode(ms1Pin, OUTPUT);
  pinMode(ms2Pin, OUTPUT);
  pinMode(ms3Pin, OUTPUT);
    // Enable the motor driver (active LOW):
  digitalWrite(enablePin, LOW);
    // Set the A4988 to 1/4-step mode:
  digitalWrite(ms1Pin, HIGH);
  digitalWrite(ms2Pin, HIGH);
  digitalWrite(ms3Pin, LOW);

  // Initialize the stepper motor with max acceleration:
  stepper.setAcceleration(6000); // Set high acceleration (steps/sec²)
}

void loop() {
  static bool movingForward = true;

  // Read the potentiometer value (0-2026):
  int potValue = analogRead(potPin);
    // Map the potentiometer value to a speed range (50 to 12000 steps/sec):
  int minSpeed = 50;    // Minimum motor speed
  int maxSpeed = 12000; // Maximum motor speed
  int motorSpeed = map(potValue, 0, 2026, minSpeed, maxSpeed);
    // Constrain the motor speed to ensure it stays within valid limits:
  motorSpeed = constrain(motorSpeed, minSpeed, maxSpeed);

  // Set the motor speed:
  stepper.setMaxSpeed(motorSpeed);

  // Move motor back and forth between 0 and 20 steps:
  if (movingForward) {
    stepper.moveTo(20); // Move 20 steps forward
  } else {
    stepper.moveTo(0);  // Move back to starting position
  }

  // Run the motor to the target position:
  stepper.run();

  // Check if the motor reached the target position:
  if (stepper.distanceToGo() == 0) {
    movingForward = !movingForward; // Reverse direction
  }
}

Also here is the final code that we used but we had to decrease the maximum speed from 30000 to 12000 because of the amount of torque in the final product (see further logs)

here are some images of the assembly with the potentiometer:

To plug is to the Arduino we had to use a breadboard because the 5v pin was already used.

See the video in the files  please.

Discussions