int mic_pin = A0;
int lightPin1 = A4;
int lightPin2 = A5;
int thresholdTouchMax = 800;
int thresholdTouchMin = -20;
int noise_level = 0;
const int sampleWindow = 50;
int ledPin1 = A1;
int ledPin2 = A2;
int ledPin3 = A3;
int ledPin4 = A4;
void setup(){
pinMode(mic_pin, INPUT);
pinMode(lightPin1, INPUT);
pinMode(lightPin2, INPUT);
pinMode(ledPin1, OUTPUT);
pinMode(ledPin2, OUTPUT);
pinMode(ledPin3, OUTPUT);
pinMode(ledPin4, OUTPUT);
Particle.subscribe("xingsuo/touchEvent1", touchGet);
Particle.subscribe("xingsuo/noiseLevel1", noiseGet);
Serial.begin(9600);
}
void loop(){
String noise_level = noiseSensor();
bool ifTouch = pressureSensor();
//Serial.println(noise_level);
//Serial.println(ifTouch);
}
void touchGet(String trigger)
{
if(trigger == "touch")
{
for(int i=0;i<255;i++)
{
analogWrite(ledPin1, i);
analogWrite(ledPin2, i);
analogWrite(ledPin3, i);
analogWrite(ledPin4, i);
delay(100);
}
for(int i = 255; i>0; i--){
analogWrite(ledPin1, i);
analogWrite(ledPin2, i);
analogWrite(ledPin3, i);
analogWrite(ledPin4, i);
delay(100);
}
}
}
bool pressureSensor(){
//int buttonState = digitalRead( switchPin );
int a1 = analogRead(lightPin1)/4;
int a2 = analogRead(lightPin2)/4;
int touch = a1-a2;
//Serial.println(analogRead(pressure_pin));
if(touch>=thresholdTouchMax || touch<thresholdTouchMin)
{
Particle.publish("xingsuo/touchEvent", "touch");
Serial.println(a1-a2);
return true;
}else
{
return false;
}
}
String noiseSensor(){
unsigned long startMillis = millis(); // Start of sample window
int highest_sample = 0;
int lowest_sample = 1000;
// collect data for 50 mS
while (millis() - startMillis < sampleWindow)
{
int sample = analogRead( mic_pin );
// invert the range, and convert it to a percent
sample = map( sample, 0, 4095, 1000, 0 );
// now see if the sample is the lowest;
if ( sample > highest_sample ){
highest_sample = sample ;
}
if ( sample < lowest_sample ){
lowest_sample = sample;
}
}
int peakToPeak = highest_sample - lowest_sample;
String level = String(peakToPeak);
Particle.publish("xingsuo/noiseLevel", level);
return level;
}
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!
You must login before you can post a comment. .