Week 10 – DJ

Project: The Arduino DJ Console

Assignment Description
For this assignment, we had to create a musical instrument. The requirements were to use at least one digital sensor (switch) and one analog sensor.

What We Made
We created a DJ console using an Arduino. This instrument allows for infinite possibilities based on the user. We used the following components:

  • Two Potentiometers (Analog Sensors): One knob adjusts the tone (pitch) of the note. The other knob adjusts the speed (duration) of the note.

  • One Button (Digital Sensor): This button acts as a mute switch to stop the sound.

  • Piezo Buzzer: This plays the sound.

Schematic

Here is the circuit diagram for our project. We connected the knobs to the analog pins (A0 and A5) and the button to a digital pin (13).

Code

We wrote code to read the sensors and play notes from a C Major scale. Here is the source code for the project:

C++

// Control a buzzer with two knobs and a button

// Define hardware pins
const int piezoPin = 9;
const int pitchPotPin = A0;
const int durationPotPin = A5;
const int buttonPin = 13;

// List of frequencies for C Major scale
int notes[] = {262, 294, 330, 349, 392, 440, 494, 523};

void setup() {
  // Start data connection to computer
  Serial.begin(9600);
  Serial.println("Instrument Ready! Note Stepping Enabled.");

  // Set pin modes
  pinMode(piezoPin, OUTPUT);
  pinMode(buttonPin, INPUT_PULLUP);
}

void loop() {
  // Check if the button is pressed
  int buttonState = digitalRead(buttonPin);

  // Mute sound if button is held down
  if (buttonState == LOW) {
    noTone(piezoPin);
  } else {
    // Read values from both knobs
    int pitchValue = analogRead(pitchPotPin);
    int durationValue = analogRead(durationPotPin);

    // Convert pitch knob value to a note index from 0 to 7
    int noteIndex = map(pitchValue, 0, 1023, 0, 7);

    // Select the frequency from the list
    int frequency = notes[noteIndex];

    // Convert duration knob value to time in milliseconds
    int noteDuration = map(durationValue, 0, 1023, 50, 500);

    // Play the sound
    tone(piezoPin, frequency, noteDuration);

    // Show information on the screen
    Serial.print("Note Index: ");
    Serial.print(noteIndex);
    Serial.print(" | Frequency: ");
    Serial.print(frequency);
    Serial.print(" Hz | Duration: ");
    Serial.print(noteDuration);
    Serial.println(" ms");

    // Wait for the note to finish
    delay(noteDuration + 50);
  }
}

Video Demonstration

Check out the video below to see the instrument in action.

 

Here is my attempting Happy Birthday (Badly)

Leave a Reply