Struggling With Joystick Code

Hi all,

This is my first post on this forum and I am very new to coding. The purpose of my project is to build a remote controlled car that I can drive via a Joystick Shield v1.a. So far the project is going well due to the fact that I found a basic structure code for the Joystick Shield which I used to write code for control over two motors on my car via an H-bridge setup. The structure I have now works flawlessly and can operate both wheels forward, backward, and opposite directions (for turning), using the joystick. The problem I ran into is that I have very little control over the car with this setup because the motors spin too fast. My goal is to write some code that will allow me to control the motors using PWM. I already have the setup wired correctly and have the wires in pins 5,6,9,10 (in order to achieve PWM). Below is the code I currently am using to drive the car (using digital inputs instead of analog). See Below this for the library code used in this example. I have spend days working on this and still have no clue how to get the program to read the coordinates of the joystick and regulate the speed depending on how far forward, backward, etc. the joystick is. Thanks for taking the time to help!!

Here is the code I am currently using:

/**
   JoystickShield - Arduino Library for JoystickShield (http://hardwarefun.com/projects/joystick-shield)

   Copyright 2011  Sudar Muthu  (email : sudar@sudarmuthu.com)

 * ----------------------------------------------------------------------------
 * "THE BEER-WARE LICENSE" (Revision 42):
 * <sudar@sudarmuthu.com> wrote this file. As long as you retain this notice you
 * can do whatever you want with this stuff. If we meet some day, and you think
 * this stuff is worth it, you can buy me a beer or coffee in return - Sudar
 * ----------------------------------------------------------------------------
 * 2014 edit by Markus Mücke, muecke.ma(a)gmail.com
 * Changes for JoysikShield V1.2
 * added a function to read the amplitude of the joystick
 * added a auto calibrate function for 3.3V and 5V mode
 *
 * Added functions:
 *  Functions for F and E Button
 *  Calibrate Joystick
 *  xAmplitude
 *  yAmplitude
 */

/**
 * Before running this example, stack the JoystickShield on top of Arduino board
 *
 */
// --------------------------------------------------------------------------- Motors
int motor_left[] = {5, 6};
int motor_right[] = {9, 10};
int motor_fan[] = {12, 13};


#include <JoystickShield.h> // include JoystickShield Library

JoystickShield joystickShield; // create an instance of JoystickShield object

void setup() {
    Serial.begin(9600);
    
    int i;
    for(i = 0; i < 2; i++){
    pinMode(motor_left[i], OUTPUT);
    pinMode(motor_right[i], OUTPUT);
    pinMode(motor_fan[i], OUTPUT);
  
    }
    
    delay(100);
    // new calibration function
    joystickShield.calibrateJoystick();

	// predefined Joystick to Pins 0 and 1.
	// Change it if you are using a different shield
	// setJoystickPins(0, 1);
  
	// predefined buttons to the following pins.
	// change it if you are using a different shield.
	// setButtonPins(pinJoystickButton, pinUp, pinRight, pinDown, pinLeft, pinF, pinE);
	// to deactivate a button use a pin outside of the range of the arduino e.g. 255, but not above
	// setButtonPins(8, 2, 3, 4, 5, 7, 6);
}
  // --------------------------------------------------------------------------- Drive

void fan_suck(){
digitalWrite(motor_fan[0], HIGH);
digitalWrite(motor_fan[1], LOW);
}

void fan_blow(){
digitalWrite(motor_fan[0], LOW);
digitalWrite(motor_fan[1], HIGH);
}

void fan_stop(){
digitalWrite(motor_fan[0], LOW);
digitalWrite(motor_fan[1], LOW);
delay(25);
}

void motor_stop(){
digitalWrite(motor_left[0], LOW); 
digitalWrite(motor_left[1], LOW); 

digitalWrite(motor_right[0], LOW); 
digitalWrite(motor_right[1], LOW);
delay(25);
}

void drive_forward(){
digitalWrite(motor_left[0], HIGH); 
digitalWrite(motor_left[1], LOW); 

digitalWrite(motor_right[0], HIGH); 
digitalWrite(motor_right[1], LOW); 
}

void drive_backward(){
digitalWrite(motor_left[0], LOW); 
digitalWrite(motor_left[1], HIGH); 

digitalWrite(motor_right[0], LOW); 
digitalWrite(motor_right[1], HIGH); 
}

