/* Project: Vibration sensor module, digital and analog signals. Comment lines if you do not need digital or analog Function: If Arduino get 0 (no vibration) from vibration sensor it will turn on green LED and turn off Red LED. If Arduino get 1 from vibration sensor, it will turn on Red LED and turn off green LED. */ //*********************************************** const int vibrdigPin = 7; //vibration sensor module digital pin attached to digital pin 7 of Arduino const int greenPin = 5; //Green LED attached to digital pin 5 of Arduino const int redPin = 6; //Red LED attached to digital pin 6 of Arduino const int vibraPin = A0; //vibration sensor module analog pin attached to analog pin A0 of Arduino int threshold=100;//sets value of vibration //************************************************ void setup() { Serial.begin(9600);//initialize serial communication at 9600 bps pinMode(vibrdigPin, INPUT); pinMode(vibraPin, INPUT); pinMode(greenPin, OUTPUT); pinMode(redPin, OUTPUT); } void loop() { int dvalue = digitalRead(vibrdigPin); //reads the digital value from vibration sensor module int avalue = analogRead(vibraPin); //reads the analog value from vibration sensor module Serial.print("Digital value: ");//prints sensor value in serial monitor Serial.print(dvalue);//prints sensor value in serial monitor Serial.print(" ");//prints sensor value in serial monitor Serial.print("Analog value: ");//prints sensor value in serial monitor Serial.println(avalue);//prints sensor value in serial monitor if (dvalue == 1) //if vibration detected { digitalWrite(redPin, HIGH); //sets LED on digitalWrite(greenPin, LOW); //sets LED off delay(2000);//sets delay for 2 seconds } else digitalWrite(redPin, LOW); //sets LED off digitalWrite(greenPin, HIGH); //sets LED on if (avalue > threshold) //if analog value of vibration more then threshold value { digitalWrite(redPin, HIGH); //sets LED on digitalWrite(greenPin, LOW); //sets LED off delay(2000);//sets delay for 2 seconds } else digitalWrite(redPin, LOW); //sets LED off digitalWrite(greenPin, HIGH); //sets LED on }