Welcome to Hackster!
Hackster is a community dedicated to learning hardware, from beginner to pro. Join us, it's free!
Jenny PenalozaJaanita Mehraniuser1051329
Published © GPL3+

Play Music with the Chip Trombone

A fun little musical instrument that plays tones and changes speed while moving your hands up and down.

BeginnerFull instructions provided1 hour551
Play Music with the Chip Trombone

Things used in this project

Hardware components

EK-TM4C123GXL TM4C Tiva LaunchPad
Texas Instruments EK-TM4C123GXL TM4C Tiva LaunchPad
×1
Grove Starter Kit for LaunchPad
Seeed Studio Grove Starter Kit for LaunchPad
x1 Grove Base BoosterPack x2 Ultrasonic Rangers (D) x1 Buzzer (D) x1 4-Digital-Display (D) x4 Grove Cables
×1

Software apps and online services

Energia
Texas Instruments Energia

Story

Read more

Schematics

Schematic Diagram of Chip Trombone

A detailed look on the set up utilizing the launchpad and grove starter kit

Code

ELEC 220 Chip Trombone

C/C++
One ultrasonic ranger is set to change pitch while the other is set to change note duration. The 4-digit-display will read the distance from your hand to the ultrasonic ranger changing pitch.
#include "TM1637.h" 
#include "Ultrasonic.h"
/* Macro Define */
/* Set pins for all the components*/
#define CLK               39              /* 4-Digit Display clock pin */
#define DIO               38              /* 4-Digit Display data pin */
#define ULTRASONIC_PIN    24              /* pin of the Ultrasonic Ranger */
#define BUZZER_PIN        36              /* sig pin of the Grove Buzzer */

/* Global Variables */
TM1637 tm1637(CLK, DIO);                  /* 4-Digit Display object */
Ultrasonic ultrasonic(ULTRASONIC_PIN);    /* Ultrasonic Ranger object */
Ultrasonic ultra2(26);
int distance = 0;                         /* variable to store the distance to obstacles in front */
int durDis = 0;
int8_t bits[4] = {0};                     /* array to store the single bits of the value */

/* the setup() method runs once, when the sketch starts */
void setup() 
{
    /* Initialize 4-Digit Display */
    tm1637.init();
    tm1637.set(BRIGHT_TYPICAL);
    pinMode(BUZZER_PIN, OUTPUT);
}

/* play tone */
void playTone(int tone, int duration) 
{
  for (long i = 0; i < duration * 1000L; i += tone * 2) 
  {
    digitalWrite(BUZZER_PIN, HIGH);
    delayMicroseconds(tone);
    digitalWrite(BUZZER_PIN, LOW);
    delayMicroseconds(tone);
  }
}

/* the loop() method runs over and over again */
void loop()
{
    distance = ultrasonic.MeasureInCentimeters();   /* read the value from the sensor */  
    durDis = ultra2.MeasureInCentimeters();
    
    memset(bits, 0, 4);                             /* reset array before we use it */
    int altDis = distance;
    for(int i = 3; i >= 0; i--) 
    {
        /* Convert the value to individual decimal digits for display */
        bits[i] = altDis % 10;
        altDis = altDis / 10;
        tm1637.display(i, bits[i]);                 /* display on 4-Digit Display */
    }
    /* set a distance threshold up to where the ultrasonic rangers will register        hand placement*/
    if (distance < 100) {
      if (durDis < 100) {
        playTone(900 + 20*distance, 250 + 20* durDis);  /*plays at a frequency*/
        
      }
      else {
        playTone(900 + 20*distance, 250); /*playes at a frequency with default 
                                            duration*/
      }
    }   
    delay(10);
}

Credits

Jenny Penaloza
1 project • 0 followers
Contact
Jaanita Mehrani
1 project • 0 followers
Contact
user1051329
0 projects • 0 followers
Contact

Comments

Please log in or sign up to comment.