void turn_left(){
digitalWrite(motor_left[0], LOW); 
digitalWrite(motor_left[1], HIGH); 

digitalWrite(motor_right[0], HIGH); 
digitalWrite(motor_right[1], LOW);
}

void turn_right(){
digitalWrite(motor_left[0], HIGH); 
digitalWrite(motor_left[1], LOW); 

digitalWrite(motor_right[0], LOW); 
digitalWrite(motor_right[1], HIGH); 
}
  

void loop() {
  joystickShield.processEvents(); // process events
  
  if (joystickShield.isCenter()){
     Serial.println("Center") ; 
     motor_stop();
     fan_stop();
  }
  
  if (joystickShield.isUp()) {
     Serial.println("Up") ;
     drive_forward();
     fan_blow();
  }
  
  if (joystickShield.isRightUp()) {
     Serial.println("RightUp") ;
  }

  if (joystickShield.isRight()) {
     Serial.println("Right") ;
     turn_right();
  }

  if (joystickShield.isRightDown()) {
     Serial.println("RightDown") ;
  }

  if (joystickShield.isDown()) {
     Serial.println("Down") ;
     drive_backward();
     fan_suck();
     
  }

  if (joystickShield.isLeftDown()) {
     Serial.println("LeftDown") ;
  }

  if (joystickShield.isLeft()) {
     Serial.println("Left") ;
     turn_left();
  }

  if (joystickShield.isLeftUp()) {
     Serial.println("LeftUp") ;
  }

  if (joystickShield.isJoystickButton()) {
     Serial.println("Joystick Clicked") ;
  }

  if (joystickShield.isUpButton()) {
     Serial.println("Up Button Clicked") ;
  }

  if (joystickShield.isRightButton()) {
     Serial.println("Right Button Clicked") ;
  }

  if (joystickShield.isDownButton()) {
     Serial.println("Down Button Clicked") ;
  }

  if (joystickShield.isLeftButton()) {
     Serial.println("Left Button Clicked") ;
  }

  // new eventfunctions
  if (joystickShield.isEButton()) {
     Serial.println("E Button Clicked") ;
  }

  if (joystickShield.isFButton()) {
     Serial.println("F Button Clicked") ;
  }  
  
  if (joystickShield.isNotCenter()){
     Serial.println("NotCenter") ;
  }

  // new position functions
  Serial.print("x ");	Serial.print(joystickShield.xAmplitude());Serial.print(" y ");Serial.println(joystickShield.yAmplitude());

  delay(500);
}

I am adding the library code in as a reply because I exceeded the character limit :confused:

/**
   JoystickShield - Arduino Library for JoystickShield (http://hardwarefun.com/projects/joystick-shield)

 *
 * ----------------------------------------------------------------------------
 * "THE BEER-WARE LICENSE" (Revision 42):
 * <sudar@sudarmuthu.com> wrote this file. As long as you retain this notice you
 * can do whatever you want with this stuff. If we meet some day, and you think
 * this stuff is worth it, you can buy me a beer or coffee in return - Sudar
 * ----------------------------------------------------------------------------
 * 2014 edit by Markus Mücke, muecke.ma(a)gmail.com
 * Changes for JoysikShield V1.2
 * added a function to read the amplitude of the joystick
 * added a auto calibrate function for 3.3V and 5V mode
 *
 * Added functions:
 *  Functions for F and E Button
 *  Calibrate Joystick
 *  xAmplitude
 *  yAmplitude
 */

#ifndef JoystickShield_H
#define JoystickShield_H

#define CENTERTOLERANCE 5

// Compatibility for Arduino 1.0

#if ARDUINO >= 100
    #include <Arduino.h>
#else    
    #include <WProgram.h>
#endif

/**
 * Enum to hold the different states of the Joystick
 *
 */
enum JoystickStates {
    CENTER,  // 0
    UP,
    RIGHT_UP,
    RIGHT,
    RIGHT_DOWN,
    DOWN,
    LEFT_DOWN,
    LEFT,
    LEFT_UP   //8
};

/**
 * Enum to hold the button states
 *
 */
enum ButtonStates {
    NO_BUTTON,     //0
    JOYSTICK_BUTTON,
    UP_BUTTON,
    RIGHT_BUTTON,
    DOWN_BUTTON,
    LEFT_BUTTON,    //5
	F_BUTTON,
	E_BUTTON
};

