Motors Controlled by Joystick

This code is suppose to control four motors via joystick, but I can't get any of them to work. I very new to this motor coding. I'm not sure if I did something wrong with the code or if the joystick shield does not work. I have an arduino Leonardo with an Adafruit motor shield on top and on top of that a joystick shield. The power supply for the motors is separate too. Also did I do the subroutines right?

#include <Adafruit_MotorShield.h>                     //includes MotorShield library

Adafruit_MotorShield AFMS = Adafruit_MotorShield();   //defines AFMS

Adafruit_DCMotor *motor1 = AFMS.getMotor(1);  // Initiate all the motors
Adafruit_DCMotor *motor2 = AFMS.getMotor(2);
Adafruit_DCMotor *motor3 = AFMS.getMotor(3);
Adafruit_DCMotor *motor4 = AFMS.getMotor(4);
int val0 = 0;           // variable to store the Arm 1 sensor
int val1 = 0;           // variable to store the Arm 2 sensor
int val2 = 0;           // variable to store the Arm 3 sensor
int val3 = 0;           // variable to store the Arm 4 sensor

//JoyStick
const byte PIN_BUTTON_SELECT = 2;   // Select button is triggered when joystick is pressed

const byte PIN_BUTTON_RIGHT = 3;    //Assigns Joystick buttons to pins
const byte PIN_BUTTON_UP = 4;
const byte PIN_BUTTON_DOWN = 5;
const byte PIN_BUTTON_LEFT = 6;

const byte PIN_ANALOG_X = 0;        //Assignes Joystick to pins
const byte PIN_ANALOG_Y = 1;

const int X_THRESHOLD_LOW = 505;    //Creates X threshold values
const int X_THRESHOLD_HIGH = 515;    

const int Y_THRESHOLD_LOW = 500;    //Creates Y threshold values
const int Y_THRESHOLD_HIGH = 510;

int x_position;                     //Creates variable x/y_position
int y_position;

int x_direction;                    //Creates variable x/y_direction
int y_direction;


void input()    // this is the subroutine that waits for the user to hit 4
  {
  int incomingByte = 0;             // for incoming serial data
  motor1->setSpeed(0);              // stop the motor
  Serial.println("Hit 4 for next motion");   
  while (incomingByte != 52)        // 52 is the ASCII code for "4"
    {
  if (Serial.available() > 0) 
         {   
    // read the incoming byte:
    incomingByte = Serial.read();

    // say what you got:
    Serial.print("I received: ");
    Serial.println(incomingByte, DEC);
   }
     }
  }

void light()    // this is the subroutine that waits for the user to hit 1
  {
  int incomingByte = 0;             // for incoming serial data
  motor1->setSpeed(0);              // stop the motor
  Serial.println("Light On/Off");   
  while (incomingByte != 49)        // 49 is the ASCII code for "1"
    {
  if (incomingByte != 49) 
         {   
    // read the incoming byte:
    incomingByte = Serial.read();

    // say what you got:
    Serial.print("Light On ");
    Serial.println(incomingByte, DEC);
    digitalWrite(7, HIGH);
    delay(1000);
   }
     }
  while (incomingByte != 50)        // 50 is the ASCII code for "2"
    {
  if (incomingByte != 50) 
         {   
    // read the incoming byte:
    incomingByte = Serial.read();

    // say what you got:
    Serial.print("Light OFF ");
    Serial.println(incomingByte, DEC);
    digitalWrite(7, LOW);
   }
     }
  }
  
