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);
}