Two sensors to run one LED (Arduino)

enesene

Joined Mar 3, 2020
29
I have one vibrate sensor and one water sensor. both of them work fine separately. Bu when i mix them it doesn't work.
Do you know what's wrong?

C:
const int LED= 9;
const int vibrate = A0;
const int water = A1;
int sensorvib;
int sensorwat;
void setup(){
Serial.begin(9600);
pinMode(LED,OUTPUT);
pinMode(vibrate, INPUT);
pinMode(water, INPUT);
}
void loop() {
if ((sensorvib < 1020) && (sensorwat > 200))
{
digitalWrite(LED,HIGH);
delay(500);
digitalWrite(LED,LOW);
delay(500);
}
}

KeithWalker

Joined Jul 10, 2017
1,851
What doesn't work?
Do you get errors when you compile it or do you get a wrong respone when it runs?
Have you tested this program by only changing line 15 to only test one or the other of the inputs?
I don't see anything wrong with it except that you declare sensorvib and sensorwat twice.

djsfantasi

Joined Apr 11, 2010
7,763
What doesn't work?
Do you get errors when you compile it or do you get a wrong respone when it runs?
Have you tested this program by only changing line 15 to only test one or the other of the inputs?
I don't see anything wrong with it except that you declare sensorvib and sensorwat twice.
I can’t explain why, but experience has taught me that declaring a reference twice, globally and locally, often causes problems.

Comment out the first definitions (the global definitions) and try again.