Off delay for motor

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;
  }
}

wouter-helek:
it works counterwise....

What does that mean?

Is your button wired from the pin to ground, as required by the pinMode as input pullup in the code?

the compiler suggested parenthesis around the individual tests and I believe you want a logical AND (&&) not a bitwise AND (&)

        if ((currentButtonState != previousButtonState)
            && (currentButtonState == LOW))  //button has become

finola_marsaili:
What does that mean?

Is your button wired from the pin to ground, as required by the pinMode as input pullup in the code?

Yes, it is. But if I test it, de LED stays on until I push the button. Than it shuts down for a certain time and goes back on. It should word the other way. Go on for a certain time if I push the button and than go back out...

How's the led wired then?

If the led has its anode on the digital pin and cathode (the flat side of the "bulb") to ground, a high on the pin is on.

If the led has its anode to 5V and cathode to digital pin, a low on the pin is on.