/**
 * Class to encapsulate JoystickShield
 */
class JoystickShield {

public:

    JoystickShield(); // constructor

    void setJoystickPins (byte pinX, byte pinY);
    void setButtonPins(byte pinSelect, byte pinUp, byte pinRight, byte pinDown, byte pinLeft, byte pinF, byte pinE);
    void setThreshold(int xLow, int xHigh, int yLow, int yHigh);

    void processEvents();
    void processCallbacks();
	
	void calibrateJoystick();

    // Joystick events
    bool isCenter();
    bool isUp();
    bool isRightUp();
    bool isRight();
    bool isRightDown();
    bool isDown();
    bool isLeftDown();
    bool isLeft();
    bool isLeftUp();
	bool isNotCenter();
	
	// Joystick coordinates
	int xAmplitude();
	int yAmplitude();

    // Button events
    bool isJoystickButton();
    bool isUpButton();
    bool isRightButton();
    bool isDownButton();
    bool isLeftButton();
	bool isFButton();
	bool isEButton();

    // Joystick callbacks
    void onJSCenter(void (*centerCallback)(void));
    void onJSUp(void (*upCallback)(void));
    void onJSRightUp(void (*rightUpCallback)(void));
    void onJSRight(void (*rightCallback)(void));
    void onJSRightDown(void (*rightDownCallback)(void));
    void onJSDown(void (*downCallback)(void));
    void onJSLeftDown(void (*leftDownCallback)(void));
    void onJSLeft(void (*leftCallback)(void));
    void onJSLeftUp(void (*leftUpCallback)(void));
	void onJSnotCenter(void (*notCenterCallback)(void));

    // Button callbacks
    void onJoystickButton(void (*jsButtonCallback)(void));
    void onUpButton(void (*upButtonCallback)(void));
    void onRightButton(void (*rightButtonCallback)(void));
    void onDownButton(void (*downButtonCallback)(void));
    void onLeftButton(void (*leftButtonCallback)(void));
	void onFButton(void (*FButtonCallback)(void));
	void onEButton(void (*EButtonCallback)(void));
	
private:

    // threshold values
    int x_threshold_low;
    int x_threshold_high;
    int y_threshold_low;
    int y_threshold_high;

    // joystick pins
    byte pin_analog_x;
    byte pin_analog_y;

    //button pins
    byte pin_joystick_button;
    byte pin_up_button;
    byte pin_right_button;
    byte pin_down_button;
    byte pin_left_button;
	byte pin_F_button;
	byte pin_E_button;
	
	// joystick
	byte joystikStroke;
	int x_position;
	int y_position;

    //current states of Joystick
    JoystickStates currentStatus;

    //current button states
    ButtonStates currentButton;

    // Joystick callbacks
    void (*centerCallback)(void);
    void (*upCallback)(void);
    void (*rightUpCallback)(void);
    void (*rightCallback)(void);
    void (*rightDownCallback)(void);
    void (*downCallback)(void);
    void (*leftDownCallback)(void);
    void (*leftCallback)(void);
    void (*leftUpCallback)(void);
	void (*notCenterCallback)(void);

    // Button callbacks
    void (*jsButtonCallback)(void);
    void (*upButtonCallback)(void);
    void (*rightButtonCallback)(void);
    void (*downButtonCallback)(void);
    void (*leftButtonCallback)(void);
	void (*FButtonCallback)(void);
	void (*EButtonCallback)(void);
	

    // helper functions
    void clearButtonStates();
    void initializeCallbacks();
};

#endif

Here is the last of the library code (It will be in two parts)

#include "JoystickShield.h"

/**
 * Constructor
 *
 */
JoystickShield::JoystickShield() {
    // define some threshold values. Change them if your Joystick is different
    setThreshold(510, 530, 510, 540);

    // Sparkfun Joystick shield connects the Joystick to Pins 0 and 1.
    // Change it if you are using a different shield
    setJoystickPins(0, 1);
	
    // Sparkfun Joystick shield connects the buttons to the following pins.
    // change it if you are using a different shield.
    setButtonPins(8, 2, 3, 4, 5, 7, 6);

    // by default set the position to centered
    currentStatus = CENTER;

    // by default set the button state to NO_BUTTON
    currentButton = NO_BUTTON;

    // initialize all callback function pointers to NULL
    initializeCallbacks();
}

