0% found this document useful (0 votes)
2 views

code ardunio joystick

This document contains an Arduino sketch for controlling a motor using a joystick. It sets up pins for motor control and reads joystick input to determine motor direction (forward, backward, or stopped) based on defined dead zone thresholds. The motor is enabled or disabled accordingly, and joystick values are printed to the Serial Monitor.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

code ardunio joystick

This document contains an Arduino sketch for controlling a motor using a joystick. It sets up pins for motor control and reads joystick input to determine motor direction (forward, backward, or stopped) based on defined dead zone thresholds. The motor is enabled or disabled accordingly, and joystick values are printed to the Serial Monitor.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

const int enablePin = 10; // Enable pin for motor

const int in1 = 9; // Motor direction pin 1

const int in2 = 8; // Motor direction pin 2

const int joystickPin = A0; // Joystick input pin

const int deadZoneLow = 300;

const int deadZoneHigh = 350; // Dead zone threshold

void setup() {

Serial.begin(9600); // Initialize Serial Monitor

pinMode(enablePin, OUTPUT);

pinMode(in1, OUTPUT);

pinMode(in2, OUTPUT);

pinMode(joystickPin, INPUT);

digitalWrite(in1, LOW);

digitalWrite(in2, LOW);

digitalWrite(enablePin, LOW); // Ensure motor is off at start

void loop() {

int joystickValue = analogRead(joystickPin); // Read joystick value

Serial.print("Joystick Value: ");

Serial.println(joystickValue);

if (joystickValue > deadZoneHigh) { // Forward motion

digitalWrite(in1, HIGH);

digitalWrite(in2, LOW);

digitalWrite(enablePin, HIGH); // Enable motor

Serial.println("Motor Forward");

else if (joystickValue < deadZoneLow) { // Backward motion


digitalWrite(in1, LOW);

digitalWrite(in2, HIGH);

digitalWrite(enablePin, HIGH); // Enable motor

Serial.println("Motor Backward");

else { // Stop motor if joystick is in dead zone

digitalWrite(in1, HIGH);

digitalWrite(in2, HIGH); // Engage braking

digitalWrite(enablePin, LOW); // Disable motor

Serial.println("Motor Stopped");

delay(100); // Small delay to prevent excessive serial output

You might also like