Hey guys, I just need your help right now. I use arduino uno and I have two sensors, the sound sensor and the other one is vibration sensor. The problem is when I combine the code of vibration sensor
int ledPin = 13;
int EP =9;
void setup(){
pinMode(ledPin, OUTPUT);
pinMode(EP, INPUT); //set EP input for measurment
Serial.begin(9600); //init serial 9600
// Serial.println("----------------------Vibration demo------------------------");
}
void loop(){
long measurement =TP_init();
delay(50);
// Serial.print("measurment = ");
Serial.println(measurement);
if (measurement > 1000){
digitalWrite(ledPin, HIGH);
}
else{
digitalWrite(ledPin, LOW);
}
}
long TP_init(){
delay(10);
long measurement=pulseIn (EP, HIGH); //wait for the pin to get HIGH and returns measurement
return measurement;
}
and the other one is for sound
const int ledPin = 12;
const int soundPin = 7;
int soundVal = 0;
void setup ()
{
pinMode (ledPin, OUTPUT);
pinMode (soundPin, INPUT);
}
void loop ()
{
soundVal = digitalRead(soundPin);
if (soundVal == LOW)
{
digitalWrite(ledPin, HIGH);
}
else
{
digitalWrite(ledPin, LOW);
}
}
The vibration sensor only functioning. Please help me to combine these two to work together.
Thanks in advance
Please post your combined version and explain exactly what problem you are having.
Steve
This is the combine code where the vibration code only functioning. Thank you for the reply.
int ledPin = 10;
int buzz = 8;
int EP = 9;
const int soundpin=A2;
const int threshold=700; // sets threshold for sound sensor
void setup(){
pinMode(ledPin, OUTPUT);
pinMode (EP, INPUT);
pinMode(soundpin,INPUT);
pinMode (buzz, OUTPUT);
Serial.begin(9600); //init serial 9600
void loop() {
int soundsens=analogRead(soundpin); // read analog data from sensor
if (soundsens>=threshold) {
digitalWrite(ledPin, HIGH); // turn led on
digitalWrite(buzz, HIGH);
delay(1000);
}
else{
digitalWrite(ledPin, LOW);
digitalWrite(buzz, LOW);
}
long measurement =TP_init();
delay(50);
if (measurement>=100000)
{
digitalWrite(ledPin, HIGH);
digitalWrite(buzz, HIGH);
delay(2000);
}
else
{
digitalWrite(ledPin, LOW);
digitalWrite(buzz, LOW);
digitalWrite(ledPin, LOW);
}
}
long TP_init(){
delay(10);
long measurement=pulseIn (EP, HIGH); //wait for the pin to get HIGH and returns measurement
return measurement;
}
Nothing works in the code you posted. It does not even compile because you are missing a closing brace } to end setup(). Put that in just above void loop() and try again.
If there are still problems post the changed code and details of any problems.
Steve
Hey guys, I have a problem regarding to combing the sound sensor and vibration sensor in Arduino. So when I combined the two sensor code the vibration sensor only works. I observed that the sound did not function when I combine these two. I hope you will help me out in my code and thanks for replying to my post :). Anyway this is the combined code:
int ledPin = 10;
int buzz = 8;
int EP = 9;
const int soundpin=A2;
const int threshold=700; // sets threshold for sound sensor
void setup(){
pinMode(ledPin, OUTPUT);
pinMode (EP, INPUT);
pinMode(soundpin,INPUT);
pinMode (buzz, OUTPUT);
Serial.begin(9600); //init serial 9600
}
void loop() {
int soundsens=analogRead(soundpin); // read analog data from sensor
if (soundsens>=threshold) {
digitalWrite(ledPin, HIGH); // turn led on
digitalWrite(buzz, HIGH);
delay(1000);
}
else{
digitalWrite(ledPin, LOW);
digitalWrite(buzz, LOW);
}
long measurement =TP_init();
delay(50);
if (measurement>=100000)
{
digitalWrite(ledPin, HIGH);
digitalWrite(buzz, HIGH);
delay(2000);
}
else
{
digitalWrite(ledPin, LOW);
digitalWrite(buzz, LOW);
digitalWrite(ledPin, LOW);
}
}
long TP_init(){
delay(10);
long measurement=pulseIn (EP, HIGH); //wait for the pin to get HIGH and returns measurement
return measurement;
}
That's not exactly a simple combining of two codes.
Your original working sound code used a digitalRead of soundPin on pin 7. You've changed that to an analogRead() of pin A2 and used a threshold of 700. Why? Are you using a different sensor?Have you remembered to change the wiring? Put some Serial.reads in to see what values you are actually getting from that analogRead().
It's interesting that the other sensor works even though you've changed the pulse length threshold from 1000 to 100000.
Steve