You should supply information about why it does not do what you want. Your problem is most likely the delay which will cause the arduino to halt for 1 second and you can avoid this by using timing with "millis()".
Thanks for your reply....
The sensor is (Gravity: Conductivity Switch Sensor– SEN0223).
Actually, I don't have the experience to use timing with "millis()".
If it is possible to correct my code.
This is a very poor datasheet for the sensor in question, and equally poor example code. That said, I can see they intent to use the internal pull-up resistor based on the lines:
This is an antiquated way of turning on a pull-up resistor on the input and has been replaced with:
pinMode(inputPin, INPUT_PULLUP);
as @Robin2 pointed out. From there, it makes no sense to me that a HIGH would be the active state of the sensor, so I will assume that the sensor goes low when it senses conductivity less than 10 MOhms. Based on my assumption, I have fixed the code as follows:
const int LedPin = 13;
const int InputPin = 6;
int lastPinValue;
unsigned long ledOnTimestamp = 0;
void setup() {
pinMode(LedPin, OUTPUT);
digitalWrite(LedPin, LOW);
pinMode(InputPin, INPUT_PULLUP);
}
void loop() {
int pinValue = digitalRead(InputPin);
if (pinValue != lastPinValue && pinValue == LOW) {
digitalWrite(LedPin, HIGH);
ledOnTimestamp = millis();
}
else lastPinValue = pinValue;
if (millis() - ledOnTimestamp >= 1000UL) {
digitalWrite(LedPin, LOW);
}
}
only that there is so limited information in the documentation on the spec sheet, but their poor example program operates on a LOW being no conductivity and HIGH being conductive. I personally would look over the sensor and identify the chips and parts and add a pull-down resistor if needed. Basically, I only added to OP code the lines to insure off before on and 1 second on the switch before turning off without delay. it wasn't requested, but I'd also add a bit of debounce timing in the input. The way the code is now, if contact is made, then unmade, then made etc. within the window of 1 second, the LED on interval will restart and only turn off 1 second after the last unmade->made transition.
Thanks all for your comments.
Dear Perehama, the sensor has a resistor. please see the following link for more information about it:
I've tested your code, and the LED is OFF after 1 sec, when the sensor is OFF. Actually, I want to turn off the LED after 1 sec, when the sensor is ON, so how can I do that?
const int LedPin = 13;
const int InputPin = 6;
int lastPinValue;
unsigned long ledOnTimestamp = 0;
There is no where on that page, or in the links for additional information that gives a schematic for the sensor circuit or indicates that it is pulled high or low in a non-active state. It's less than adequate documentation.
AHM83:
I've tested your code, and the LED is OFF after 1 sec, when the sensor is OFF. Actually, I want to turn off the LED after 1 sec, when the sensor is ON, so how can I do that?
You need to change one line:
if (pinValue != lastPinValue && pinValue == HIGH) {
to
if (pinValue != lastPinValue && pinValue == LOW) {