Running a stepper while a sound is playing

Hey guys,
I’m looking for a coding help for my project. Basically I’m looking for a way to make a sound play while the motor is working. I’m using a teensy and a stepper motor.

I have a stepper motor which rotates clockwise and anti-clockwise in a loop(the loop is about 4 seconds). And I want to play a short sound that I made during each loops. I was thinking of using a teensy audio adaptor and add my sound in a SD card. Would anyone know any sample code I can have for this?

[code]
#include <MultiStepper.h>
#include <AccelStepper.h>

/*
     Basic example code for controlling a stepper without library

     by Dejan, https://howtomechatronics.com
*/

// defines pins
#define stepPin 3
#define dirPin 4

void setup() {
  // Sets the two pins as Outputs
  pinMode(stepPin, OUTPUT);
  pinMode(dirPin, OUTPUT);
}
void loop() {
  digitalWrite(dirPin, HIGH); // Enables the motor to move in a particular direction
  // Makes 200 pulses for making one full cycle rotation
  for (int x = 0; x < 1000; x++) {
    digitalWrite(stepPin, HIGH);
    delayMicroseconds(100);    // by changing this time delay between the steps we can change the rotation speed
    digitalWrite(stepPin, LOW);
    delayMicroseconds(100);
  }
  delay(50); // One second delay

  digitalWrite(dirPin, LOW); //Changes the rotations direction
  // Makes 400 pulses for making two full cycle rotation
  for (int x = 0; x < 1000; x++) {
    digitalWrite(stepPin, HIGH);
    delayMicroseconds(100);
    digitalWrite(stepPin, LOW);
    delayMicroseconds(100);
  }
  delay(50);



  digitalWrite(dirPin, HIGH); // Enables the motor to move in a particular direction
  // Makes 200 pulses for making one full cycle rotation
  for (int x = 0; x < 1000; x++) {
    digitalWrite(stepPin, HIGH);
    delayMicroseconds(100);    // by changing this time delay between the steps we can change the rotation speed
    digitalWrite(stepPin, LOW);
    delayMicroseconds(100);
  }
  delay(50); // One second delay

  digitalWrite(dirPin, LOW); //Changes the rotations direction
  // Makes 400 pulses for making two full cycle rotation
  for (int x = 0; x < 1000; x++) {
    digitalWrite(stepPin, HIGH);
    delayMicroseconds(100);
    digitalWrite(stepPin, LOW);
    delayMicroseconds(100);
  }
  delay(50);


  digitalWrite(dirPin, HIGH); // Enables the motor to move in a particular direction
  // Makes 200 pulses for making one full cycle rotation
  for (int x = 0; x < 3846; x++) {
    digitalWrite(stepPin, HIGH);
    delayMicroseconds(200);    // by changing this time delay between the steps we can change the rotation speed
    digitalWrite(stepPin, LOW);
    delayMicroseconds(200);
  }
  delay(50); // One second delay

  digitalWrite(dirPin, LOW); //Changes the rotations direction
  // Makes 400 pulses for making two full cycle rotation
  for (int x = 0; x < 3846; x++) {
    digitalWrite(stepPin, HIGH);
    delayMicroseconds(300);
    digitalWrite(stepPin, LOW);
    delayMicroseconds(300);
  }
  delay(1000);


  digitalWrite(dirPin, HIGH); // Enables the motor to move in a particular direction
  // Makes 200 pulses for making one full cycle rotation
  for (int x = 0; x < 3846; x++) {
    digitalWrite(stepPin, HIGH);
    delayMicroseconds(200);    // by changing this time delay between the steps we can change the rotation speed
    digitalWrite(stepPin, LOW);
    delayMicroseconds(200);
  }
  delay(50); // One second delay

  digitalWrite(dirPin, LOW); //Changes the rotations direction
  // Makes 400 pulses for making two full cycle rotation
  for (int x = 0; x < 3846; x++) {
    digitalWrite(stepPin, HIGH);
    delayMicroseconds(300);
    digitalWrite(stepPin, LOW);
    delayMicroseconds(300);
  }
  delay(1000);


  digitalWrite(dirPin, HIGH); // Enables the motor to move in a particular direction
  // Makes 200 pulses for making one full cycle rotation
  for (int x = 0; x < 11538; x++) {
    digitalWrite(stepPin, HIGH);
    delayMicroseconds(100);    // by changing this time delay between the steps we can change the rotation speed
    digitalWrite(stepPin, LOW);
    delayMicroseconds(100);
  }
  delay(50); // One second delay

  digitalWrite(dirPin, LOW); //Changes the rotations direction
  // Makes 400 pulses for making two full cycle rotation
  for (int x = 0; x < 3846; x++) {
    digitalWrite(stepPin, HIGH);
    delayMicroseconds(150);
    digitalWrite(stepPin, LOW);
    delayMicroseconds(150);
  }
  delay(1000);


  digitalWrite(dirPin, HIGH); // Enables the motor to move in a particular direction
  // Makes 200 pulses for making one full cycle rotation
  for (int x = 0; x < 23076; x++) {
    digitalWrite(stepPin, HIGH);
    delayMicroseconds(80);    // by changing this time delay between the steps we can change the rotation speed
    digitalWrite(stepPin, LOW);
    delayMicroseconds(80);
  }
  delay(50); // One second delay

  digitalWrite(dirPin, LOW); //Changes the rotations direction
  // Makes 400 pulses for making two full cycle rotation
  for (int x = 0; x < 11538; x++) {
    digitalWrite(stepPin, HIGH);
    delayMicroseconds(100);
    digitalWrite(stepPin, LOW);
    delayMicroseconds(100);
  }
  delay(1000);


  digitalWrite(dirPin, HIGH); // Enables the motor to move in a particular direction
  // Makes 200 pulses for making one full cycle rotation
  for (int x = 0; x < 19230; x++) {
    digitalWrite(stepPin, HIGH);
    delayMicroseconds(70);    // by changing this time delay between the steps we can change the rotation speed
    digitalWrite(stepPin, LOW);
    delayMicroseconds(70);
  }
  delay(50); // One second delay

  digitalWrite(dirPin, LOW); //Changes the rotations direction
  // Makes 400 pulses for making two full cycle rotation
  for (int x = 0; x < 38460; x++) {
    digitalWrite(stepPin, HIGH);
    delayMicroseconds(50);
    digitalWrite(stepPin, LOW);
    delayMicroseconds(50 );
  }
  delay(5000);




}
[/code]

Images of code are nearly worthless. Please post your code properly.

Please read the forum guidelines to see how to properly post code and some information on making a good post.

Use the IDE autoformat tool (ctrl-t or Tools, Auto format) before posting code in code tags.

Use one of the stepper libraries that allow the stepper to be controlled in a non-blocking fashion and in the background. Like the AccelStepper library or the MobaTools stepper library (among others).

1 Like

It would be much better to start the learning process with example code from ONE of the libraries that you included, rather than code that does not use either of those libraries.

If you have only one motor, there is no point in including the MultiStepper library.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.