Back to Parent

#include "ZX_Sensor.h" //Library for gesture and distance sensor

#define speakerPin D3 // Speaker
#define led D4 //LED

int note; //Note frequency value
int x; //X axis distance of hand
int z; //Z axis distance of hand
int xVal; //Note value
int yVal; //Octave value

int melody[] = {1319,1319,1319,1047,1319,1397,784}; //Start melody
int duration[] = {500,500,500,500,500,250,500}; //Note duration
String instrument; //MIDI instrument

//String notes[]={c    d    e    f    g    a    b    C};
int tones1[] = { 523, 587, 659, 698, 784, 880, 988, 1047 }; //Lowest Octave Notes
int tones2[] = { 1047, 1175, 1319, 1397, 1568, 1760, 1976, 2093 }; //Middle Octave Notes
int tones3[] = { 2093, 2349, 2637, 2794, 3136, 3520, 3951, 4186 }; //Highest Octave Notes

ZX_Sensor   sensor = ZX_Sensor( 0x10 ); //Setting up the I2C commuications for ZX Sensor

void setup() {
  
  Particle.subscribe ("diot/2019/smartstudio/",handleSharedEvent); //Subscribing studio events
  sensor.init (); //Initializing ZX sensor
  pinMode (speakerPin, OUTPUT); //Setting speaker to output
  pinMode (led, OUTPUT); //Setting LED to output
  digitalWrite(led,LOW); //Setting LED as off
  Particle.publish("diot/2019/smartstudio/harmoniiize/","Music is Playing"); //Informing start of music
  playStart(); //Playing start melody
}

void loop() {
    
    // Decising instrument
    
    if (instrument="Drum") {
        //set MIDI to Drum
    } else if (instrument="Cello") {
        //set MIDI to Cello
    } else if (instrument="Piano") {
        //set MIDI to Piano
    } else if (instrument="Guitar") {
        //set MIDI to Guitar
    } else if (instrument="Flute") {
        //set MIDI to Flute
    }
    
    //Reading sensor values
    x = sensor.readX();
    z = sensor.readZ();
    
    //Mapping note index
    xVal = map(x,0,240,0,7);
    yVal = map(z,0,240,0,2);
    
    // Selecting the note
    if (yVal==0) {
        note = tones1[xVal];
    } else if (yVal==1) {
        note = tones2[xVal];
    } else if (yVal==2) {
        note = tones3[xVal];
    }
    tone(speakerPin,note,250); //Playing the selected note
    delay(250); // 4 beats/minute (=1000/delay)
}

//Playing Melody
void playStart() {
    for (int i=0;i<7;i++) {
        tone(speakerPin,melody[i],duration[i]);
        delay(duration[i]);
    }
}

//Catching published event
void handleSharedEvent(const char *event, const char *data) {
    
    String eventName = String(event);
    String dataValue = String(data);
    
    
    if( eventName.indexOf( "feelometer_avg" ) != -1 ){
      instrumentChange(dataValue); //Instrument selection from Feel-o-meter data
    } else if (eventName.indexOf( "Shhh" ) != -1) {
        redLight(); //Displaying Shhh command
    }
}

//Instrument Selection
void instrumentChange(String Val) {
    if (Val="1.000000") {
        instrument = "Drum";
    } else if (Val="2.000000") {
        instrument = "Cello";
    } else if (Val="3.000000") {
        instrument = "Piano";
    } else if (Val="4.000000") {
        instrument = "Guitar";
    } else if (Val="5.000000") {
        instrument = "Flute";
    }
}

//Turning ON Shhh signalling
void redLight() {
    digitalWrite(led,HIGH);
}
Click to Expand

Content Rating

Is this a good/useful/informative piece of content to include in the project? Have your say!

0