Motor Control Code Issues

I am currently in the process of building a simple tank-style R/C vehicle. It uses the transmitter and electronics out of an old nikko R/C car, along with a motor shield and Tamiya tracked chassis with a dual-motor transmission. The shield is powered by a 2s Li-Po battery, and the Arduino is powered through the Vin.

Since the control system is not proportional, and the voltage is way higher than the motors should be able to take, the motors receive about a 50% PWM at full throttle.

Having tested the thing multiple times, the electronics are all in working order, and I'm pretty sure they are wired up correctly.

When I press forward on the remote, 4.9v should be applied to digital pin 4
back sends it to 5
right sends it to 6
left sends it to 7

That being said, here is the code:

void setup() {
  // put your setup code here, to run once:
pinMode(12, OUTPUT);//Left motor direction is output
pinMode(9, OUTPUT);//Left motor brake is output
pinMode(13, OUTPUT);//Right motor direction is output
pinMode(8, OUTPUT);//Right motor brake is output
pinMode(3, OUTPUT);//left motor output
pinMode(11, OUTPUT);//right motor output
pinMode(4, INPUT);//FWD power wire is input
pinMode(5, INPUT);//REV power wire is input
pinMode(6, INPUT);//R power wire is input
pinMode(7, INPUT);//L power wire is input
digitalWrite(9, LOW);//Sets left brake off
digitalWrite(8, LOW);//Sets right brake off
}

void loop() {
  // put your main code here, to run repeatedly:
if (digitalRead(4) == HIGH && digitalRead(6) == HIGH){//If FWD and right
  digitalWrite(12, HIGH);//both motors FWD
  digitalWrite(13, HIGH);
  analogWrite(3, 127);//left motor 50%
  analogWrite(11, 56);//right motor 25%
}
else if (digitalRead(4) == HIGH && digitalRead(7) == HIGH){//if FWD and left
  digitalWrite(12, HIGH);//both motors fwd
  digitalWrite(13, HIGH);
  analogWrite(3, 56);//left motor 25%
  analogWrite(11, 127);//right motor 50%
}
else if (digitalRead(5) == HIGH && digitalRead(6) == HIGH){//rev and right
  digitalWrite(12, LOW);//reverse
  digitalWrite(13, LOW);//reverse
  analogWrite(3, 127);//left 50%
  analogWrite(11, 56);//right 25%
}
else if (digitalRead(5) == HIGH && digitalRead(7) == HIGH){//rev and left
  digitalWrite(12, LOW);//both rev
  digitalWrite(13, LOW);
  analogWrite(3, 56);//left 25%
  analogWrite(11, 127);//right 50%
}
else if (digitalRead(4) == HIGH){//just fwd
  digitalWrite(12, HIGH);//fwd
  digitalWrite(13, HIGH);
  analogWrite(3, 127);//left 50%
  analogWrite(11, 127);//right 50%
}
else if (digitalRead(5) == HIGH){//just rev
  digitalWrite(12, LOW);//both fwd
  digitalWrite(13, LOW);
  analogWrite(3, 127);//both 50%
  analogWrite(11, 127);
}
else if (digitalRead(6) == HIGH){//just right
  digitalWrite(12, HIGH);//left fwd
  digitalWrite(13, LOW);//right rev
  analogWrite(3, 127);//both 50%
  analogWrite(11, 127);
}
else if (digitalRead(7) == HIGH){//just left
  digitalWrite(12, LOW);//left rev
  digitalWrite(13, HIGH);//right fwd
  analogWrite(3, 127);//both 50%
  analogWrite(11, 127);
}
else{//The only situation this should cover is no input
  analogWrite(3, 0);//no power to either
  analogWrite(11, 0);
}
}

Hopefully the comments answer any questions about function you might have.

Now the main problem is that when nothing is being pressed, the motors just continue to do what they were last told to do. I suspect this is either a coding problem or something to do with hanging voltage. Are there any issues with the code? I don't think so, I went over it a million times, but I would like some people with more experience to check it.

Thanks in advance!

der_cobester:
Now the main problem is that when nothing is being pressed, the motors just continue to do what they were last told to do. I suspect this is either a coding problem or something to do with hanging voltage. Are there any issues with the code? I don't think so, I went over it a million times, but I would like some people with more experience to check it.

Thanks in advance!

Where do you turn off what you turned on? You can't expect magic to turn the pins you set HIGH to just go LOW without your program doing it.

How long should your last command continue to operate?

Paul

Where do you turn off what you turned on? You can't expect magic to turn the pins you set HIGH to just go LOW without your program doing it.

else{//The only situation this should cover is no input
  analogWrite(3, 0);//no power to either
  analogWrite(11, 0);
}

Well this should stop the motors if none of the top statements are true.
The easiest is to add serial prints to your sketch and see what command its getting stuck in and to see if your else statement is working.

Another thing check that all your grounds are connected together and the arduino GND to make sure your pins are pulled low when they should be otherwise the voltage might hang.

M......

Alright, when I have time I think I'll pull out the breadboard and put in some pull resistors to ground. I was Hoping it would work as is without a breadboard, but I guess not.