Change Direction of DC Motor with 2 button input Ladyada Motorshield

Hi all,
Sorry to still be super-noob at programming after years of tinkering, but...can anyone tell me why this code doesn't work?

//Camera Motor sketch for use with LadyAda motor shield and DC motor
//and 2 digital ins which tell motor when to change directions
//Because the analogue pins are available on the motor shield, I am using
//A4 and A5 as digital ins.
#include <AFMotor.h>

int botBut = A5;//pin that receives input from the bottom button
int topBut = A4;//pin that receives input from the top button
int valTop = LOW;//intitalizing top button
int valBot = LOW;//initializing bottom button

int minStep = 200;//least # of steps per 'go'
int maxStep = 500;//maximum # of steps per 'go'
int stepNum = 100;




AF_DCMotor motorD(2, MOTOR12_64KHZ);//DC motor on motor port 2 at 64 khz


void setup() {

Serial.begin(9600);


pinMode(botBut, INPUT);//button to tell motor it is at the end of its run on one side
pinMode(topBut, INPUT);//button to tell motor it is at the end of its run on one side
motorD.setSpeed(100); //set the speed of the motor


}
void loop()
{
  
  valTop = digitalRead(topBut);//check the state of the top button
  valBot = digitalRead(botBut);//check the state of the bottom button
  
  if (valTop == LOW && valBot ==LOW){//If neither button is pressed,
   Serial.println("LOW,LOW");
   Direction();
  }
   else if (valTop == HIGH) {
   for(int i=0;i<20; i++){ //this is long enough to disengage the button
    motorD.run(BACKWARD);
    Serial.println("BACKWARD, TOPHIGH");
  valTop = digitalRead(topBut);
  valBot = digitalRead(botBut);
  
    }
   }
   
  else if (valBot == HIGH){
    for(int i=0;i<20; i++){
      motorD.run(FORWARD);
      Serial.println("FORWARD, BOTHIGH");
  valBot = digitalRead(botBut);
   valTop = digitalRead(topBut);
   
  }
}
  }
   
void Direction(){
    boolean FB = (int) random (0,2);
  if (FB == true) {
    GoForward ();
  }
  if (FB == false) {
    GoBackward ();
  }//step the motor as presecribed byt he "go" funciton
    
    
  }
  

void GoForward(){
  
  
  stepNum = (int)random(minStep, maxStep);
  for (int i = 0; i< stepNum; i ++){
  valTop = digitalRead(topBut);
  valBot = digitalRead(botBut);
    
  motorD.run(FORWARD);
  Serial.println ("FORWARD!");
  
  if(valTop==HIGH){
    
    for (int i=0; i<200; i++){
      motorD.run(BACKWARD); 
    }
  }
  
     if(valBot==HIGH){
    for (int i=0; i<200; i++){
      motorD.run(FORWARD);      
    }
  
  }
  }
}

  
 void GoBackward(){
  
  
  stepNum = (int)random(minStep, maxStep);
  for (int i = 0; i< stepNum; i ++){
  valTop = digitalRead(topBut);
  valBot = digitalRead(botBut);
    
  motorD.run(BACKWARD);
  Serial.println ("BACKWARD!");
  if(valTop==HIGH){
   
    for (int i=0; i<20; i++){
      motorD.run(BACKWARD);
     //lastvalTop = valTop;
    }
  }
     if(valBot==HIGH){
   
    for (int i=0; i<20; i++){
      motorD.run(FORWARD);
    } 
  }
  }
 }

You are not activating the internal pull-up resistors, so you must have external pull-down resistors. Right?

And if you are using external pull-down resistors, explaining the symptoms of the problem would make it easier for us to help you troubleshoot.

I have external resistors. I've got the code working now. The problem seems to have been the logic of the GoForward and GoBackward functions. The motor just jerked around when the buttons were pushed. Thanks for putting your eyeballs on the last version. I'm sure this one is not too elegant, but it's doing what I want for now. Suggestions for improvement most welcome.

HH

