Hey guys, I'm making Arduino project for airsoft game. I need last command. This is "radioactive" case, and it should make loud noise when its vibrating too much (during run for example). I completed code and I need one thing - after counting vibrations (let say for 10 seconds, it should activate relay. Relay is connected to pin 2. This is code:
#include "SD.h"
#define SD_ChipSelectPin 4
#include "TMRpcm.h"
#include "SPI.h"
TMRpcm tmrpcm;
const int buzzerPin = 8; //active piezo buzzer attached to digital pin 8 of Arduino
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(){
tmrpcm.speakerPin = 9;
Serial.begin(9600);
if (!SD.begin(SD_ChipSelectPin)) {
Serial.println("SD fail");
return;
}
tmrpcm.setVolume(5);
tmrpcm.play("3.wav");
pinMode(vibraPin, INPUT);
pinMode(greenPin, OUTPUT);
pinMode(redPin, OUTPUT);
pinMode(buzzerPin, OUTPUT);
}
void loop(){
if (!tmrpcm.isPlaying()) {
tmrpcm.play("3.wav");}
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
//tone(buzzerPin, 500);//sets buzzer on
delay(100);//sets delay for 100 ms
}
else
digitalWrite(redPin, LOW); //sets LED off
digitalWrite(greenPin, HIGH); //sets LED on
//noTone(buzzerPin);//sets buzzer off
if (avalue > threshold) //if analog value of vibration more then threshold value
{
digitalWrite(redPin, HIGH); //sets LED on
digitalWrite(greenPin, LOW); //sets LED off
tone(buzzerPin, 500);//sets buzzer on
delay(100);//sets delay for 100 ms
}
else
digitalWrite(redPin, LOW); //sets LED off
digitalWrite(greenPin, HIGH); //sets LED on
noTone(buzzerPin);//sets buzzer off
}
I see that you tried to post the code in code tags (thank you) but that is not really the way. You can go back and fix your original post by, first. highlighting the code and then clicking the </> in the menu bar.
I do notice that the code for the else clauses is not in curly brackets. Because of the lack of curly brackets only the one line of code right after the else will execute conditionally. The other lines will execute every time through loop(). Is that what you want?
else
digitalWrite(redPin, LOW); // executes conditionally
digitalWrite(greenPin, HIGH); // execute every time through loop()
else
digitalWrite(redPin, LOW); // executes conditionally
digitalWrite(greenPin, HIGH); // execute every time through loop()
noTone(buzzerPin); // execute every time through loop()
I need one more option, when there is vibration detection stretching for 10 seconds it should trigger relay (there will be loud horn connected) for LEDs, I did not connect them yet, I don't know if I will drill trough box to make them visible.
Record the value of millis() when the vibration is detected. Each time through loop() compare the current value of millis() with the recorded value. If it is over 10000 and the vibration is still there, sound the horn. If, before the 10 seconds, the vibration goes away set the recorded value of millis() to the current value of millis().
I tried but failed I need to read state from vibra pin exceeding threshold. It should count exceeding value and after lets say 20 in 10 secs period activate relay. But I managed to clean my code.
#include "SD.h"
#define SD_ChipSelectPin 4
#include "TMRpcm.h"
#include "SPI.h"
TMRpcm tmrpcm;
const int buzzerPin = 8; //active piezo buzzer attached to digital pin 8 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
const int RELAY_PIN = 2;
void setup(){
tmrpcm.speakerPin = 9;
Serial.begin(9600);
if (!SD.begin(SD_ChipSelectPin)) {
Serial.println("SD fail");
return;
}
tmrpcm.setVolume(5);
tmrpcm.play("3.wav");
pinMode(vibraPin, INPUT);
pinMode(buzzerPin, OUTPUT);
pinMode(RELAY_PIN, OUTPUT);
}
void loop(){
if (!tmrpcm.isPlaying()) {
tmrpcm.play("3.wav");}
int avalue = analogRead(vibraPin); //reads the analog value from vibration sensor module
// Serial.print("Analog value: ");//prints sensor value in serial monitor
// Serial.println(avalue);//prints sensor value in serial monitor
if (avalue > threshold) //if analog value of vibration more then threshold value
{
tone(buzzerPin, 500);//sets buzzer on
delay(100);//sets delay for 100 ms
}
else{
noTone(buzzerPin);//sets buzzer off
}
}