Running a motor until a button is triggered

I am making a halloween decoration and need to run a motor until a button is triggered. I have most of the code, I just need an example of it in a code that is similar. PLEASE HELP!!!!!!!

const int motor = 4;  // Motor control pin
const int button = 5;  // Button input pin

void setup() {
    pinMode(motor, OUTPUT);
    pinMode(button, INPUT);
    digitalWrite(motor, HIGH); // Turn motor on.
}

void loop() {
    if (digitalRead(button))  // If button is pressed
        digitalWrite(motor, LOW);  // Turn off motor
}

I can't turn the motor on in the setup like in that example, it has to be written in the void loop because there is a lot more to the code than that

ryan036: I can't turn the motor on in the setup like in that example, it has to be written in the void loop because there is a lot more to the code than that

Perhaps you mis stated your requirements in your first post. In your second post it seems you want the motor to start, stop when a button is pressed, and never start again until the arduino is reset. Is that what you really want?

Yes

ryan036: I can't turn the motor on in the setup like in that example, it has to be written in the void loop because there is a lot more to the code than that

Perhaps you should have said that in the first place, or shown your code.

const int motor = 4;  // Motor control pin
const int button = 5;  // Button input pin
int motorPower = LOW;

void setup() {
    pinMode(motor, OUTPUT);
    pinMode(button, INPUT);
    motorPower = HIGH;
}

void loop() {
    digitalWrite(motor, motorPower);
    if (digitalRead(button))  // If button is pressed
        motorPower = LOW;  // Turn off motor
}

Sorry I am new to this, but durring the code I want the motor to come on and when a button is hit turn off.

ryan036:
Sorry I am new to this, but durring the code I want the motor to come on and when a button is hit turn off.

void loop() {
    if (time for motor to come on)
        digitalWrite(motor, motorPower);

    if (digitalRead(button)) {  // If button is pressed
        motorPower = LOW;  // Turn off motor
        digitalWrite(motor, motorPower);
    }
}

To avoid dealing with possible floating input pin, one can set the button pin high like below and then use ground to pull it low.

//zoomkat servo button test 7-30-2011

#include <Servo.h>
int button1 = 4; //button pin, connect to ground to move servo
int press1 = 0;
Servo servo1;

void setup()
{
  pinMode(button1, INPUT);
  servo1.attach(7);
  digitalWrite(4, HIGH); //enable pullups to make pin high
}

void loop()
{
  press1 = digitalRead(button1);
  if (press1 == LOW)
  {
    servo1.write(160);
  }
  else {
    servo1.write(20);
  }
}

I don't see any need for an Arduino here. All you need is a self-energizing relay with a normally-closed momentary switch to de-energize it.

Simple statemachine so a press of the button changes the state of the motor.

#define BUTTON 7
#define MOTOR 8
boolean motorRunning = false;

void setup()
{
  pinMode(BUTTON , INPUT);
  pinMode(MOTOR , OUTPUT);
  digitalWrite(MOTOR, LOW);
}

void loop()
{
  if (digitalRead(BUTTON) == HIGH) 
  {
    motorRunning = !motorRunning;  // T->F->T etc
  }

  if (motorRunning)
  {
    digitalWrite(MOTOR, HIGH);
  }
  else
  {
    digitalWrite(MOTOR, LOW);
  } 
  delay(1000); // some time to release the button, can be programmed more robust by monitoring state of button and motor simultaneous.
}

Thanks Everyone! I got it running!