MUSICAL INSTRUMENT [week 10]

For this week’s assignment, I decided to a piano-like musical instrument. So, basically there are two ways of controlling it. First, without a photoresistor, buttons will play one music, but with a photoresistor, its frequency change to a high-pitched sound.

Buttons are digital input, while a photoresistor is an analog input. Sound is outputted from the buzzor.

VIDEO:

CODE:

int but1 = 3;
int but2 = 4;
int but3 = 5;
int but4 = 6;

int LDR=A1;
int buzzer = 13;
int sensorValue;
void setup()
{
  //let's declare the button pins as input
  pinMode(but1,INPUT);
  pinMode(but2,INPUT);
  pinMode(but3,INPUT);
  pinMode(but4,INPUT);

  //declare buzzer pin as output
  pinMode(buzzer,OUTPUT);
    Serial.begin(9600);
  
  
}

void loop()
{
  

  // read the value from buttons
  int b1 = digitalRead(but1);
  int b2 = digitalRead(but2);
  int b3 = digitalRead(but3);
  int b4 = digitalRead(but4);

 int sensorvalue= analogRead(LDR);
Serial.println(sensorvalue);
  if (sensorvalue<680){
  if( b1 == 1 ){
     tone(buzzer,300,100);
     delay(100);
     tone(buzzer,300,100);
     delay(100);
     tone(buzzer,300,100);
     delay(100);
     tone(buzzer,300,100);
  }
    if( b2 == 1 ){
     tone(buzzer,400,100);
      delay(100);
      tone(buzzer,400,100);
       delay(100);
       tone(buzzer,400,100);
        delay(100);
        
         delay(100);
  }
    if( b3 == 1 ){
     tone(buzzer,500,100);
  }
    if( b4 == 1 ){
     tone(buzzer,600,100);
  }
  }

   if (sensorvalue > 680){

  if( b1 == 1 ){
     tone(buzzer,700,100);
  }
    if( b2 == 1 ){
     tone(buzzer,800,100);
  }
    if( b3 == 1 ){
     tone(buzzer,900,100);
  }
    if( b4 == 1 ){
     tone(buzzer,1000,100);
  }
  }

    
  }

 

One thought on “MUSICAL INSTRUMENT [week 10]”

  1. I want to see you perform changing the light on the LDR with one hand and playing notes with the buttons with the other! Let’s move away from using delay with any interactive elements. It will make the perception of the interactivity seem like something is wrong since many times when the user presses a button the program will not know that, as it is stuck in one of the delays.

Leave a Reply