I'm a noob when it comes to programming. I'm trying to use a push button to turn on a dc motor and have it run for 5 sec and then turn itself off and not turn on again until the button is pushed again. Every time the button is pushed it only runs for 5 sec. I'm using a ATtiny85 that is programmed with an Arduino Nano as an ISP. I used Tinker Circuits and it worked perfectly with the code you see below, but when I actually made the circuit it was all over the place. After doing some research I learned you need to debounce the push button to get rid of the sporadic readings. I'm not sure how to incorporate that code into what I have already done though. Any help is greatly appreciated.
int PushButton = 0; // Pin 0 on ATtiny85
int MosfetOutput = 2; // Pin 2 on ATtiny85
int val = 0;
void setup()
{
pinMode(PushButton, INPUT); //Push Button
pinMode(MosfetOutput, OUTPUT); //Output to Mosfet
}
void loop ()
{
val = digitalRead(PushButton); // Push Button is currently wired to alway show it as High unless button is pressed. Then it is read as low.
if (val == LOW)
{
digitalWrite(MosfetOutput, HIGH);
delay (5000); // Runs Motor for 5 seconds
}
else
{
digitalWrite(MosfetOutput, LOW); // Motor stays off until Push Button is pressed.
}
}