Turn off DC motor with button when in opeartion


int button1 = 12;
int button2 = 6;
int button1val;
int button2val;
int redLED = 8;
int dcmotor = 7;
int greenLED = 9;
bool dcmotorRunning;
void setup()
{
pinMode(button1,INPUT);
pinMode(button2,INPUT);
pinMode(redLED,OUTPUT);
pinMode(dcmotor, OUTPUT);

}

void loop(){
button1val = digitalRead(button1);
button2val = digitalRead(button2);

if(button1val == 0){
digitalWrite(dcmotor, HIGH);
digitalWrite(redLED,HIGH);
digitalWrite(greenLED,LOW);
delay(10000);
digitalWrite(dcmotor, LOW);
delay(5000);
digitalWrite(redLED,LOW);
digitalWrite(greenLED,HIGH);
}
if(button2val == 0 && dcmotorRunning == false){
digitalWrite(dcmotor,LOW);
digitalWrite(redLED,LOW);
digitalWrite(greenLED,HIGH);
}

}

Hi everyone, I am nnew to arduino and would like too know how to cancel the dc motor and turn off the red LED and turn on the Green LED when I press the 2nd push button. A bit like an emergency stop button. Here is what I have got so far but just can't seem too work out how to do it. Any advice would be great.

Don't use the UNO as power supply for any motor. Datasheet for the motor, please..
Use a free wheel, kick back diode across the motor.
Please read the advice in "How to get the best out of this forum", especially how to post code.

If you want your buttons to always be responsive, you can not use delay(). It blocks program execution until it is done. Look at the Blink Without Delay example in the IDE (File->examples->02.digital->Blink Without Delay) to see how you can track elapsed time and let the rest of the program do what you want.

Additionally: no need for your external pull-up reisistors. Just declare the pin as INPUT_PULLUP v. INPUT and you get the same behavior with fewer parts.

Also, it makes NO sense to use a transistor to drive your motor when you are using +5V from the Uno. You can't do that. The Uno power supply with most likely not be able to provide enough current for your motor. Transistors are needed when you run your motor at some voltage above the Uno (like +12V or +24V)