Archives
Trending
Support
Login
clear text
XML
Django
JavaScript
MATLAB
C
C++
C#
Python
SQL
Shell
Bash
Markdown
YAML
JSON
HTML
CSS
PHP
Java
Ruby
Go
Rust
Swift
Kotlin
Arduino
TypeScript
Perl
Autohotkey
Lua
SQF
R
Scala
Haskell
Groovy
Dart
Clojure
VB.NET
Objective-C
PowerShell
Bash
CoffeeScript
Verilog
/********* Pleasedontcode.com ********** Pleasedontcode thanks you for automatic code generation! Enjoy your code! - Terms and Conditions: You have a non-exclusive, revocable, worldwide, royalty-free license for personal and commercial use. Attribution is optional; modifications are allowed, but you're responsible for code maintenance. We're not liable for any loss or damage. For full terms, please visit pleasedontcode.com/termsandconditions. - Project: # Motor Control - Version: 002 - Source Code compiled for: Arduino Uno - Source Code created on: 2026-03-21 00:03:04 ********* Pleasedontcode.com **********/ /****** SYSTEM REQUIREMENTS *****/ /****** SYSTEM REQUIREMENT 1 *****/ /* Read X and Y axis values from the joystick using */ /* analog pins A0 and A1 */ /****** SYSTEM REQUIREMENT 2 *****/ /* Control motor speed and direction based on */ /* joystick Y axis: forward when pushed up, backward */ /* when pushed down, stopped when centered */ /****** SYSTEM REQUIREMENT 3 *****/ /* Use PWM pin 5 to control motor speed and pins 8,9 */ /* to control motor direction via L298N motor driver */ /****** END SYSTEM REQUIREMENTS *****/ /* START CODE */ /***** DEFINITION OF ANALOG INPUT PINS *****/ // SYSTEM REQUIREMENT 1: Joystick X axis on analog pin A0 const uint8_t JOYSTICK_X_PIN = A0; // SYSTEM REQUIREMENT 1: Joystick Y axis on analog pin A1 const uint8_t JOYSTICK_Y_PIN = A1; /***** DEFINITION OF MOTOR CONTROL PINS *****/ // SYSTEM REQUIREMENT 3: PWM pin 5 for motor speed control const uint8_t MOTOR_ENABLE_PIN = 5; // SYSTEM REQUIREMENT 3: Pins 8,9 for motor direction control via L298N const uint8_t MOTOR_IN1_PIN = 8; const uint8_t MOTOR_IN2_PIN = 9; /***** DEFINITION OF JOYSTICK VARIABLES *****/ // SYSTEM REQUIREMENT 1: Variable to store X axis value int joystickXValue = 0; // SYSTEM REQUIREMENT 1: Variable to store Y axis value int joystickYValue = 0; // Joystick center position threshold (for detecting centered position) const int JOYSTICK_CENTER = 512; const int JOYSTICK_THRESHOLD = 50; // Motor speed variable (0-255 for PWM) uint8_t motorSpeed = 0; /****** MOTOR CONTROL FUNCTIONS (L298N replacement) *****/ // Function to set motor forward direction void motorForward(uint8_t speed) { // SYSTEM REQUIREMENT 3: Set motor direction to forward (IN1=HIGH, IN2=LOW) digitalWrite(MOTOR_IN1_PIN, HIGH); digitalWrite(MOTOR_IN2_PIN, LOW); // Set motor speed using PWM on enable pin analogWrite(MOTOR_ENABLE_PIN, speed); } // Function to set motor backward direction void motorBackward(uint8_t speed) { // SYSTEM REQUIREMENT 3: Set motor direction to backward (IN1=LOW, IN2=HIGH) digitalWrite(MOTOR_IN1_PIN, LOW); digitalWrite(MOTOR_IN2_PIN, HIGH); // Set motor speed using PWM on enable pin analogWrite(MOTOR_ENABLE_PIN, speed); } // Function to stop the motor void motorStop(void) { // SYSTEM REQUIREMENT 3: Stop motor (both IN1 and IN2 LOW, speed 0) digitalWrite(MOTOR_IN1_PIN, LOW); digitalWrite(MOTOR_IN2_PIN, LOW); // Stop the motor by setting speed to 0 analogWrite(MOTOR_ENABLE_PIN, 0); } void setup(void) { // Initialize serial communication for debugging Serial.begin(9600); // Configure joystick pins as inputs pinMode(JOYSTICK_X_PIN, INPUT); pinMode(JOYSTICK_Y_PIN, INPUT); // SYSTEM REQUIREMENT 3: Configure motor control pins pinMode(MOTOR_ENABLE_PIN, OUTPUT); pinMode(MOTOR_IN1_PIN, OUTPUT); pinMode(MOTOR_IN2_PIN, OUTPUT); // Initialize motor to stopped state motorStop(); Serial.println("Joystick Motor Control System Initialized"); Serial.println("Push joystick UP for forward, DOWN for backward"); } void loop(void) { // SYSTEM REQUIREMENT 1: Read joystick values readJoystick(); // SYSTEM REQUIREMENT 2: Control motor based on joystick input controlMotor(); // Small delay for stability delay(50); } // SYSTEM REQUIREMENT 1: Function to read X and Y axis values from analog pins A0 and A1 void readJoystick(void) { // SYSTEM REQUIREMENT 1: Read X axis from analog pin A0 joystickXValue = analogRead(JOYSTICK_X_PIN); // SYSTEM REQUIREMENT 1: Read Y axis from analog pin A1 joystickYValue = analogRead(JOYSTICK_Y_PIN); // Debug output to serial monitor Serial.print("Joystick X: "); Serial.print(joystickXValue); Serial.print(" | Joystick Y: "); Serial.println(joystickYValue); } // SYSTEM REQUIREMENT 2: Function to control motor speed and direction based on joystick Y axis void controlMotor(void) { // SYSTEM REQUIREMENT 2: Check if joystick is pushed up (Y value higher than center + threshold) if (joystickYValue > (JOYSTICK_CENTER + JOYSTICK_THRESHOLD)) { // Forward direction: joystick pushed up // Map joystick value (513-1023) to motor speed (0-255) motorSpeed = map(joystickYValue, JOYSTICK_CENTER + JOYSTICK_THRESHOLD, 1023, 0, 255); // Constrain motor speed to valid PWM range motorSpeed = constrain(motorSpeed, 0, 255); // SYSTEM REQUIREMENT 3: Set motor speed using PWM pin 5 and direction using pins 8,9 motorForward(motorSpeed); Serial.print("FORWARD - Speed: "); Serial.println(motorSpeed); } // SYSTEM REQUIREMENT 2: Check if joystick is pushed down (Y value lower than center - threshold) else if (joystickYValue < (JOYSTICK_CENTER - JOYSTICK_THRESHOLD)) { // Backward direction: joystick pushed down // Map joystick value (0-511) to motor speed (0-255) motorSpeed = map(joystickYValue, 0, JOYSTICK_CENTER - JOYSTICK_THRESHOLD, 255, 0); // Constrain motor speed to valid PWM range motorSpeed = constrain(motorSpeed, 0, 255); // SYSTEM REQUIREMENT 3: Set motor speed using PWM pin 5 and direction using pins 8,9 motorBackward(motorSpeed); Serial.print("BACKWARD - Speed: "); Serial.println(motorSpeed); } // SYSTEM REQUIREMENT 2: Joystick is centered - stop motor else { // Motor stopped when joystick is centered motorStop(); Serial.println("STOPPED - Joystick Centered"); } } /* END CODE */
Mark as private (unlisted)
for 30 minutes
for 6 hours
for 1 day
for 1 week
for 1 month
for 1 year
everlasting (like CCCP)