Small DC motor doesnt like my code

I have a Arduino Uno V3 and a Motor shield. I am trying to make a motor run for 3 different cases, very new to Arduino so not even sure I have the code written the best way.
Anyway I have everything functioning when I use Leds in place of the motor but when i change the code for the dc motor i have issues.

Here are the three cases I am looking to accomplish.

(1) if masterswitch is toggled high and top switch is high, toggle motor down until bottom switch is toggled high.
(2) if masterswitch is toggled high and bottom switch is high, toggle motor up until top switch is toggled high.
(3) if masterswitch is toggled high and bottom switch is Low and top switch is low, toggle motor up until top switch is toggled high.

^--Hopefully this makes sense.

Here is my code when using with LED's (This works good for me so far);

const int master_switch = 2;
const int top_switch = 3;
const int bot_switch = 4;
const int motor_up = 12;
const int motor_down = 13;

// switch states
int master_state = 0;
int top_state = 0;
int bot_state = 0;

void setup(){
  pinMode(master_switch, INPUT);
  pinMode(top_switch, INPUT);
  pinMode(bot_switch, INPUT);
  pinMode(motor_up, OUTPUT);
  pinMode(motor_down, OUTPUT);
  
}

void loop(){
  master_state = digitalRead(master_switch);
  top_state = digitalRead(top_switch);
  bot_state = digitalRead(bot_switch);
  
  if(master_state == HIGH && bot_state == HIGH){
    digitalWrite(motor_up, HIGH);
  }
  else if(top_state == HIGH){
  digitalWrite(motor_up, LOW);
}
{
  
  if(master_state == HIGH && top_state == HIGH){
    digitalWrite(motor_down, HIGH);
  }
  else if(bot_state == HIGH){
  digitalWrite(motor_down, LOW);
}
  }
{
  
  if(master_state == HIGH && bot_state == LOW && top_state == LOW){
    digitalWrite(motor_up, HIGH);
  }
  else if(top_state == HIGH){
  digitalWrite(motor_up, LOW);
}
}
}

And here is where I run into problems, the motor attempts to do something but no go.

const int master_switch = 2;
const int top_switch = 7;
const int bot_switch = 4;
const int motordir = 12;
const int motorbrake = 9;
const int motorpwm = 3;

// switch states
int master_state = 0;
int top_state = 0;
int bot_state = 0;

void setup(){
  pinMode(master_switch, INPUT);
  pinMode(top_switch, INPUT);
  pinMode(bot_switch, INPUT);
  pinMode(motordir, OUTPUT);
  pinMode(motorbrake, OUTPUT);
  
}

void loop()
// If Master Switch is Toggled and current state is down, turn motor on up until top switch is toggled.
{
  master_state = digitalRead(master_switch);
  top_state = digitalRead(top_switch);
  bot_state = digitalRead(bot_switch);
  
  if(master_state == HIGH && bot_state == HIGH){
    digitalWrite(motordir, HIGH);
    digitalWrite(motorbrake, LOW);
    analogWrite(motorpwm, 50);
 
  }
  else if(top_state == HIGH){
  digitalWrite(motorbrake, HIGH);  
}
//If master Switch is Toggled and current state is up, turn motor on down until bot switch toggled.
{
  if(master_state == HIGH && top_state == HIGH){
    digitalWrite(motordir, LOW);
    digitalWrite(motorbrake, LOW);
    analogWrite(motorpwm, 50);
  }
  else if(bot_state == HIGH){
  digitalWrite(motorbrake, HIGH);
}
  
//Fail safe if power loss, If MasterSwitch is Toggled and Bottom and Top Switch are open, turn motor up on until top switch is toggled.
{
  if(master_state == HIGH && bot_state == LOW && top_state == LOW){
    digitalWrite(motordir, HIGH);
    digitalWrite(motorbrake, LOW);
    analogWrite(motorpwm, 50);
  }
  else if(top_state == HIGH){
  digitalWrite(motorbrake, HIGH);
}
}
}
}

Thanks in advance for any help, if you have a better way to go about this I am all ears.

What goes wrong with the motors? If the code works for the LEDs isn't it more likely to be hardware-related? How are you powering the motors?

You have other problems but, you only need to this part of code 1 time per loop in your situation.

master_state = digitalRead(master_switch);
  top_state = digitalRead(top_switch);
  bot_state = digitalRead(bot_switch);

What are you driving the motor with? The Arduino can't run the motor directly, you need motor driver of some kind.

cyclegadget:
You have other problems but, you only need to this part of code 1 time per loop in your situation.

master_state = digitalRead(master_switch);

top_state = digitalRead(top_switch);
bot_state = digitalRead(bot_switch);

Thanks adjusted code.