/**
 * Set Analyog pins which are connected to the Joystick
 *
 */
void JoystickShield::setJoystickPins(byte pinX, byte pinY) {
    pin_analog_x = pinX;
    pin_analog_y = pinY;

    // set Joystick pins to input mode
    pinMode(pin_analog_x, INPUT);
    pinMode(pin_analog_y, INPUT);
}

/**
 * Set the pins used by the buttons
 * to deactivate a button use a pin outside of the range of the arduino e.g. 255
 */
void JoystickShield::setButtonPins(byte pinSelect, byte pinUp, byte pinRight, byte pinDown, byte pinLeft, byte pinF, byte pinE)  {
    pin_joystick_button = pinSelect;
    pin_up_button       = pinUp;
    pin_right_button    = pinRight;
    pin_down_button     = pinDown;
    pin_left_button     = pinLeft;
	pin_F_button		= pinF;
	pin_E_button		= pinE;

    // set Button pins to input mode
    pinMode(pin_joystick_button, INPUT);
    pinMode(pin_up_button      , INPUT);
    pinMode(pin_right_button   , INPUT);
    pinMode(pin_down_button    , INPUT);
    pinMode(pin_left_button    , INPUT);
	pinMode(pin_E_button	   , INPUT);
	pinMode(pin_F_button       , INPUT);

    // Enable "pull-up resistors" for buttons
    digitalWrite(pin_joystick_button, HIGH);
    digitalWrite(pin_up_button      , HIGH);
    digitalWrite(pin_right_button   , HIGH);
    digitalWrite(pin_down_button    , HIGH);
    digitalWrite(pin_left_button    , HIGH);
	digitalWrite(pin_F_button       , HIGH);
	digitalWrite(pin_E_button       , HIGH);
}

/**
 * Configure threshold values for Joystick
 *
 */
void JoystickShield::setThreshold(int xLow, int xHigh, int yLow, int yHigh) {
    x_threshold_low  = xLow;
    x_threshold_high = xHigh;
    y_threshold_low  = yLow;
    y_threshold_high = yHigh;
}

/**
 * Calibrate Joystick
 *
 */
void JoystickShield::calibrateJoystick()  {
	byte i;

	// calibrate x
	int xCenter = 0;
	for(i = 0; i<10; i++)
		xCenter += analogRead(pin_analog_x);
	xCenter /= i;

	// calibrate y
	int yCenter = 0;
	for(i = 0; i<10; i++)
		yCenter += analogRead(pin_analog_y);
	yCenter /= i;

	// save Stroke of Joystick
	joystikStroke = max(pin_analog_x, pin_analog_y)*1.01;

	// set Center with tolerance
	setThreshold(xCenter-CENTERTOLERANCE, xCenter+CENTERTOLERANCE, yCenter-CENTERTOLERANCE, yCenter+CENTERTOLERANCE);
}

/**
 * Process Events. This should be called in the loop()
 *
 */
void JoystickShield::processEvents() {
    int x_direction = 0;
    int y_direction = 0;

    // read from Joystick pins
    x_position = analogRead(pin_analog_x);
    y_position = analogRead(pin_analog_y);

    // determine Joystick direction
    if (x_position > x_threshold_high) {
        x_direction = 1;
		x_position = map(x_position, x_threshold_high,x_threshold_high+x_threshold_low,0,100);
		x_position = constrain(x_position,0,100);
	} else if (x_position < x_threshold_low) {
        x_direction = -1;
		x_position = map(x_position, 0,x_threshold_low,-100,0);
    } else
		x_position = 0;

    if (y_position > y_threshold_high) {
        y_direction = 1;
		y_position = map(y_position, y_threshold_high,y_threshold_high+y_threshold_low,0,100);
		y_position = constrain(y_position,0,100);
    } else if (y_position < y_threshold_low) {
        y_direction = -1;
		y_position = map(y_position, 0,y_threshold_low,-100,0);
    } else
		y_position = 0;
		

    if (x_direction == -1) {
        if (y_direction == -1) {
            currentStatus = LEFT_DOWN;
        } else if (y_direction == 0) {
            currentStatus = LEFT;
        } else {
            currentStatus = LEFT_UP;
        }
    } else if (x_direction == 0) {
        if (y_direction == -1) {
            currentStatus = DOWN;
        } else if (y_direction == 0) {
            currentStatus = CENTER;
        } else {
            currentStatus = UP;
        }
    } else {
        if (y_direction == -1) {
            currentStatus = RIGHT_DOWN;
        } else if (y_direction == 0) {
            currentStatus = RIGHT;
        } else {
            currentStatus = RIGHT_UP;
        }
    }
	
    // Determine which buttons were pressed
    if (digitalRead(pin_joystick_button) == LOW) {
        currentButton = JOYSTICK_BUTTON;
    }

    if (digitalRead(pin_up_button) == LOW) {
        currentButton = UP_BUTTON;
    }

    if (digitalRead(pin_right_button) == LOW) {
        currentButton = RIGHT_BUTTON;
    }

    if (digitalRead(pin_down_button) == LOW) {
        currentButton = DOWN_BUTTON;
    }

    if (digitalRead(pin_left_button) == LOW) {
        currentButton = LEFT_BUTTON;
    }

	if (digitalRead(pin_F_button) == LOW) {
		currentButton = F_BUTTON;
	}

	if (digitalRead(pin_E_button) == LOW) {
		currentButton = E_BUTTON;
	}	
}


