Hackster is hosting Hackster Holidays, Ep. 7: Livestream & Giveaway Drawing. Watch previous episodes or stream live on Friday!Stream Hackster Holidays, Ep. 7 on Friday!
mac28Michael AnginoYidi WangBrian HoepflNicholas Meisburger
Published

Mewzk Layzer

The highest quality sound in a small. lightweight package.

BeginnerShowcase (no instructions)734
Mewzk Layzer

Things used in this project

Hardware components

Grove - Ultrasonic Ranger
Seeed Studio Grove - Ultrasonic Ranger
×1
Breadboard (generic)
Breadboard (generic)
×1
Jumper wires (generic)
Jumper wires (generic)
×1
Buzzer
Buzzer
×3
Display Driver, LCD 4 Digits
Display Driver, LCD 4 Digits
×1
Grove Starter Kit for LaunchPad
Seeed Studio Grove Starter Kit for LaunchPad
×1
EK-TM4C123GXL TM4C Tiva LaunchPad
Texas Instruments EK-TM4C123GXL TM4C Tiva LaunchPad
×1

Software apps and online services

Energia
Texas Instruments Energia

Hand tools and fabrication machines

Breadboard, Plain
Breadboard, Plain
Tape Measure, Manual
Tape Measure, Manual

Story

Read more

Schematics

The circuit board

Code

Music Laser Library

C/C++
#ifndef MUSICLASER_H_
#define MUSICLASER_H_

#include "Energia.h"
#include "Ultrasonic.h"

class MusicLaser {
private:
	/*
	 * Initializes the necessary information for buzzer to play notes, as well as preset tempo and note frequency.
	 */
	int buzzerPin;
	int tempo = 200;
	int tones[13] = { 1915, 1804, 1700, 1607, 1519, 1432, 1351, 1275, 1204, 1136, 1073, 1014, 956 };
	Ultrasonic ultrasonic;
public:
	int note = 13;

	MusicLaser(int buzzerPin, Ultrasonic ultrasonic): buzzerPin(buzzerPin), ultrasonic(ultrasonic){};

	void setUp ();

	void playTone (int tone, int duration);

	void playNote (int noteKey, int duration);

	int getDistance ();

	void loopFunc();
};

#endif /* MUSICLASER_H_ */

Music Laser Library cpp file

C/C++
#include "MusicLaser.h"
#include "Ultrasonic.h"

void MusicLaser::setUp() {
	/*
	 * Completes setup by initializing buzzer pins.
	 */
	pinMode(buzzerPin, OUTPUT);
}

void MusicLaser::playTone (int tone, int duration) {
	/*
	 * Plays note by turning on and off buzzer with durations corresponding to the frequency of the note.
	 */
	for (long i = 0; i < duration * 1000L; i += tone * 2) {
	    digitalWrite(buzzerPin, HIGH);
	    delayMicroseconds(tone);
	    digitalWrite(buzzerPin, LOW);
	    delayMicroseconds(tone);
	    }
}

void MusicLaser::playNote(int noteKey, int duration){
	/*
	 * Calls play tone with a specfied note key as input that will access an item in the array.
	 */
	this->playTone(tones[noteKey], duration);
}

int MusicLaser::getDistance() {
	  /*
	  * Takes distance from distance sensor and then determines the coresponding note bin.
	  * Returns the note bin.
	  */
	  float distance = ultrasonic.MeasureInCentimeters();
	  if(distance > 103){
	    distance = 103;
	    }
	  int noteKey = distance / 8;
	  this->note = noteKey + 1;
	  return noteKey;
	}

void MusicLaser::loopFunc() {
	/*
	 * Call this function in the void loop section.
	 * Calls get distance to determine distance and then plays the note for the bin returned by getDistance.
	 * Calls playNote to play note.
	 */
	int noteKey = this->getDistance ();
	playNote (noteKey, this->tempo);
	delay (this->tempo);
}

Implementation of Library

C/C++
#include <MusicLaser.h>
#include "TM1637.h"

/*
 * Defines constants for pins
 */
#define ULTRASONIC_PIN 24
#define DIO 38
#define CLK 39

/*
 * Initializes the 4 digit display
 */
TM1637 tm1637(CLK, DIO);
int8_t bits[4] = {0};


/*
 * Initializes instance of musicLaser
 */
Ultrasonic ultrasonic(ULTRASONIC_PIN);
MusicLaser musicLaser(5, ultrasonic);

void setup() {
  /*
   * Calls setup method on musicLaser and sets up 4 digit display.
   */
  musicLaser.setUp();
  
  tm1637.init();
  tm1637.set(BRIGHT_TYPICAL);
}

void loop() {
  /*
   * Calls musicLaser loop fundtion to play notes and displays note bin on 4 digit display.
   */
  musicLaser.loopFunc();
  
  showDist ();
}

void showDist() {
  /*
   * Prints bin number to 4 digit display.
   */
  int number_dist = musicLaser.note;
  memset(bits, 0, 4);
  for (int i = 3; i >= 2; i--){
    bits[i] = number_dist % 10;
    number_dist = number_dist / 10;
    tm1637.display(i, bits[i]);
  }
}

Credits

mac28
2 projects • 0 followers
Michael Angino
3 projects • 0 followers
Yidi Wang
1 project • 0 followers
Brian Hoepfl
1 project • 0 followers
Nicholas Meisburger
2 projects • 0 followers

Comments