MarkT:
What goes wrong with the motors?

The motor will run when master switch is toggled and both top and bottom switches are low, it will shut off either the top or bottom switch are toggled high.
If you toggle the master switch when either the top or bottom switch are high the motor makes a noise as if it is trying to run but not enough power.

If the code works for the LEDs isn't it more likely to be hardware-related? How are you powering the motors?

The code from the LED test and the motor are modified, I think my problem is in how I am controlling the dc motor, I am powering the motor through the Arduino Motor Shield and an external 9V battery. The DC motor is a simple Radio Shack motor, that does not require much power.

cyclegadget:
What are you driving the motor with? The Arduino can't run the motor directly, you need motor driver of some kind.

Using the official Arduino Motor Shield and a 9V battery.
Here is a pic of how I have things wired up:

Anyone have any ideas?

What is the motor doing (or not doing) that you don't want?

analogWrite(motorpwm, 50);

This is less than 1 volt. Does the motor run OK on less than 1 volt?

Replaced variable with 250 still operates the same.

For testing purpose throw in a delay and see if it makes a difference.

analogWrite(motorpwm, 50);
          delay(1000);

no go on adding a delay. it just runs the motor for a sec and shuts off. I think the pwm signal is ending before either the top or bot switch is toggled. is there a way to have the motor run continuously until topswitch or botswitch is toggled in the given scenario?

is there a way to have the motor run continuously until topswitch or botswitch is toggled in the given scenario?

That is exactly what should happen when the analogWrite() function is called. The fact that adding a delay(1000) made the motor work for one second implies that somewhere else in your code YOU are turning the motor off.

Since you are not using the internal pullup resistors (why not?), you need to have an external resistor (an unnecessary piece of hardware). Just how IS the switch wired? That mess of wiring is too hard to follow in the picture.

PaulS:
That mess of wiring is too hard to follow in the picture.

Down load Fritzing and do a snazzy diagram with that.

Here is what I have, the motor shield just sits on top of the Arduino and I did not find the shield. pretty simple though. battery connects to vin and ground. dc motor to channel A.

Perhaps you should print the state of each switch, so you can determine that they are indeed doing what you think they are.

Here is what I have found after some more testing. I can get the motor to operate correctly when i only code for (1) if statement. When I add the second scenario (additional if statement) I run into problems. I am assuming the different if statements are somehow conflicting. Anyone know a better way to do this? I was thinking about using case statements but not sure on how to properly implement this.

Here is the code I used to get the dc motor to work correctly in one scenario. The quoted out code shows where i tried to add an additional case and the motor would no longer operate. Thanks for the help.

//define switches
int masterswitch = 2;
int topswitch = 4;
int botswitch = 7;

//define motor function
int direct = 12;
int brake = 9;
int pwm = 3;

//define state
int masterstate = 0;
int topstate = 0;
int botstate = 0;


void setup(){
  //pinout switches
  pinMode(masterswitch, INPUT);
  pinMode(topswitch, INPUT);
  pinMode(botswitch, INPUT);
  //pinout motor
  pinMode(direct, OUTPUT);
  pinMode(brake, OUTPUT);
  pinMode(pwm, OUTPUT);
}


void loop(){
  masterstate = digitalRead(masterswitch);
  topstate = digitalRead(topswitch);
  botstate = digitalRead(botswitch);
  
  //motor down -- turn motor on down until bot switch HIGH
  if(masterstate == HIGH && topstate == HIGH)
  {
    digitalWrite(direct, HIGH);
    digitalWrite(brake, LOW);
    analogWrite(pwm, 50);
  }
  else if(topstate == LOW && botstate == HIGH){
    digitalWrite(brake, HIGH);

  }

  //motor down -- turn motor on up until top switch HIGH
//  if(masterstate == HIGH && botstate == HIGH)
//  {
//     digitalWrite(direct, LOW);
//    digitalWrite(brake, LOW);
//     analogWrite(pwm, 50);
//  }
//  else if(botstate == LOW && topstate == HIGH)
//  {
//    digitalWrite(brake, HIGH);
//  }

}

I STILL don't see any code that proves that you are reading the switch states correctly.

PaulS:
I STILL don't see any code that proves that you are reading the switch states correctly.

Sorry very new to this. I am not exactly sure what you mean, and not sure how to implement this.
Do you mean simply outputting to the serial monitor via:

serial.println(switchstate);

Do you mean simply outputting to the serial monitor via:

serial.println(switchstate);

Almost.

serial.print("switchstate: ");
serial.println(switchstate);

With your code, you might see:

0
0
1
1
0
0
0
1
0
0
0

in the Serial Monitor, which would tell you what?

With mine, you'd know which value went with which switch.