Back to Parent

int ledPin1 = A1;
int ledPin2 = A2;
int ledPin3 = A3;
int ledPin4 = A4;

#define NTDH1 589
#define NTDH2 661
#define NTDH3 700
#define NTDH4 786
#define NTDH5 882
#define NTDH6 990
#define NTDH7 112

int tune1[]=
{
NTDH7,NTDH5,NTDH6
};

int tune2[]=
{
NTDH3,NTDH4,NTDH1
};
int tune3[]=
{
NTDH1,NTDH2,NTDH5
};

float durt[]=
{
1+1,0.5,1.5
};

int length;
int tonepin = D0;

int noiseLevel1 = 800;
int noiseLevel2 = 1000;
int noiseLevel3 = 1200;

void setup(){

Particle.subscribe("xingsuo/touchEvent", touchGet);
Particle.subscribe("xingsuo/noiseLevel", noiseGet);

pinMode(ledPin1, OUTPUT);
pinMode(ledPin2, OUTPUT);
pinMode(ledPin3, OUTPUT);
pinMode(ledPin4, OUTPUT);
pinMode(tonepin, OUTPUT);

pinMode(D1, OUTPUT);


Serial.begin(9600);
}

void loop(){



}

void touchGet(const char *event, const char *data)
{
  String dataStr = data;
  String test = "touch";
if(dataStr == "touch")
{

    for(int i=0;i<255;i++)
    {
      analogWrite(ledPin1, i);
      analogWrite(ledPin2, i);
      analogWrite(ledPin3, i);
      analogWrite(ledPin4, i);
      delay(100);
    }
    delay(2000);
    for(int i = 255; i>0; i--){
      analogWrite(ledPin1, i);
      analogWrite(ledPin2, i);
      analogWrite(ledPin3, i);
      analogWrite(ledPin4, i);
      delay(100);
    }

}

}

void noiseGet(const char *event, const char *data)
{

 String dataStr = data;
 int level = dataStr.toInt();
 Serial.println(level);
if(level <= noiseLevel1)
{
  playSound(0);
}
else if((level > noiseLevel1) && (level <= noiseLevel2) )
{
  playSound(1);
}
else if((level > noiseLevel2) && (level <= noiseLevel3) )
{
  playSound(2);
}
else if(level > noiseLevel3 )
{
  playSound(3);
}
}

void playSound(int level)
{
switch(level)
{
  case 0: ;
  case 1: {
            length=sizeof(tune1)/sizeof(tune1[0]);
            for(int x=0;x<length;x++)
            {
              tone(tonepin,tune1[x]);
              delay(500*durt[x]);
              noTone(tonepin);
            }
            delay(2000);
          };
  case 2: {
            length=sizeof(tune2)/sizeof(tune2[0]);
            for(int x=0;x<length;x++)
            {
              tone(tonepin,tune2[x]);
              delay(500*durt[x]);
              noTone(tonepin);
            }
            delay(2000);
          };

  case 3: {
            length=sizeof(tune3)/sizeof(tune3[0]);
            for(int x=0;x<length;x++)
            {
              tone(tonepin,tune3[x]);
              delay(200*durt[x]);
              noTone(tonepin);
            }
            delay(2000);
          };

          }

        }
Click to Expand

Content Rating

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

0