void setup() {
  Serial.begin(9600);           // set up Serial library at 9600 bps
  Serial.println("Robot arm test!");
  motor1->setSpeed(100);        // set the speed to 100/255
  motor2->setSpeed(100);        // do the same for the others...
  motor3->setSpeed(100);    
  motor4->setSpeed(100);
  pinMode(7, OUTPUT);

//JoyStick
  pinMode(PIN_BUTTON_RIGHT, INPUT);           //set Joystick inputs to INPUT and set buttons to HIGH
  digitalWrite(PIN_BUTTON_RIGHT, HIGH);
  
  pinMode(PIN_BUTTON_LEFT, INPUT);  
  digitalWrite(PIN_BUTTON_LEFT, HIGH);
  
  pinMode(PIN_BUTTON_UP, INPUT);  
  digitalWrite(PIN_BUTTON_UP, HIGH);
  
  pinMode(PIN_BUTTON_DOWN, INPUT);  
  digitalWrite(PIN_BUTTON_DOWN, HIGH);
  
  pinMode(PIN_BUTTON_SELECT, INPUT);  
  digitalWrite(PIN_BUTTON_SELECT, HIGH);  
}

void loop()

  {
  void Light();
  
//JoyStick
  x_direction = 0;                            //set x/y_direction values to 0
  y_direction = 0;
 
  x_position = analogRead(PIN_ANALOG_X);      //set values for Joystick x/y_position
  y_position = analogRead(PIN_ANALOG_Y);
 
 
  if (x_position > X_THRESHOLD_HIGH) {        //set threshold values for Joystick (make the Joystick less sensitive)
    x_direction = 1;
  } else if (x_position < X_THRESHOLD_LOW) {
    x_direction = -1;
  }

  if (y_position > Y_THRESHOLD_HIGH) {
    y_direction = 1;
  } else if (y_position < Y_THRESHOLD_LOW) {
    y_direction = -1;
  }
   
 
  if (x_direction == -1) {
      if (y_direction == -1) {
        Serial.println("left-down");
        
      } else if (y_direction == 0) {
        Serial.println("left");
        motor1->setSpeed(100);          //run motor1 at 100 BACKWARD
        motor1->run(BACKWARD);
      } else {
        // y_direction == 1
        Serial.println("left-up");      
      }  
  } else if (x_direction == 0) {
      if (y_direction == -1) {
        Serial.println("down");
        motor2->setSpeed(100);          //run motor2 at 100 FORWARD
        motor2->run(FORWARD);
      } else if (y_direction == 0) {
        Serial.println("centered");
      } else {
        // y_direction == 1
        Serial.println("up");
        motor2->setSpeed(100);          //run motor2 at 100 BACKWARD
        motor2->run(BACKWARD);      
      }
  } else {
      // x_direction == 1
      if (y_direction == -1) {
        Serial.println("right-down");
      } else if (y_direction == 0) {
        Serial.println("right");
        motor1->setSpeed(100);          //run motor1 at 100 FORWARD
        motor1->run(FORWARD);
      } else {
        // y_direction == 1
        Serial.println("right-up");      
      }
  }
//print button direction to Serial and move motors
  Serial.print("l:");
  Serial.print(digitalRead(PIN_BUTTON_LEFT));
  motor4->setSpeed(100);               //run motor4 at 100 FORWARD
  motor4->run(FORWARD);
  Serial.print(" ");
 
  Serial.print("r:");
  Serial.print(digitalRead(PIN_BUTTON_RIGHT));
  motor4->setSpeed(100);              //run motor4 at 100 BACKWARD
  motor4->run(BACKWARD);
  Serial.print(" ");
 
  Serial.print("u:");
  Serial.print(digitalRead(PIN_BUTTON_UP));
  motor3->setSpeed(100);              //run motor3 at 100 FORWARD
  motor3->run(FORWARD);
  Serial.print(" ");
 
  Serial.print("d:");
  Serial.print(digitalRead(PIN_BUTTON_DOWN));
  motor3->setSpeed(100);              //run motor3 at 100 BACKWARD
  motor3->run(BACKWARD);
  Serial.print(" ");
  
  input();  // wait for return to be 4
  }

You need to read the serial data in ONE place. Not all over the place. What you are doing now is like having 4 of your friends read this response, a couple of letters at a time. No one gets a complete picture of the answer.

