/* ============================================
code is placed under the MIT license
Copyright (c) 2023 J-M-L
For the Arduino Forum : https://forum.arduino.cc/u/j-m-l
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
Explanation of Changes:
New Output Pin: The line const byte xDirectionPin = 4; adds a new pin for indicating the joystick's negative movement direction.
Pin Initialization: In the setup() function, pinMode(xDirectionPin, OUTPUT); initializes the pin, and digitalWrite(xDirectionPin, HIGH); sets it to HIGH initially, indicating no negative movement.
Direction Logic: Inside the loop(), we check if the joystick is moved left (negative direction) and set xDirectionPin to LOW. If the joystick is not pushed left or is within the dead zone, we set the pin back to HIGH.
This allows you to monitor the joystick's movement effectively, providing an output signal when it moves in the negative direction.
===============================================
*/
const byte xAxisPin = A0;
const byte xCLKPin = 2;
const byte xDTPin = 3;
const byte xDirectionPin = 4; // New output pin for direction indication
const int xCenter = 512; // joystick is centered when reading 512
const int xDeadZone = 100; // joystick will be considered still with reading in -xDeadZone and +xDeadZone around the xCenter
// demo mode to see what's going on with the LEDs
const unsigned long quadraturePeriod = 200; // in ms
unsigned long xLastTick;
void setup() {
Serial.begin(115200);
Serial.println("rotary hack too\n");
pinMode(xCLKPin, OUTPUT);
pinMode(xDTPin, OUTPUT);
pinMode(xDirectionPin, OUTPUT); // Initialize the direction pin
digitalWrite(xCLKPin, HIGH);
digitalWrite(xDTPin, HIGH);
digitalWrite(xDirectionPin, HIGH); // Start with HIGH (not moving)
}
void loop() {
int potValue = analogRead(xAxisPin);
int distance = abs(512 - potValue);
int tickPeriod = map(distance, 0, 512, 1000, 50);
// Determine if joystick is moving in the negative direction
if (distance > xDeadZone) {
if (potValue < (xCenter - xDeadZone)) {
digitalWrite(xDirectionPin, LOW); // Joystick is pushed left (negative direction)
} else {
digitalWrite(xDirectionPin, HIGH); // Joystick is not pushed left
}
if (millis() - xLastTick >= tickPeriod) {
if (potValue > (xCenter + xDeadZone)) {
quadFSM(1); // Move in positive direction
} else if (potValue < (xCenter - xDeadZone)) {
quadFSM(-1); // Move in negative direction
}
xLastTick += tickPeriod;
}
} else {
// If within dead zone, ensure direction pin is HIGH
digitalWrite(xDirectionPin, HIGH);
}
}
byte quadFSM(byte direction) {
static byte state;
switch (state) {
case 0 :
digitalWrite(xDTPin, LOW);
state += direction;
break;
case 1 :
digitalWrite(xCLKPin, LOW);
state += direction;
break;
case 2 :
digitalWrite(xDTPin, HIGH);
state += direction;
break;
case 3 :
digitalWrite(xCLKPin, HIGH);
state += direction;
break;
}
state &= 0x3; // Keep state in the range 0-3
}
CLK
DT
X axis
© J-M-L for the arduino forum
demo code for generating
a quadrature signal
<------ dead zone ------>
<- Increment ->
<- Decrement ->