Pages: [1]   Go Down
Author Topic: Vibration Sensor  (Read 720 times)
0 Members and 1 Guest are viewing this topic.
Baltic states
Offline Offline
Jr. Member
**
Karma: 0
Posts: 97
new day, new try
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello everybody,
I have bought this vibration sensor: http://i40.tinypic.com/vpksxh.jpg
At first I thought that it will work like a button(if vibrates it give digital HIGH, else LOW).
But in reality it works only with this code:
Code:
#define SensorLED     13
#define SensorINPUT   2

unsigned char state = 0;

void setup()
{
  pinMode(SensorLED, OUTPUT);
  pinMode(SensorINPUT, INPUT);
  attachInterrupt(0, blink, FALLING);//D2 interface: interrupt 0. call blink()

}
void loop()
{
      if(state!=0)
      {
        state = 0;
        digitalWrite(SensorLED,HIGH);
        delay(500);
      }  
      else
        digitalWrite(SensorLED,LOW);
}


void blink()//when input falling edge, this interrupt function works.
{
  state++;
}

//the result is the LED( PIN 13 ) will light 500ms after the Shock.


Can somebody explain me how this code works? Is it possible to change sensors sensitivity with the code?
Thank you very much!
« Last Edit: December 18, 2011, 06:27:00 am by Mastino » Logged

0
Offline Offline
Shannon Member
****
Karma: 199
Posts: 11649
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

The interrupt routine is activated whenever pin 2 goes from HIGH to LOW, and increments the counter called "state".

The loop() code checks state every half second and if its not zero (indicating some joggling of the sensor in that 1/2 second) it turns the LED on (and clears the state back to zero).

This code is not perfect (for instance if there are 256 joggles of the sensor state wraps round to zero again and it will miss it).

I suspect the sensor is a simple tremble switch.
Logged

[ I won't respond to messages, use the forum please ]

Baltic states
Offline Offline
Jr. Member
**
Karma: 0
Posts: 97
new day, new try
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thank you very much for your answer!
Is it possible to change sensors sensitivity with the code?
Logged

Brunsbüttel, SH, F.Rep.GERM
Offline Offline
God Member
*****
Karma: 4
Posts: 596
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

i dont think u can distinguish strong vibration from soft vibration... the amplitude can just be "too low" and "high enough"...

but maybe u can draw some conclusions out of the frequency of the vibration?
Logged

-Arne

Worcester, MA
Offline Offline
God Member
*****
Karma: 3
Posts: 623
Arduino rocks
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

You may want to look into using an accelerometer with tap detection instead. 
Logged

I2C GPS Shield

Checkout my Open Source GPS Tracker on Kickstarter

Pages: [1]   Go Up
Jump to: