Alex Cockman
Published

Arduino Assignment 2

How to control a DC motor using a joystick.

BeginnerProtip1 hour425
Arduino Assignment 2

Things used in this project

Story

Read more

Schematics

Fritzing Schematic

Fritzing BreadBoard View

Code

Arduino Assignment 2 Code

C/C++
This is the code that I used for Arduino Assignment 2
// For this projct I got this code from https://toptechboy.com/arduino-tutorial-39-using-a-joystick-to-control-dc-motor-speed-and-direction/. Paul McWhorter is the man who wrote the article.
//For this project I used 3,4, and 5 on the digital pins side. I also used pin A1 on the Analog side along with the 5 volt and the gnd on the power side.
//Alex Cockman
int speedPin=5;
int dir1=4;
int dir2=3;
int mSpeed;
int jPin=A1;
int jVal;

//Set inputs for arduino uno

void setup() {
  // put your setup code here, to run once:
pinMode(speedPin,OUTPUT);
pinMode(dir1,OUTPUT);
pinMode(dir2,OUTPUT);
pinMode(jPin,INPUT);


//States pins as either inputs or outputs. Alex Cockman

}

void loop() {
jVal=analogRead(jPin);

Serial.println(jVal);
if (jVal<512){
digitalWrite(dir1,LOW);
digitalWrite(dir2,HIGH);        // Tells which direction joystick is rotating
mSpeed=-255./512.*jVal+255.;    // Fuction aquired from math equations calculated from point slope form. This equation is when the joystick is being pressed back
analogWrite(speedPin,mSpeed);
}
if(jVal>=512){
digitalWrite(dir1,HIGH); 
digitalWrite(dir2,LOW);          // Tells which direction joystick is pressed 
mSpeed=(255./512.)*jVal-255.;    // Equation to determine speed when the joystick is being pressed foward. Alex Cockman
analogWrite(speedPin,mSpeed);
}
}

Credits

Alex Cockman
4 projects • 5 followers

Comments