void JoystickShield::processCallbacks() {
    processEvents();

    // Joystick Callbacks
    if (isCenter() && centerCallback != NULL) {
        centerCallback();
    }

    if (isUp() && upCallback != NULL) {
        upCallback();
    }

    if (isRightUp() && rightUpCallback != NULL) {
        rightUpCallback();
    }

    if (isRight() && rightCallback != NULL) {
        rightCallback();
    }

    if (isRightDown() && rightDownCallback != NULL) {
        rightDownCallback();
    }

    if (isDown() && downCallback != NULL) {
        downCallback();
    }

    if (isLeftDown() && leftDownCallback != NULL) {
        leftDownCallback();
    }

    if (isLeft() && leftCallback != NULL) {
        leftCallback();
    }

    if (isLeftUp() && leftUpCallback != NULL) {
        leftUpCallback();
    }
	
	if (isNotCenter() && notCenterCallback != NULL) {
		notCenterCallback();
	}
		
    // Button Callbacks
    if (isJoystickButton() && jsButtonCallback != NULL) {
        jsButtonCallback();
    }

    if (isUpButton() && upButtonCallback != NULL) {
        upButtonCallback();
    }

    if (isRightButton() && rightButtonCallback != NULL) {
        rightButtonCallback();
    }

    if (isDownButton() && downButtonCallback != NULL) {
        downButtonCallback();
    }

    if (isLeftButton() && leftButtonCallback != NULL) {
        leftButtonCallback();
    }
	
	if (isFButton() && FButtonCallback != NULL) {
        FButtonCallback();
    }
	
	if (isEButton() && EButtonCallback != NULL) {
		EButtonCallback();
	}

}

Sorry about this guys this is the last one. Its the second half of the second library.

/**
 * Joystick in Center status
 *
 */
bool JoystickShield::isCenter() {
    if (currentStatus == CENTER ) {
        return true;
    } else {
        return false;
    }
}

/**
 * Joystick in Up status
 *
 */
bool JoystickShield::isUp() {
    if (currentStatus == UP ) {
        return true;
    } else {
        return false;
    }
}

/**
 * Joystick in Right Up status
 *
 */
bool JoystickShield::isRightUp() {
    if (currentStatus == RIGHT_UP ) {
        return true;
    } else {
        return false;
    }
}

/**
 * Joystick in Right status
 *
 */
bool JoystickShield::isRight() {
    if (currentStatus == RIGHT ) {
        return true;
    } else {
        return false;
    }
}

/**
 * Joystick in Right Down status
 *
 */
bool JoystickShield::isRightDown() {
    if (currentStatus == RIGHT_DOWN ) {
        return true;
    } else {
        return false;
    }
}

/**
 * Joystick in Down status
 *
 */
bool JoystickShield::isDown() {
    if (currentStatus == DOWN ) {
        return true;
    } else {
        return false;
    }
}

/**
 * Joystick in Left Down status
 *
 */
bool JoystickShield::isLeftDown() {
    if (currentStatus == LEFT_DOWN ) {
        return true;
    } else {
        return false;
    }
}

/**
 * Joystick in Left status
 *
 */
