Mike86
Published

Motion Control Music Player

This is a tutorial about how to use the music play shield and ultrasonic module together to make a motion control music player.

BeginnerFull instructions provided30 minutes503
Motion Control Music Player

Things used in this project

Hardware components

Iteaduino Uno
×1
music play shield (out of stock now)
×1
Itead Ultrasonic Ranging Module HC-SR04
×1
SD card or Flash disk
×1

Story

Read more

Schematics

Schematic for Itead Uno

Code

Untitled file

C/C++
const int EchoPin = 2; //Ultrasonic signal input
const int TrigPin = 3; //Ultrasonic control signal output
int currDist = 0; 
void setup(){
  pinMode(EchoPin, INPUT);
  for (int pinindex = 3; pinindex < 8; pinindex++) {
    pinMode(pinindex, OUTPUT); // set pins 3 to 10 as outputs
  }
pinMode(A0,OUTPUT);
pinMode(A1,OUTPUT);
pinMode(A2,OUTPUT);
pinMode(A3,OUTPUT);
digitalWrite(A0,LOW);
digitalWrite(A1,LOW);
digitalWrite(A2,LOW);
digitalWrite(A3,LOW);
Serial.begin(9600);
}
void loop(){
currDist = MeasuringDistance(); //Read distance
 //Serial.println(currDist);
  if(currDist > 25){ 
  }
//power
  //else if(currDist < 8){
   // mpower();
 // }
   else if(currDist < 7){
    mpause();
  }
    else if(currDist < 15){
    mnext(); 
  }
    else if(currDist < 25){
    mprev();  
  }
}
long MeasuringDistance() {
  long duration;
  digitalWrite(TrigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(TrigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(TrigPin, LOW);
  duration = pulseIn(EchoPin, HIGH);
  return duration / 29 / 2;
}
//mplay
void mplay() {
  digitalWrite(A0, HIGH);
  delay(100);
 digitalWrite(A0, LOW);
 Serial.println("mplay");
}  
//mprev
void mprev() {
  digitalWrite(A1, HIGH);
  delay(1000);
  digitalWrite(A1, LOW);
 Serial.println("mprev");
}
//mnext
void mnext() {
  digitalWrite(A2, HIGH);
  //return;
  delay(1000);
  digitalWrite(A2, LOW);
 Serial.println("mnext");
}  
//mpause
void mpause() {
  digitalWrite(A0, HIGH);
  delay(1000);
 digitalWrite(A0, LOW);
   Serial.println("mpause");
}  
//mpower
//void mpower() {
 // digitalWrite(A3, HIGH);
 // delay(500);
 //digitalWrite(A3, LOW);
  // Serial.println("mpower");
//}  

Credits

Mike86

Mike86

15 projects • 11 followers

Comments