Using Tools + Auto Format would be a good idea, too. That crappy indenting does not make it easy to read your code.

  void Light();

Why is there a function prototype in the loop() function?

//print button direction to Serial and move motors

You've already done that. Why are you doing it again?

How would I read the Serial data in one place? the Function void Light(); is there because there is also an LED that I want to turn on and off with the press of a button on my keyboard. The print button direction to serial was to do two things, one to make sure that the motors were going the correct direction and two to see if the buttons were working at all on the joystick shield. I did the auto formatting, would you like me to re-upload the code?

would you like me to re-upload the code?

Of course.

Have a look at the examples in Serial Input Basics - simple reliable ways to receive data.

You may also find some useful ideas in Planning and Implementing a Program

...R

On line 120:

 void Light();

Should be:

 Light();

Here is the code reformatted, but other than that I have not made any changes.

outsider:
On line 120:

 void Light();

Should be:

 Light();

I don't thats right because then it is not declared as it is a sub function.

When @PaulS said to read all of the serial data in one place how would I do that; would it be something like combining all the sub functions into one function?

I also have yet to take a look at the Serial input basics page, but I will soon. Also did I do the sub functions that are activated by a key on the keyboard correctly?

#include <Adafruit_MotorShield.h>                     //includes MotorShield library

Adafruit_MotorShield AFMS = Adafruit_MotorShield();   //defines AFMS

Adafruit_DCMotor *motor1 = AFMS.getMotor(1);  // Initiate all the motors
Adafruit_DCMotor *motor2 = AFMS.getMotor(2);
Adafruit_DCMotor *motor3 = AFMS.getMotor(3);
Adafruit_DCMotor *motor4 = AFMS.getMotor(4);
int val0 = 0;           // variable to store the Arm 1 sensor
int val1 = 0;           // variable to store the Arm 2 sensor
int val2 = 0;           // variable to store the Arm 3 sensor
int val3 = 0;           // variable to store the Arm 4 sensor

//JoyStick
const byte PIN_BUTTON_SELECT = 2;   // Select button is triggered when joystick is pressed

const byte PIN_BUTTON_RIGHT = 3;    //Assigns Joystick buttons to pins
const byte PIN_BUTTON_UP = 4;
const byte PIN_BUTTON_DOWN = 5;
const byte PIN_BUTTON_LEFT = 6;

const byte PIN_ANALOG_X = 0;        //Assignes Joystick to pins
const byte PIN_ANALOG_Y = 1;

const int X_THRESHOLD_LOW = 505;    //Creates X threshold values
const int X_THRESHOLD_HIGH = 515;

const int Y_THRESHOLD_LOW = 500;    //Creates Y threshold values
const int Y_THRESHOLD_HIGH = 510;

int x_position;                     //Creates variable x/y_position
int y_position;

int x_direction;                    //Creates variable x/y_direction
int y_direction;


void input()    // this is the subroutine that waits for the user to hit 4
{
  int incomingByte = 0;             // for incoming serial data
  motor1->setSpeed(0);              // stop the motor
  Serial.println("Hit 4 for next motion");
  while (incomingByte != 52)        // 52 is the ASCII code for "4"
  {
    if (Serial.available() > 0)
    {
      // read the incoming byte:
      incomingByte = Serial.read();

      // say what you got:
      Serial.print("I received: ");
      Serial.println(incomingByte, DEC);
    }
  }
}

void light()    // this is the subroutine that waits for the user to hit 1
{
  int incomingByte = 0;             // for incoming serial data
  motor1->setSpeed(0);              // stop the motor
  Serial.println("Light On/Off");
  while (incomingByte != 49)        // 49 is the ASCII code for "1"
  {
    if (incomingByte != 49)
    {
      // read the incoming byte:
      incomingByte = Serial.read();

      // say what you got:
      Serial.print("Light On ");
      Serial.println(incomingByte, DEC);
      digitalWrite(7, HIGH);
      delay(1000);
    }
  }
  while (incomingByte != 50)        // 50 is the ASCII code for "2"
  {
    if (incomingByte != 50)
    {
      // read the incoming byte:
      incomingByte = Serial.read();

      // say what you got:
      Serial.print("Light OFF ");
      Serial.println(incomingByte, DEC);
      digitalWrite(7, LOW);
    }
  }
}

