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]