Skip to content
Play melody by changing distance. Arduino project
C++ C
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
img
.gitignore
LICENSE
README.md
melody.h
notes_frequencies.h
rangiano.ino

README.md

RANGIANO

Arduino based instrument for playing melodies by changing distance

Demo video:
thumbnail link to YouTube

Used components

  1. Arduino UNO Rev3
  2. Seeedstudio Grove ultrasonic ranger v2.0
  3. Seeedstudio Grove speaker v1.1

rangiano wiring

Wiring scheme with links to components and assembled project at DESCRiBE

Usage

Works out of the box

  • Clone project: git clone https://github.com/efojs/rangiano.git or copy code to your sketch

  • Connect sensor and speaker to pins 12 and 3 accordingly (or set yours in rangiano.ino)

To customize

In melody.h:

  • Choose or write melody following this pattern:
[ note + number of octave + _N ] ==> C4_N == piano middle C

In rangiano.ino:

  • Set working range in centimetres:
#define FAR 40
#define MINDIST 5  
  • Set direction of playing (default: played by approaching sensor):
// #define GOAWAY true   // to play by moving away from sensor
  • Set method of playing (default: using tone() method):
// #define GENERATE true  // to play by digitalWrite() and delay(): with pause, but more realiable

How it works

Our goal is to play melody note by note:

  • Melody is set as an array of notes in melody.h Notes are defined by respective frequencies in notes_frequencies.h
const float notes[] = { C4_N, D4_N, E4_N, F4_N, G4_N, A4_N, B4_N, C5_N };
  • Every note in array has its own index (from 0 to N)
  • We will divide working range into subranges by number of notes in melody — steps
  • Then play note corresponding to the index of step of current location

Technically

Ranger measures distance from 3 to 350 cm ==> cm
Then we check if it is in the working range.

               |<-------- working range -------->|
               |                                 |
  |____________|=================================|___________|
  |           NEAR          |                   FAR          |
  |                         |                                |
  3 cm                      cm                              350 cm

If cm is in range, we find index of current step (depending on direction of playing).
By default we play by approaching sensor.
So divide distance from FAR to cm by noteStep, e.g.:

cm =       124
FAR =  170
noteStep =  25

int index = (170 - 124)/25 ==> 1.84 ==> 1  // not rounding

--  
                  cm    
        NEAR       |    FAR
         |         |     |
steps:   | 3 | 2 | 1 | 0 |
                  /
                 /
                /               
notes:   | C | D | E | F |

Then pass index to player method, either:

  • one using builtin tone() method, or
  • one generating sound.

Known issues

  • tone() somehow disturbs sensor at about 150 cm
  • not yet sure if it's possible to use generateSound() method without pause for measure

Contribute

Will be great if you add features or fix something (e.g. this readme) by making a pull request or raising an issue.

Disclaimer

This project is made for educational purposes.
Be careful working with electricity.
Code provided as is and no warranty applied — so be careful and check everything before usage — I can not be responsible for any damage caused by your usage of this code.

Licence

Some code or concepts used from these Arduino tutorial:

Rangiano is released under the MIT License.

You can’t perform that action at this time.