This is the GIF of my working DC motor and joystick circuit. So dashing.
This is a picture of my circuit. Because the joystick has a resistor built into it, I did not need to use an external resistor, the DC motor also did not require one.
int x = A1; // setting a variable to represent analog input pin A1
int xval = 0; // setting joystick xval counter
void setup() {
Serial.begin(9600);
pinMode(3, OUTPUT); // set pin 3 to output
}
void loop() {
xval = analogRead(x); // reads the x value of joystick
xval = map(xval, 0, 1024, 0, 255); // maps value of joystick to DC motor
analogWrite(3, xval); // writes the value of mapped joystick value to DC motor
Serial.println(xval);
}
Here is a snippet of my code. All of my important lines of code are commented. The values from the x input of the joystick ranged from 0 to 1024 according to the serial monitor, so that is what I chose to use as my input low and high values, respectively. As for the DC motor, I only mapped the motor to the x value of the joystick as both the x and y input values for the joystick give the same value, it just changes based on the direction of the joystick being interacted with.
Here is the schematic that I drew up for my joystick and DC motor circuit.