//Camera Motor sketch for use with LadyAda motor shield and DC motor
//and 2 digital ins which tell motor when to change directions
//Because the analogue pins are available on the motor shield, I am using
//A4 and A5 as digital ins.
#include <AFMotor.h>

int botBut = A5;//pin that receives input from the bottom button
int topBut = A4;//pin that receives input from the top button
int valTop = LOW;//intitalizing top button
int valBot = LOW;//initializing bottom button

int minStep = 300;//least # of steps per 'go'
int maxStep = 700;//maximum # of steps per 'go'
int stepNum = 400;




AF_DCMotor motorD(2, MOTOR12_64KHZ);//DC motor on motor port 2 at 64 khz


void setup() {

Serial.begin(9600);


pinMode(botBut, INPUT);//button to tell motor it is at the end of its run on one side
pinMode(topBut, INPUT);//button to tell motor it is at the end of its run on one side
motorD.setSpeed(100); //set the speed of the motor


}
void loop()
{
  
  valTop = digitalRead(topBut);//check the state of the top button
  valBot = digitalRead(botBut);//check the state of the bottom button
  
  if (valTop == LOW && valBot ==LOW){//If neither button is pressed,
   Serial.println("LOW,LOW");
   Direction();
  }
   else if (valTop == HIGH) {
   for(int i=0;i<200; i++){ //this is long enough to disengage the button
    motorD.run(BACKWARD);
    Serial.println("BACKWARD, TOPHIGH");
  valTop = digitalRead(topBut);
  valBot = digitalRead(botBut);
  
    }
   }
   
  else if (valBot == HIGH){
    for(int i=0;i<200; i++){
      motorD.run(FORWARD);
      Serial.println("FORWARD, BOTHIGH");
  valBot = digitalRead(botBut);
   valTop = digitalRead(topBut);
   
  }
}
  }
   
void Direction(){
    int FB = random (0,2);
  if (FB == 0) {
    GoForward ();
  }
  if (FB == 1) {
    GoBackward ();
  }//step the motor as presecribed byt he "go" funciton
    
    
  }
  

void GoForward(){
  
  stepNum = (int)random(minStep, maxStep);
  for (int i = 0; i< stepNum; i ++){
  valTop = digitalRead(topBut);
  valBot = digitalRead(botBut);
  Serial.println(stepNum);
    
    if(valTop==HIGH){
    for (int i=0; i<200; i++){
      motorD.run(BACKWARD); 
      Serial.println("GFTopHigh");
    }
  }
  
     else if(valBot==HIGH){
    for (int i=0; i<200; i++){
      motorD.run(FORWARD);  
  Serial.println("GFBotHigh");    
    }
     }
    
    else if(valBot==LOW && valTop==LOW){
  motorD.run(FORWARD);
  Serial.println ("FORWARD!");
  }
  }
  
  
  
  }


  
 void GoBackward(){
  
  
  stepNum = (int)random(minStep, maxStep);
  for (int i = 0; i< stepNum; i ++){
  valTop = digitalRead(topBut);
  valBot = digitalRead(botBut);
  Serial.println(stepNum);
  if(valTop==HIGH){
   
    for (int i=0; i<200; i++){
      motorD.run(BACKWARD);
     Serial.println("GBTopHigh");
    }
  }
     else if(valBot==HIGH){
   
    for (int i=0; i<200; i++){
      motorD.run(FORWARD);
      Serial.println("GBBotHigh"); 
    } 
  } 
 else if(valBot==LOW && valTop==LOW){ 
  motorD.run(BACKWARD);
  Serial.println ("BACKWARD!");
  
  }
 }
 }
int botBut = A5;//pin that receives input from the bottom button
int topBut = A4;//pin that receives input from the top button

It occurs to me that these values (and others like them) are unlikely to change - any good reason that they are not defined as constants?

no reason. will change that. thanks!

GoForward and GoBackward are identical except for the last few lines. Consider making a single function that you could call like this:

Go(FORWARD,"Forward");
Go(BACKWARD,"Backward");

Also, in your 200 iteration loops, you could probably turn the motor on once before you enter it. No need to do it on each iteration.