bool JoystickShield::isLeft() {
    if (currentStatus == LEFT ) {
        return true;
    } else {
        return false;
    }
}

/**
 * Joystick in Left Up status
 *
 */
bool JoystickShield::isLeftUp() {
    if (currentStatus == LEFT_UP) {
        return true;
    } else {
        return false;
    }
}

/**
 * Joystick in not Center status
 *
 */
bool JoystickShield::isNotCenter() {
    if (currentStatus != CENTER) {
        return true;
    } else {
        return false;
    }
}

/**
 * Joystick x position from -100 to 100, 0 = center
 *
 */
int JoystickShield::xAmplitude()  {
	return x_position;
}

/**
 * Joystick y position from -100 to 100, 0 = center
 *
 */
int JoystickShield::yAmplitude()  {
	return y_position;
}

/**
 * Joystick button pressed
 *
 */
bool JoystickShield::isJoystickButton() {
    if (currentButton == JOYSTICK_BUTTON) {
        clearButtonStates();
        return true;
    } else {
        return false;
    }
}

/**
 * Up button pressed
 *
 */
bool JoystickShield::isUpButton() {
    if (currentButton == UP_BUTTON) {
        clearButtonStates();
        return true;
    } else {
        return false;
    }
}

/**
 * Right button pressed
 *
 */
bool JoystickShield::isRightButton() {
    if (currentButton == RIGHT_BUTTON) {
        clearButtonStates();
        return true;
    } else {
        return false;
    }
}

/**
 * Down button pressed
 *
 */
bool JoystickShield::isDownButton() {
    if (currentButton == DOWN_BUTTON) {
        clearButtonStates();
        return true;
    } else {
        return false;
    }
}

/**
 * Left button pressed
 *
 */
bool JoystickShield::isLeftButton() {
    if (currentButton == LEFT_BUTTON) {
        clearButtonStates();
        return true;
    } else {
        return false;
    }
}

/**
 * F button pressed
 *
 */
bool JoystickShield::isFButton() {
    if (currentButton == F_BUTTON) {
        clearButtonStates();
        return true;
    } else {
        return false;
    }
}

/**
 * E button pressed
 *
 */
bool JoystickShield::isEButton() {
    if (currentButton == E_BUTTON) {
        clearButtonStates();
        return true;
    } else {
        return false;
    }
}
/**
 * Joystick Callbacks
 *
 */
/****************************************************************** */
void JoystickShield::onJSCenter(void (*centerCallback)(void)) {
    this->centerCallback = centerCallback;
}

void JoystickShield::onJSUp(void (*upCallback)(void)) {
    this->upCallback = upCallback;
}

void JoystickShield::onJSRightUp(void (*rightUpCallback)(void)) {
    this->rightUpCallback = rightUpCallback;
}

void JoystickShield::onJSRight(void (*rightCallback)(void)) {
    this->rightCallback = rightCallback;
}

void JoystickShield::onJSRightDown(void (*rightDownCallback)(void)) {
    this->rightDownCallback = rightDownCallback;
}

void JoystickShield::onJSDown(void (*downCallback)(void)) {
    this->downCallback = downCallback;
}

void JoystickShield::onJSLeftDown(void (*leftDownCallback)(void)) {
    this->leftDownCallback = leftDownCallback;
}

void JoystickShield::onJSLeft(void (*leftCallback)(void)) {
    this->leftCallback = leftCallback;
}

void JoystickShield::onJSLeftUp(void (*leftUpCallback)(void)) {
    this->leftUpCallback = leftUpCallback;
}

void JoystickShield::onJSnotCenter(void (*notCenterCallback)(void)) {
	this->notCenterCallback = notCenterCallback;
}

/****************************************************************** */

/**
 * Button Callbacks
 *
 */
 /****************************************************************** */
void JoystickShield::onJoystickButton(void (*jsButtonCallback)(void)) {
    this->jsButtonCallback = jsButtonCallback;
}

void JoystickShield::onUpButton(void (*upButtonCallback)(void)) {
    this->upButtonCallback = upButtonCallback;
}

void JoystickShield::onRightButton(void (*rightButtonCallback)(void)) {
    this->rightButtonCallback = rightButtonCallback;
}

void JoystickShield::onDownButton(void (*downButtonCallback)(void)) {
    this->downButtonCallback = downButtonCallback;
}

