// Includes the Arduino Stepper Library
#include <Stepper.h>
// Pins for LEDs
int ledpin1 = 5; // green
int ledpin2 = 6; // red
// Joystick pins
int joystickVCC = 3; // Joystick VCC pin
int joystickGND = 4; // Joystick GND pin
int joystickX = A0; // Joystick X-axis pin (not used in this case)
int joystickY = A1; // Joystick Y-axis pin
int joystickSW = 2; // Joystick button
// Variables to store joystick values
int joystickYValue = 0;
int joystickThreshold = 100; // Threshold to detect joystick tilt
int buttonState = HIGH; // Initial state of the button
int lastButtonState = HIGH; // Last button state for debouncing
// Defines the number of steps per rotation
int stepsPerRevolution = 200; // Arduino HW: 2048; Wokwi: 200
// Creates an instance of stepper class
// Pins entered in sequence IN1-IN3-IN2-IN4 for proper step sequence
Stepper myStepper = Stepper(stepsPerRevolution, 10, 11, 12, 13); // Wokwi
// Speed variable
int speed = 10;
void setup() {
// Initialize LED pins
pinMode(ledpin1, OUTPUT);
pinMode(ledpin2, OUTPUT);
// Initialize joystick pins
pinMode(joystickVCC, OUTPUT); // Set joystick VCC pin as output
digitalWrite(joystickVCC, HIGH); // Provide 5V to joystick
pinMode(joystickGND, OUTPUT); // Set joystick GND pin as output
digitalWrite(joystickGND, LOW); // Connect joystick GND to ground
pinMode(joystickX, INPUT); // X-axis
pinMode(joystickY, INPUT); // Y-axis
pinMode(joystickSW, INPUT_PULLUP); // Joystick button
// Initialize the serial port
Serial.begin(9600);
}
void loop() {
// Read the joystick Y-axis value
joystickYValue = analogRead(joystickY);
// Read the button state
buttonState = digitalRead(joystickSW);
// Check if the button is pressed (debounce logic)
if (buttonState == LOW && lastButtonState == HIGH) {
speed += 10; // Increase speed by 10
Serial.print("Speed increased to: ");
Serial.println(speed);
delay(200); // Debounce delay
}
lastButtonState = buttonState; // Update the last button state
// Set the speed of the stepper motor
myStepper.setSpeed(speed);
// Determine direction based on joystick Y-axis value
if (joystickYValue > 512 + joystickThreshold) {
// Joystick pushed forward
digitalWrite(ledpin1, HIGH);
digitalWrite(ledpin2, LOW);
Serial.print(joystickYValue);
Serial.println(" clockwise");
myStepper.step(stepsPerRevolution); // Move clockwise
} else if (joystickYValue < 512 - joystickThreshold) {
// Joystick pulled backward
digitalWrite(ledpin1, LOW);
digitalWrite(ledpin2, HIGH);
Serial.print(joystickYValue);
Serial.println(" counterclockwise");
myStepper.step(-stepsPerRevolution); // Move counterclockwise
} else {
// Joystick in neutral position
digitalWrite(ledpin1, LOW);
digitalWrite(ledpin2, LOW);
// Serial.println(" Joystick in neutral, motor stopped");
}
delay(1000);
}
/*
Hardware Connection:
1. Stepper motor:
- Connect stepper motor control pins to Arduino pins 10, 11, 12, and 13.
- Connect motor's 5V pin to Arduino's 5V pin.
- Connect motor's GND pin to Arduino's GND.
2. LEDs and resistors:
- Connect LEDs to pins 5 and 6 with a 220-ohm resistor in series.
- Connect the other end of the LEDs to GND.
3. 5-Pin Joystick:
- Connect the joystick's VCC pin to Arduino pin 3 (set to HIGH in code).
- Connect the joystick's GND pin to Arduino pin 4 (set to LOW in code).
- Connect the joystick's Y-axis pin to Arduino A1.
- Connect the joystick's X-axis pin to Arduino A0 (optional, not actively used in this code).
- Connect the joystick's button pin (SW) to Arduino pin 2.
*/