0
\$\begingroup\$

I am trying to use an Arduino Uno as a timer so that when current passes through an input pin it will only continue out of the output pin for a certain amount of time and then the current will stop flowing. Does anyone know how to achieve this and the code necessary? Any help is appreciated.

\$\endgroup\$
2
  • \$\begingroup\$ Take a relay breakout board, connect it to the Arduino and program a simple delay timer (for example with the delay() command but there are also more advanced/flexible ways of building a timer). If you’ve succeeded in making that work, you can make it fancier. This is, apart from blinking an LED, one of the easiest things to build with an Arduino and there are literally zillions of sites that describe how to do this. \$\endgroup\$
    – StarCat
    Commented Dec 20, 2020 at 8:51
  • 2
    \$\begingroup\$ Also check that you have yhe basics right. Arduino inputs work with voltage levels, not currents. A current flowing into Arduino pin means there is something wrong in the circuit. \$\endgroup\$
    – Justme
    Commented Dec 20, 2020 at 9:45

1 Answer 1

0
\$\begingroup\$

The most simple way would be to check if there is voltage at an input pin. If yes, the Arduino waits xyz seconds and then checks again. If there is again a voltage at the input pin, you can assume that there was a voltage at the pin for at least xyz seconds. After that you could turn on a digital pin and use it to switch something (if your load is over 40mA you should use a transistor or a relay to not damage your Arduino board. There a dozens of tutorials out there...). Since we all were beginners once, I will add a basic code snippet for you:

if (digitalRead(pin) == HIGH) { //check if there is a voltage at the pin
    delay(amountOfTime); // wait xyz seconds

    if (digitalRead(pin) == HIGH) { //check again
      //put your code here...
    }
}

The problem with this code is that you can't be sure if there is an interruption of the current flow during the delay(amountOfTime) part because if you use amountOfTime = 2000 for example, there could be a voltage interruption during those 2 seconds.

So a more advances way of programming it would be to create a while loop and wait as long as there is a voltage at the input pin. If there is no voltage anymore, you can check how long it took the Arduino to run the loop (and so how long the input was HIGH). After that you can simple compare the time that it took to run the loop with a threshold.
I prepared a piece of code for this scenario as well:

int starttime = millis(); //maybe change "int" to "long" because you can store larger numbers in a long
while (digitalRead(pin) == HIGH) {
    //just wait and do nothing
}
int buttonPressedTime = millis() - starttime; //again, you may change "int" to "long"
//check how long the button was pressed:
if (buttonPressedTime > xyz) {
    //the button was pressed for over xyz seconds
}

(if you don't know what the millis()-funktion is you can simply google it or browse the forum)




****EDIT****
I think I got your question a bit wrong, but I won't delete my old answer because it might be heplful for others

If you just want to check if there is a voltage at an input pin and if yes just wait xyz amount of time and afterwards switch another pin of the Arduino to HIGH, you can use the delay-function.
Code:
if (digitalRead(inputPin) == HIGH) { delay(amountOfTimeInMilliseconds); digitalWrite(outputPin, HIGH); } (sorry, but the code tags aren't working as they're supposed to)

\$\endgroup\$

Not the answer you're looking for? Browse other questions tagged or ask your own question.