void JoystickShield::onLeftButton(void (*leftButtonCallback)(void)) {
    this->leftButtonCallback = leftButtonCallback;
}

void JoystickShield::onFButton(void (*FButtonCallback)(void)) {
	this->FButtonCallback = FButtonCallback;
}

void JoystickShield::onEButton(void (*EButtonCallback)(void)) {
	this->EButtonCallback = EButtonCallback;
}

/****************************************************************** */

/**
 * Clear the current button state
 *
 */
void JoystickShield::clearButtonStates() {
    currentButton = NO_BUTTON;
}

/**
 * Initialize all Function pointers to NULL
 *
 */
void JoystickShield::initializeCallbacks() {
    // Joystick callbacks
    centerCallback      = NULL;
    upCallback          = NULL;
    rightUpCallback     = NULL;
    rightCallback       = NULL;
    rightDownCallback   = NULL;
    downCallback        = NULL;
    leftDownCallback    = NULL;
    leftCallback        = NULL;
    leftUpCallback      = NULL;
	notCenterCallback   = NULL;

    // Button callbacks
    jsButtonCallback    = NULL;
    upButtonCallback    = NULL;
    rightButtonCallback = NULL;
    downButtonCallback  = NULL;
    leftButtonCallback  = NULL;
	FButtonCallback		= NULL;
	EButtonCallback		= NULL;
}

I have spend days working on this and still have no clue how to get the program to read the coordinates of the joystick

Really?

// Joystick coordinates
 int xAmplitude();
 int yAmplitude();

Just call these methods...

and regulate the speed depending on how far forward, backward, etc. the joystick is.

That will require some experimenting.

PaulS:
Really?

// Joystick coordinates

int xAmplitude();
int yAmplitude();



Just call these methods...
That will require some experimenting.

Sorry I'm still pretty new at this and have tried that but have gotten no results. Is there any way you could provide an example? Here is where I tried and failed miserably :o

As you can see I tried editing the code only for the "Joystick Up" function first.

int motor_left[] = {5, 6};
int motor_right[] = {9, 10};
int motor_fan[] = {12, 13};
int yAmplitude = 0;


#include <JoystickShield.h> // include JoystickShield Library

JoystickShield joystickShield; // create an instance of JoystickShield object

void setup() {
    Serial.begin(9600);
    
    pinMode(motor_left[0], OUTPUT);
    pinMode(motor_left[1], OUTPUT);
    pinMode(motor_right[0], OUTPUT);
    pinMode(motor_right[1], OUTPUT);
    pinMode(motor_fan[0], OUTPUT);
    pinMode(motor_fan[1], OUTPUT);
    
    delay(100);
    
    
    // new calibration function
    joystickShield.calibrateJoystick();

	// predefined Joystick to Pins 0 and 1.
	// Change it if you are using a different shield
	// setJoystickPins(0, 1);
  
	// predefined buttons to the following pins.
	// change it if you are using a different shield.
	// setButtonPins(pinJoystickButton, pinUp, pinRight, pinDown, pinLeft, pinF, pinE);
	// to deactivate a button use a pin outside of the range of the arduino e.g. 255, but not above
	// setButtonPins(8, 2, 3, 4, 5, 7, 6);
}
  // --------------------------------------------------------------------------- Drive

void fan_suck(){
digitalWrite(motor_fan[0], HIGH);
digitalWrite(motor_fan[1], LOW);
}

void fan_blow(){
digitalWrite(motor_fan[0], LOW);
digitalWrite(motor_fan[1], HIGH);
}

void fan_stop(){
digitalWrite(motor_fan[0], LOW);
digitalWrite(motor_fan[1], LOW);
delay(25);
}

void motor_stop(){
digitalWrite(motor_left[0], LOW); 
digitalWrite(motor_left[1], LOW); 

digitalWrite(motor_right[0], LOW); 
digitalWrite(motor_right[1], LOW);
delay(25);
}

void drive_forward(){

  
if (yAmplitude < 50){
  analogWrite(motor_left[0], 100);
  analogWrite(motor_left[1], 0);

  analogWrite(motor_right[0], 100); 
  analogWrite(motor_right[1], 0); 
}
else
  analogWrite(motor_left[0], 0);
  analogWrite(motor_left[1], 0);

  analogWrite(motor_right[0], 0); 
  analogWrite(motor_right[1], 0); 

}

