Huray! Today I managed to make a stepper motor move, when a specific audio frequency in a audiosignal reaches a specific level. //
See a video here: https://www.youtube.com/watch?v=u0zMcRt … e=youtu.be
I added a stepper motor code into my previous code. I declared two Variables BAND and LEVEL.
With BAND i will tell the stepper shield, which frequency band to read.
With LEVEL i tell the stepper shield, what level in that specific frequency band must be reached, until the loop starts.
If the LEVEL reaches a value under LEVEL, then the stepper will stop.
Find the new code here:
/******************************************************************************
Sketch by Alex Rex
based on:
SparkFun Spectrum Shield PWM Demo
Toni Klopfenstein @ SparkFun Electronics
February 2015
https://github.com/sparkfun/Spectrum_Shield
*********************************************************************************/
//Declare Spectrum Shield pin connections
#define STROBE 4 /* #define constantName value */
#define RESET 5 /* #define constantName value */
#define DC_One A0 /* Analog Pin A0 with the name DC_one */
#define DC_Two A1 /* Analog Pin A1 with the name DC_Two */
//Define LED connections on the shield
int LED[] = {3, 6, 7, 9, 10, 11, 12}; /* for the seven LED's */
//Define spectrum variables
int freq_amp; /* variable for the 7 Frequency-Bands of the MSGEQ7 */
int Frequencies_One[7];
int Frequencies_Two[7];
int i;
int level = 125; /*minimum level of sound to trigger LED or STEPPER*/
/* ~110 for piano songs, 175 - 340 for pop-songs (songs with a beat)*/
int band = 3; /*7 frequency bands, choose: 0,1,2,3,4,5,6*/
/*0 = ~63Hz , 1 = ~160Hz , 2 = ~400Hz , 3 = ~1000Hz , 4 = ~2500Hz , 5 = 6250Hz , 6 = 16000Hz */
//Stepper Motor Shield pin connections
#define STEPPER_PIN_1 0 /*IN1*/
#define STEPPER_PIN_2 1 /*IN2*/
#define STEPPER_PIN_3 2 /*IN3*/
#define STEPPER_PIN_4 8 /*IN4*/
int step_number = 0;
/********************Setup Loop*************************/
void setup() {
//Set LED pin configurations
for(i=0; i<7; i++)
{
pinMode(LED[i], OUTPUT);
digitalWrite(LED[i], LOW);
}
//Set Spectrum Shield pin configurations
pinMode(STROBE, OUTPUT);
pinMode(RESET, OUTPUT);
pinMode(DC_One, INPUT); /* Input from MSGEQ7 to arduino */
pinMode(DC_Two, INPUT); /* Input from MSGEQ7 to arduino */
digitalWrite(STROBE, HIGH);
digitalWrite(RESET, HIGH);
//Initialize Spectrum Analyzers
digitalWrite(STROBE, LOW);
delay(1);
digitalWrite(RESET, HIGH);
delay(1);
digitalWrite(STROBE, HIGH);
delay(1);
digitalWrite(STROBE, LOW);
delay(1);
digitalWrite(RESET, LOW);
//Set Stepper Shield pin configurations
pinMode(STEPPER_PIN_1, OUTPUT);
pinMode(STEPPER_PIN_2, OUTPUT);
pinMode(STEPPER_PIN_3, OUTPUT);
pinMode(STEPPER_PIN_4, OUTPUT);
}
/**************************Main Function Loop*****************************/
void loop() {
Read_Frequencies();
Graph_Frequencies();
delay(25);
OneStep(false);
delay(2);
}
/*******************Pull frequencies from Spectrum Shield********************/
void Read_Frequencies(){ /* Read volume value for each band */
for (freq_amp = 0; freq_amp<7; freq_amp++)
{
Frequencies_One[freq_amp] = analogRead(DC_One);
Frequencies_Two[freq_amp] = analogRead(DC_Two);
digitalWrite(STROBE, HIGH);
digitalWrite(STROBE, LOW);
}
}
/*******************Light LEDs based on Frequencies Output Value*************/
void Graph_Frequencies(){
for( i= 0; i<7; i++)
{
if(Frequencies_One[i] >= level)
{
digitalWrite(LED[i], HIGH);
}
else{
digitalWrite(LED[i], LOW);
}
}
}
/**************Stepper Motor Moving to Level Value of one Band from EQ*************/
void OneStep(bool dir){
if(Frequencies_One[band] >= level){
switch(step_number){
case 0:
digitalWrite(STEPPER_PIN_1, HIGH);
digitalWrite(STEPPER_PIN_2, LOW);
digitalWrite(STEPPER_PIN_3, LOW);
digitalWrite(STEPPER_PIN_4, LOW);
break;
case 1:
digitalWrite(STEPPER_PIN_1, LOW);
digitalWrite(STEPPER_PIN_2, HIGH);
digitalWrite(STEPPER_PIN_3, LOW);
digitalWrite(STEPPER_PIN_4, LOW);
break;
case 2:
digitalWrite(STEPPER_PIN_1, LOW);
digitalWrite(STEPPER_PIN_2, LOW);
digitalWrite(STEPPER_PIN_3, HIGH);
digitalWrite(STEPPER_PIN_4, LOW);
break;
case 3:
digitalWrite(STEPPER_PIN_1, LOW);
digitalWrite(STEPPER_PIN_2, LOW);
digitalWrite(STEPPER_PIN_3, LOW);
digitalWrite(STEPPER_PIN_4, HIGH);
break;
}
}else{
switch(step_number){
case 0:
digitalWrite(STEPPER_PIN_1, LOW);
digitalWrite(STEPPER_PIN_2, LOW);
digitalWrite(STEPPER_PIN_3, LOW);
digitalWrite(STEPPER_PIN_4, LOW); /*HIGH*/
break;
case 1:
digitalWrite(STEPPER_PIN_1, LOW);
digitalWrite(STEPPER_PIN_2, LOW);
digitalWrite(STEPPER_PIN_3, LOW); /*HIGH*/
digitalWrite(STEPPER_PIN_4, LOW);
break;
case 2:
digitalWrite(STEPPER_PIN_1, LOW);
digitalWrite(STEPPER_PIN_2, LOW); /*HIGH*/
digitalWrite(STEPPER_PIN_3, LOW);
digitalWrite(STEPPER_PIN_4, LOW);
break;
case 3:
digitalWrite(STEPPER_PIN_1, LOW); /*HIGH*/
digitalWrite(STEPPER_PIN_2, LOW);
digitalWrite(STEPPER_PIN_3, LOW);
digitalWrite(STEPPER_PIN_4, LOW);
}
}
step_number++;
if(step_number > 3){
step_number = 0;
}
}
Thats pretty cool!
But now i wanna use one stepper motor for each frequency band (7 Bands = 7 Motors).
I thougt about to use some I²C - Motor Shields. Someone can recomand anything, that will work?
The LED’s will disappear in the final project - i just let them connected for now, to see the different frequency bands appear.
WHY ALL THAT?
In the End of the project i would like to visualize Audio in material form. (Not as LED’s, Beamer or Displays!)
The Motors (Rotors, or Actuators) should move some structure (or similar objects), that will interpret the Audio into moving material. I’m still in the beginning with that project. As for now.