void setup() {
  Serial.begin(9600);           // set up Serial library at 9600 bps
  Serial.println("Robot arm test!");
  motor1->setSpeed(100);        // set the speed to 100/255
  motor2->setSpeed(100);        // do the same for the others...
  motor3->setSpeed(100);
  motor4->setSpeed(100);
  pinMode(7, OUTPUT);

  //JoyStick
  pinMode(PIN_BUTTON_RIGHT, INPUT);           //set Joystick inputs to INPUT and set buttons to HIGH
  digitalWrite(PIN_BUTTON_RIGHT, HIGH);

  pinMode(PIN_BUTTON_LEFT, INPUT);
  digitalWrite(PIN_BUTTON_LEFT, HIGH);

  pinMode(PIN_BUTTON_UP, INPUT);
  digitalWrite(PIN_BUTTON_UP, HIGH);

  pinMode(PIN_BUTTON_DOWN, INPUT);
  digitalWrite(PIN_BUTTON_DOWN, HIGH);

  pinMode(PIN_BUTTON_SELECT, INPUT);
  digitalWrite(PIN_BUTTON_SELECT, HIGH);
}

void loop()

{
  void Light();

  //JoyStick
  x_direction = 0;                            //set x/y_direction values to 0
  y_direction = 0;

  x_position = analogRead(PIN_ANALOG_X);      //set values for Joystick x/y_position
  y_position = analogRead(PIN_ANALOG_Y);


  if (x_position > X_THRESHOLD_HIGH) {        //set threshold values for Joystick (make the Joystick less sensitive)
    x_direction = 1;
  } else if (x_position < X_THRESHOLD_LOW) {
    x_direction = -1;
  }

  if (y_position > Y_THRESHOLD_HIGH) {
    y_direction = 1;
  } else if (y_position < Y_THRESHOLD_LOW) {
    y_direction = -1;
  }


  if (x_direction == -1) {
    if (y_direction == -1) {
      Serial.println("left-down");

    } else if (y_direction == 0) {
      Serial.println("left");
      motor1->setSpeed(100);          //run motor1 at 100 BACKWARD
      motor1->run(BACKWARD);
    } else {
      // y_direction == 1
      Serial.println("left-up");
    }
  } else if (x_direction == 0) {
    if (y_direction == -1) {
      Serial.println("down");
      motor2->setSpeed(100);          //run motor2 at 100 FORWARD
      motor2->run(FORWARD);
    } else if (y_direction == 0) {
      Serial.println("centered");
    } else {
      // y_direction == 1
      Serial.println("up");
      motor2->setSpeed(100);          //run motor2 at 100 BACKWARD
      motor2->run(BACKWARD);
    }
  } else {
    // x_direction == 1
    if (y_direction == -1) {
      Serial.println("right-down");
    } else if (y_direction == 0) {
      Serial.println("right");
      motor1->setSpeed(100);          //run motor1 at 100 FORWARD
      motor1->run(FORWARD);
    } else {
      // y_direction == 1
      Serial.println("right-up");
    }
  }
  //print button direction to Serial and move motors
  Serial.print("l:");
  Serial.print(digitalRead(PIN_BUTTON_LEFT));
  motor4->setSpeed(100);               //run motor4 at 100 FORWARD
  motor4->run(FORWARD);
  Serial.print(" ");

  Serial.print("r:");
  Serial.print(digitalRead(PIN_BUTTON_RIGHT));
  motor4->setSpeed(100);              //run motor4 at 100 BACKWARD
  motor4->run(BACKWARD);
  Serial.print(" ");

  Serial.print("u:");
  Serial.print(digitalRead(PIN_BUTTON_UP));
  motor3->setSpeed(100);              //run motor3 at 100 FORWARD
  motor3->run(FORWARD);
  Serial.print(" ");

  Serial.print("d:");
  Serial.print(digitalRead(PIN_BUTTON_DOWN));
  motor3->setSpeed(100);              //run motor3 at 100 BACKWARD
  motor3->run(BACKWARD);
  Serial.print(" ");

  input();  // wait for return to be hit
}