void drive_backward(){
digitalWrite(motor_left[0], LOW); 
digitalWrite(motor_left[1], HIGH); 

digitalWrite(motor_right[0], LOW); 
digitalWrite(motor_right[1], HIGH); 
}

void turn_left(){
digitalWrite(motor_left[0], LOW); 
digitalWrite(motor_left[1], HIGH); 

digitalWrite(motor_right[0], HIGH); 
digitalWrite(motor_right[1], LOW);
}

void turn_right(){
digitalWrite(motor_left[0], HIGH); 
digitalWrite(motor_left[1], LOW); 

digitalWrite(motor_right[0], LOW); 
digitalWrite(motor_right[1], HIGH); 
}
  

void loop() {
  joystickShield.processEvents(); // process events
  
  if (joystickShield.isCenter()){
     Serial.println("Center") ; 
     motor_stop();
     fan_stop();
  }
  
  if (joystickShield.isUp()) {
     Serial.println("Up") ;
     drive_forward();
     fan_blow();
  }
  
  if (joystickShield.isRightUp()) {
     Serial.println("RightUp") ;
  }

  if (joystickShield.isRight()) {
     Serial.println("Right") ;
     turn_right();
  }

  if (joystickShield.isRightDown()) {
     Serial.println("RightDown") ;
  }

  if (joystickShield.isDown()) {
     Serial.println("Down") ;
     drive_backward();
     fan_suck();
     
  }

  if (joystickShield.isLeftDown()) {
     Serial.println("LeftDown") ;
  }

  if (joystickShield.isLeft()) {
     Serial.println("Left") ;
     turn_left();
  }

  if (joystickShield.isLeftUp()) {
     Serial.println("LeftUp") ;
  }

  if (joystickShield.isJoystickButton()) {
     Serial.println("Joystick Clicked") ;
  }

  if (joystickShield.isUpButton()) {
     Serial.println("Up Button Clicked") ;
  }

  if (joystickShield.isRightButton()) {
     Serial.println("Right Button Clicked") ;
  }

  if (joystickShield.isDownButton()) {
     Serial.println("Down Button Clicked") ;
  }

  if (joystickShield.isLeftButton()) {
     Serial.println("Left Button Clicked") ;
  }

  // new eventfunctions
  if (joystickShield.isEButton()) {
     Serial.println("E Button Clicked") ;
  }

  if (joystickShield.isFButton()) {
     Serial.println("F Button Clicked") ;
  }  
  
  if (joystickShield.isNotCenter()){
     Serial.println("NotCenter") ;
  }

  // new position functions
  Serial.print("x ");	Serial.print(joystickShield.xAmplitude());Serial.print(" y ");Serial.println(joystickShield.yAmplitude());

  delay(500);
}

Where is yAmplitude assigned a value?

At the end of loop(), you should have

  yAmplitude = joystickShield.yAmplitude();

PaulS:
Where is yAmplitude assigned a value?

At the end of loop(), you should have

  yAmplitude = joystickShield.yAmplitude();

yAmplitude values seemed to be assigned in the second library. They go from 0 to 100 and are printed in the serial monitor (sorry if I used the wrong name here). I need to figure out how to have the program read them so when it reaches y=50 my motors are running at about 130 or so etc.

yAmplitude values seemed to be assigned in the second library.

The yAmplitude() method, from the library, returns a value. You print that value, but you don't store it in a variable, so the value is not usable anywhere in your code.

[/

PaulS:
The yAmplitude() method, from the library, returns a value. You print that value, but you don't store it in a variable, so the value is not usable anywhere in your code.

So how would I define yAmplitude as a variable so I could use it to control speed? I'm sorry for all these basic questions but thank you for taking the time to help me!

So how would I define yAmplitude as a variable so I could use it to control speed?

You already have it defined as a variable. I showed, in reply #6, how to assign the variable a value.

I am adding the library code in as a reply because I exceeded the character limit

For help, I suggest you limit your code to just converting the joystick input to PWM output for the motors. Once that is worked out, incorporate that back into the massive main code.

PaulS:
You already have it defined as a variable. I showed, in reply #6, how to assign the variable a value.

zoomkat:
For help, I suggest you limit your code to just converting the joystick input to PWM output for the motors. Once that is worked out, incorporate that back into the massive main code.

Thank you both for your help I figured it out!