Skip to content

Commit e40939f

Browse files
committed
Vibration sensor sketch
1 parent 059d034 commit e40939f

File tree

1 file changed

+94
-0
lines changed

1 file changed

+94
-0
lines changed
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
/**
2+
* The MySensors Arduino library handles the wireless radio link and protocol
3+
* between your home built sensors/actuators and HA controller of choice.
4+
* The sensors forms a self healing radio network with optional repeaters. Each
5+
* repeater and gateway builds a routing tables in EEPROM which keeps track of the
6+
* network topology allowing messages to be routed to nodes.
7+
*
8+
* Created by Henrik Ekblad <[email protected]>
9+
* Copyright (C) 2013-2015 Sensnology AB
10+
* Full contributor list: https://github.com/mysensors/Arduino/graphs/contributors
11+
*
12+
* Documentation: http://www.mysensors.org
13+
* Support Forum: http://forum.mysensors.org
14+
*
15+
* This program is free software; you can redistribute it and/or
16+
* modify it under the terms of the GNU General Public License
17+
* version 2 as published by the Free Software Foundation.
18+
*
19+
*******************************
20+
*
21+
* DESCRIPTION
22+
*
23+
* Vibration Sensor
24+
*
25+
* connect the sensor as follows :
26+
*
27+
* VCC >>> 5V
28+
* S >>> D3
29+
* GND >>> GND
30+
*
31+
* Based on: http://www.dfrobot.com/wiki/index.php/DFRobot_Digital_Vibration_Sensor_V2_SKU:DFR0027
32+
* Contributor: epierre
33+
**/
34+
35+
36+
#include <MySensor.h>
37+
#include <SPI.h>
38+
#include <Wire.h>
39+
40+
#define CHILD_ID_VIBRATION 0
41+
#define VIBRATION_SENSOR_DIGITAL_PIN 3
42+
#define SensorLED 13
43+
44+
unsigned long SLEEP_TIME = 10*1000; // Sleep time between reads (in seconds)
45+
46+
//VARIABLES
47+
int val = 0; // variable to store the value coming from the sensor
48+
float valVIBRATION =0.0;
49+
float lastVIBRATION =0.0;
50+
unsigned char state = 0;
51+
52+
MySensor gw;
53+
MyMessage vibrationMsg(CHILD_ID_VIBRATION, V_LEVEL);
54+
55+
void setup()
56+
{
57+
gw.begin();
58+
59+
// Send the sketch version information to the gateway and Controller
60+
gw.sendSketchInfo("VIBRATION Sensor", "1.0");
61+
62+
// Register all sensors to gateway (they will be created as child devices)
63+
gw.present(CHILD_ID_VIBRATION, S_VIBRATION);
64+
65+
66+
pinMode(VIBRATION_SENSOR_DIGITAL_PIN, INPUT);
67+
attachInterrupt(1, blink, FALLING);// Trigger the blink function when the falling edge is detected
68+
pinMode(SensorLED, OUTPUT);
69+
}
70+
71+
void loop()
72+
{
73+
74+
if(state>=40){ // basically below 40 so ignire basic level
75+
gw.send(vibrationMsg.set(int(state)));
76+
state = 0;
77+
digitalWrite(SensorLED,HIGH);
78+
} else {
79+
state = 0;
80+
digitalWrite(SensorLED,LOW);
81+
}
82+
83+
84+
// Power down the radio. Note that the radio will get powered back up
85+
// on the next write() call.
86+
delay(1000); //delay to allow serial to fully print before sleep
87+
88+
gw.sleep(SLEEP_TIME); //sleep for: sleepTime
89+
}
90+
91+
void blink()//Interrupts function
92+
{
93+
state++;
94+
}

0 commit comments

Comments
 (0)