Hackster is hosting Hackster Holidays, Ep. 6: Livestream & Giveaway Drawing. Watch previous episodes or stream live on Monday!Stream Hackster Holidays, Ep. 6 on Monday!
BenoitP
Published © CC BY

Music... Differently

I want to built some interractive Things for education sensibilisation. We go in school for talking about technology, with our machine !

IntermediateFull instructions provided6,328
Music... Differently

Things used in this project

Hardware components

Arduino Nano R3
Arduino Nano R3
×1
servomotor
×1
Resistor 2.21k ohm
Resistor 2.21k ohm
×1

Story

Read more

Schematics

Piano Schem

Drum Schem

Code

Code Arduino For The Drum

Arduino
Just deploy it on your arduino nano
#include <Servo.h>

Servo myservo1;  // create servo object to control a servo
Servo myservo2; 

int pos = 0;
int pos1 = 0;
int potpin0 = 0;  // analog pin used to connect the potentiometer
int potpin1 = 1;

int angleMaxGauche;
int angleMinGauche;
int angleMaxDroite;
int angleMinDroite;
int temp;

int boutonIni = 12;
int bouton0 = 2;
int bouton1 = 4;
int led1 = 8;
  
void setup() {
  myservo1.attach(9); 
  myservo2.attach(6);
  // attaches the servo on pin 9 to the servo object
    pinMode(boutonIni, INPUT);
    pinMode(bouton0, INPUT);
    pinMode(bouton1, INPUT);
    pinMode(led1, OUTPUT);
    Serial.begin(9600);
    
 //   digitalWrite(led1, HIGH);
    initialisation(); //on entre les positions min et max des servo
    Serial.println(angleMaxGauche);
     Serial.println(angleMinGauche);
      Serial.println(angleMaxDroite);
       Serial.println(angleMinDroite);
    
}

void loop() {
  
temp=pota(); //lit la valeur de la vitesse de la baguette (potard droit)

if(digitalRead(bouton0)==LOW){
 baguetteGauche(temp);
}  else if(digitalRead(bouton1)==LOW) {
  baguetteDroite(temp);
}  else if(digitalRead(bouton0)==LOW && digitalRead(bouton1)==LOW){
  baguetteGauche(temp);
  baguetteDroite(temp);

  }
 

}





void baguetteGauche(int temp) {
  digitalWrite(8,HIGH);
 for (pos = angleMinGauche; pos <= angleMaxGauche; pos += 5) { 
    myservo1.write(pos);              
         delay(temp);                
  }
    for (pos = angleMaxGauche; pos >= angleMinGauche; pos -= 5) { 
    myservo1.write(pos);              
    delay(temp);                      
  }
  digitalWrite(8,LOW);
  }
  
  
  
  
  void baguetteDroite(int temp) {
     digitalWrite(7,HIGH);
 for (pos1 = angleMaxDroite; pos1 <= angleMinDroite; pos1 += 5) { 
    myservo2.write(pos1);              
         delay(temp);                
  }
    for (pos1 = angleMinGauche; pos1 >= angleMaxGauche; pos1 -= 5) { 
    myservo2.write(pos1);              
    delay(temp);                      
  }
   digitalWrite(7,LOW);
  }
  
  
  
  int pota(){
 
  int poti = analogRead(A1);
  
   poti = map(poti, 0, 1023, 5, 50);
   
   return poti;
   
  }
  
  
  

void initialisation(){
 int val1 = analogRead(A0); 
// Serial.read(boutonIni);
 
 
 Serial.println("Initialiser angle de frappe du moteur gauche");
 while(digitalRead(boutonIni)==HIGH){
 val1 = analogRead(A0); 
  val1 = map(val1, 0, 1023, 0, 180);
 myservo1.write(val1);
 delay(10);
 

 }

delay(1000);

angleMaxGauche = val1;
 
 

  int val2 = analogRead(A0);
 
  Serial.println("Initialiser angle de repos du moteur gauche"); 
 while(digitalRead(boutonIni)==HIGH){
    val2 = analogRead(A0); 
 val2 = map(val2, 0, 1023, 0, 180);
 myservo1.write(val2);
 delay(10);


 }
 
 delay(1000);

 angleMinGauche = val2;
 
 
 
 
 int val3 = analogRead(A0); 
  Serial.println("Initialiser angle de frappe du moteur droite");
 while(digitalRead(boutonIni)==HIGH){
     val3 = analogRead(A0); 
 val3 = map(val3, 0, 1023, 0, 180);
 myservo2.write(val3);
 delay(10);

 
 }
 delay(1000);

 angleMaxDroite = val3;
 

 
  int val4 = analogRead(A0);
  Serial.println("Initialiser angle de repos du moteur droite"); 
 while(digitalRead(boutonIni)==HIGH){
     val4 = analogRead(A0); 
 val4 = map(val4, 0, 1023, 0, 180);
 myservo2.write(val4);
 delay(10);

 }
 delay(1000);

angleMinDroite = val4;
 
}
  
 
  
  
  
  
  
  
  
  
  

Code For the Piano

Arduino
Deploy this code on your arduino Leonardo or UNO
// Name the pin as led.
#define speaker 11

// Set the Snt end Pin & Receive Pin.
int trig = 12;
int echo = 13;
long cmm;

void setup()
{
pinMode(trig, OUTPUT);
digitalWrite(trig, LOW);
pinMode(echo, INPUT);
Serial.begin(9600);
}

void loop()
{
// Set a timer.
long start = millis();

cmm=What_Range();

if(cmm>10 && cmm<70)
{
tone(speaker, 440*cmm/30);
delay(100);
}
else
{
delay(10);
}

Serial.println(cmm);
noTone(speaker); // arbitrary delay to limit data to serial port
}

int What_Range()
{
int cm;
digitalWrite(trig, HIGH);
delayMicroseconds(10);
digitalWrite(trig, LOW);
cm = pulseIn(echo, HIGH) / 58;
return(cm);
}

Credits

BenoitP

BenoitP

1 project • 4 followers
I'm a student engeener , expert in electronics and robotics.

Comments