Week 10 Assignment: 4 Tabs Mini Piano

Concept:

In this week’s assignment, I decided to create a piano on a much smaller scale. As the title suggested, it is composed of 4 tabs. Its motivation is to make a piano that can be carried anywhere and played easily. The piano has multiple different tones to it, which can be adjusted with the use of a potentiometer. While there are digital applications where people can easily play piano, as this week’s text suggested- the importance of touch and expression when performing tasks compared to just touching a flat screen- I realized how a physical piano can bring forth a better experience than a digital one.

Prototype:

https://youtube.com/shorts/p7VoEhPxomk

Code:

#include "pitches.h"

//setting up various pins, numkeys is the number of keys on the piano, TABPins are the digital pins
const int numKeys = 4;                
const int TABPins[numKeys] = {2, 3, 4, 5}; 
const int pushButton = 6;            
const int speakerPin = 7;             

//variable to store the last pressed tab in it
int lastTAB = -1;                  
int pitch;                         
//variable for flagging system as open or close   
bool systemEnabled = false;           


void setup() {
  //set each buttonPin as input 
  for (int i = 0; i < numKeys; i++) {
    pinMode(TABPins[i],  INPUT);
  }
  //set the button as input 
  pinMode(pushButton, INPUT);
  Serial.begin(9600);
}

void loop() {
  // Check the state of the push button
  int buttonState = digitalRead(pushButton);

  // Toggle the system on/off when the button is pressed
  if (buttonState == LOW) {
    delay(50); 
    //when the button is pressed, invert the systemEnabled variable
    if (digitalRead(pushButton) == LOW) {
      systemEnabled = !systemEnabled;

      // If the system is now enabled, reset the lastTAB variable
      if (systemEnabled) {
        lastTAB = -1;
      }

      // Wait for the button to be released
      while (digitalRead(pushButton) == LOW) {
        delay(10);
      }
    }
  }

  // If the system is enabled, read the potentiometer value and play notes
  if (systemEnabled) {
    int potValue = analogRead(potentiometerPin);
    // Map potentiometer value to a pitch range
    pitch = map(potValue, 0, 1023, 200, 4000);  

    for (int i = 0; i < numKeys; i++) {
      int TABValue = digitalRead(TABPins[i]);

      // Play a note if the TAB is pressed and it's not the same TAB as the last one
      if (TABValue == LOW && i != lastTAB) {
        // note variable that stores a value from the loswet Note + the addition of the pitch which changes according to potentiometer
        int note = NOTE_B0 + pitch + i * 100;  
        // output the speaker with that note value for 1 second
        tone(speakerPin, note, 1000);
        delay(100);  

        // Update the lastTAB variable
        lastTAB = i;
      }
    }
  }
}

Reflection:

This was a fun hands-on exercise. One aspect of the project that took the most time was building the prototype which replicates the piano. The hard part on the other hand was most probably the setting up of the push button to flag the system as open or closed since I had to input in multiple delays to prevent various errors. As for future improvements, more tabs can be added within the piano to make it more feasible to produce multiple notes in one go. Also, the overall look of the piano can most probably be much better than this.

 

Leave a Reply