When @PaulS said to read all of the serial data in one place how would I do that; would it be something like combining all the sub functions into one function?

Suppose that you had one function called readSerialData(). How hard would THAT be to implement?

Here is the final code. It works... Thanks so much for the help everyone. Except for the light, but that doesn't really matter to much.

VANiSH

#include <Adafruit_MotorShield.h>                     //includes MotorShield library

Adafruit_MotorShield AFMS = Adafruit_MotorShield();   //defines AFMS

Adafruit_DCMotor *motor1 = AFMS.getMotor(1);  // Initiate all the motors
Adafruit_DCMotor *motor2 = AFMS.getMotor(2);
Adafruit_DCMotor *motor3 = AFMS.getMotor(3);
Adafruit_DCMotor *motor4 = AFMS.getMotor(4);
int val0 = 0;           // variable to store the Arm 1 sensor
int val1 = 0;           // variable to store the Arm 2 sensor
int val2 = 0;           // variable to store the Arm 3 sensor
int val3 = 0;           // variable to store the Arm 4 sensor

//JoyStick
const byte PIN_BUTTON_SELECT = 2;   // Select button is triggered when joystick is pressed

const byte PIN_BUTTON_RIGHT = 3;    //Assigns Joystick buttons to pins
const byte PIN_BUTTON_UP = 4;
const byte PIN_BUTTON_DOWN = 5;
const byte PIN_BUTTON_LEFT = 6;

const byte PIN_ANALOG_X = 0;        //Assignes Joystick to pins
const byte PIN_ANALOG_Y = 1;

const int X_THRESHOLD_LOW = 450;    //Creates X threshold values
const int X_THRESHOLD_HIGH = 550;

const int Y_THRESHOLD_LOW = 450;    //Creates Y threshold values
const int Y_THRESHOLD_HIGH = 550;

int x_position;                     //Creates variable x/y_position
int y_position;

int x_direction;                    //Creates variable x/y_direction
int y_direction;

char receivedChar;
//boolean newData = false;


/*void readSerialData() {
  if (Serial.available() > 0) {
    receivedChar = Serial.read();
  }
  if (receivedChar != 49) {             //49 is ASCII number for 1
    Serial.print("Light On");
    Serial.println(receivedChar, DEC);
    digitalWrite(7, HIGH);
  }
  if (receivedChar != 50) {             //50 is ASCII number for 2
    Serial.print("Light Off");
    Serial.println(receivedChar, DEC);
    digitalWrite(7, LOW);
  }
  }
*/
void setup() {
  Serial.begin(9600);           // set up Serial library at 9600 bps
  AFMS.begin();                 // set up AFMS at 1.6KHz
  Serial.println("Robot Arm Script");
  motor1->setSpeed(100);        // set the speed to 100/255
  motor2->setSpeed(100);        // do the same for the others...
  motor3->setSpeed(100);
  motor4->setSpeed(100);
  pinMode(7, OUTPUT);

  //JoyStick
  pinMode(PIN_BUTTON_RIGHT, INPUT);           //set Joystick inputs to INPUT and set buttons to HIGH
  digitalWrite(PIN_BUTTON_RIGHT, HIGH);

  pinMode(PIN_BUTTON_LEFT, INPUT);
  digitalWrite(PIN_BUTTON_LEFT, HIGH);

  pinMode(PIN_BUTTON_UP, INPUT);
  digitalWrite(PIN_BUTTON_UP, HIGH);

  pinMode(PIN_BUTTON_DOWN, INPUT);
  digitalWrite(PIN_BUTTON_DOWN, HIGH);

  pinMode(PIN_BUTTON_SELECT, INPUT);
  digitalWrite(PIN_BUTTON_SELECT, HIGH);
}

