Friday, November 22, 2013

Stepper Motor Music

I recently came upon a small project by accident.  I was working with a bipolar stepper motor and an Arduino, trying to program the steps individually without using the stepper motor class.  I finally succeeded, wasn't too hard but noticed I was getting some interesting beat sounds coming from the clicking of the stepper motor as it resonated on my table top.
So I wanted to amplify this sound but could not find my piezo amplifier so I decided to use the guitar pickups on my daughters pink mini strat.
By placing the stepper motor on the strings and running different sets of intervals between the steps on the stepper motor, I produced some crazy sounds.
You can listen to them here on my Sound Cloud account.
https://soundcloud.com/migsmixs/sets/stepper-motors

The basic setup is an H-Bridge with an arduino.
You can find the example here.
http://arduino.cc/en/Tutorial/MotorKnob

Rather then using the code in the stepper class, I just started sending values to the pins at different intervals.

Here's a video of it working.
https://www.youtube.com/watch?v=eIfX5nt4FPQ




Here's the code for the arduino.
#####
int mo1 = 8;  //These are the different pins connected to the H-Bridge
int mo2 = 9;
int mo3 = 10;
int mo4 = 11;
int BeatDelay=20;



void setup() {              
  // initialize the digital pin as an output.
  pinMode(mo1, OUTPUT);
 pinMode(mo2, OUTPUT);
 pinMode(mo3, OUTPUT);
 pinMode(mo4, OUTPUT);
}

void loop() {




myBeat7();




}

//This is a function that takes in different intervals.
void stepMyMotor(int mySpeed){
  digitalWrite(mo1, HIGH);   // turn the Motor on (HIGH is the voltage level)
  delay(mySpeed);               // wait for a delay
  digitalWrite(mo1, LOW);   // turn the Motor off (HIGH is the voltage level)
  delay(mySpeed);
    digitalWrite(mo4, HIGH);
  delay(mySpeed);            
  digitalWrite(mo4, LOW);
  delay(mySpeed);
    digitalWrite(mo2, HIGH);
  delay(mySpeed);            
  digitalWrite(mo2, LOW);
  delay(mySpeed);
      digitalWrite(mo3, HIGH);
  delay(mySpeed);            
  digitalWrite(mo3, LOW);
  delay(mySpeed);
  }

//These are the different beats
void myBeat1(){
stepMyMotor(1);
stepMyMotor(100);
stepMyMotor(10);
stepMyMotor(100);
stepMyMotor(10);
stepMyMotor(200);
  }


  void myBeat2(){
stepMyMotor(1);
stepMyMotor(1);

  }

  void myBeat3(){
stepMyMotor(1);
stepMyMotor(40);

  }



    void myBeat4(){
stepMyMotor(1);
stepMyMotor(120);

  }

//To keep the beats within the same intervals, the delays are divisors of each other 80, 160.
      void myBeat6(){
stepMyMotor(1);
stepMyMotor(80);
stepMyMotor(1);
stepMyMotor(160);

  }


      void myBeat5(){
stepMyMotor(1);
stepMyMotor(120);
stepMyMotor(1);
stepMyMotor(30);
stepMyMotor(30);
stepMyMotor(30);
stepMyMotor(30);

  }

  void rev(){
   int i=0;
   while (i<50 p="">     stepMyMotor(i);
     i++;
     }
    }
 
          void myBeat7(){
stepMyMotor(1);
stepMyMotor(120);
stepMyMotor(1);
stepMyMotor(60);
stepMyMotor(60);


  }

1 comment:

  1. {myBeat7();} was not declared i this scope
    source code error

    ReplyDelete