Hi,
I've been working on a motor that should stop after 10 seconds. I want to use Arduino for this.
I've picked up a code from the forum but it works counterwise....
The code:
const byte inputPin = A1;
const byte ledPin = 13;
unsigned long startTime;
unsigned long currentTime;
unsigned long period = 5000; //adjust to change period
byte currentButtonState = HIGH;
byte previousButtonState = HIGH;
boolean timing = false;
void setup()
{
Serial.begin(115200);
pinMode(inputPin, INPUT_PULLUP); //input will he HIGH when not pressed
pinMode(ledPin, OUTPUT);
digitalWrite(ledPin, HIGH); //turn off the LED
}
void loop()
{
currentTime = millis();
if (!timing) //only react to input if not already timing
{
previousButtonState = currentButtonState;
currentButtonState = digitalRead(inputPin);
if (currentButtonState != previousButtonState & currentButtonState == LOW) //button has become pressed
{
digitalWrite(ledPin, LOW); //turn on the LED
startTime = millis();
timing = true;
}
}
else if (currentTime - startTime >= period)
{
digitalWrite(ledPin, HIGH); //turn off the LED
timing = false;
}
}