void loop() {
  //  readSerialData();

  //JoyStick
  x_direction = 0;                            //set x/y_direction values to 0
  y_direction = 0;

  x_position = analogRead(PIN_ANALOG_X);      //set values for Joystick x/y_position
  y_position = analogRead(PIN_ANALOG_Y);

  if (x_position > X_THRESHOLD_HIGH) {        //set threshold values for Joystick (make the Joystick less sensitive)
    x_direction = 1;
  } else if (x_position < X_THRESHOLD_LOW) {
    x_direction = -1;
  }

  if (y_position > Y_THRESHOLD_HIGH) {
    y_direction = 1;
  } else if (y_position < Y_THRESHOLD_LOW) {
    y_direction = -1;
  }

  if (x_direction == -1) {
    if (y_direction == -1) {
      Serial.println("left-down");

    } else if (y_direction == 0) {
      Serial.println("left");
      motor1->setSpeed(100);          //run motor1 at 100 BACKWARD
      motor1->run(BACKWARD);
    } else {
      // y_direction == 1
      Serial.println("left-up");
    }
  } else if (x_direction == 0) {
    if (y_direction == -1) {
      Serial.println("down");
      motor2->setSpeed(100);          //run motor2 at 100 FORWARD
      motor2->run(FORWARD);
    } else if (y_direction == 0) {
      Serial.println("centered");
      motor1->run(RELEASE);
      motor2->run(RELEASE);
    } else {
      // y_direction == 1
      Serial.println("up");
      motor2->setSpeed(100);          //run motor2 at 100 BACKWARD
      motor2->run(BACKWARD);
    }
  } else {
    // x_direction == 1
    if (y_direction == -1) {
      Serial.println("right-down");
    } else if (y_direction == 0) {
      Serial.println("right");
      motor1->setSpeed(100);          //run motor1 at 100 FORWARD
      motor1->run(FORWARD);
    } else {
      // y_direction == 1
      Serial.println("right-up");
    }
  }
  //print button direction to Serial and move motors
  if (digitalRead(PIN_BUTTON_LEFT) == LOW) {
    Serial.println("l:");
    motor4->setSpeed(100);               //run motor4 at 100 FORWARD
    motor4->run(FORWARD);
    Serial.print(" ");
  }
  if (digitalRead(PIN_BUTTON_RIGHT) == LOW) {
    Serial.println("r:");
    motor4->setSpeed(100);              //run motor4 at 100 BACKWARD
    motor4->run(BACKWARD);
    Serial.print(" ");
  }
  if (digitalRead(PIN_BUTTON_UP) == LOW) {
    Serial.println("u:");
    motor3->setSpeed(100);              //run motor3 at 100 FORWARD
    motor3->run(BACKWARD);
    Serial.print(" ");
  }
  if (digitalRead(PIN_BUTTON_DOWN) == LOW) {
    Serial.println("d:");
    motor3->setSpeed(100);              //run motor3 at 100 BACKWARD
    motor3->run(FORWARD);
    Serial.print(" ");
  }
  if (digitalRead(PIN_BUTTON_LEFT) == HIGH) {
    delay(250);
    motor4->run(RELEASE);
  }
  if (digitalRead(PIN_BUTTON_RIGHT) == HIGH) {
    delay(250);
    motor4->run(RELEASE);
  }
  if (digitalRead(PIN_BUTTON_UP) == HIGH) {
    delay(250);
    motor3->run(RELEASE);
  }
  if (digitalRead(PIN_BUTTON_DOWN) == HIGH) {
    delay(250);
    motor3->run(RELEASE);
  }
}