For a very short-term university design project we are tasked with developing a device that uses the online Miro-Boards for an alternative purpose other than brainstorming and gathering information.
So we decided to make a small tic-tac-toe "gaming" device which uses an accelerometer ADXL335, a joystick HW-504 and two click-buttons (shown abobe).
The idea is that when a player holds the device the Z-Analog Reading of the ADXL determines that the first button gives either a "X" or when the device is flipped upside down an "O" text output.
The second button is used for being a dedicated "N" keyboard (that creates a sticky note in Miro where we can use the second button to either create a "X" or "O").
When we do all of the steps by themselves they work, reading out the ADXL, using the joystick and just using the buttons. But when we ask ChatGPT (we have no coding experience) to combine the codes and interlace the Z-Reading with button functionality, neither of the inputs works at all really and is super buggy. This is the code:
"
#include <Mouse.h>
#include <Keyboard.h>
int horzPin = A1; // Analog output of horizontal joystick pin
int vertPin = A0; // Analog output of vertical joystick pin
int selPin = 2; // Select button pin of joystick
int vertZero, horzZero; // Stores the initial value of each axis, usually around 512
int vertValue, horzValue; // Stores current analog output of each axis
const int sensitivity = 200; // Higher sensitivity value = slower mouse, should be <= about 500
int mouseClickFlag = 0;
//int invertMouse = 1; //Invert joystick based on orientation
int invertMouse = -1; //Noninverted joystick based on orientation
#define zPin A3
const int zThreshold = 330; // Z threshold for determining the text
void setup()
{
pinMode(horzPin, INPUT); // Set both analog pins as inputs
pinMode(vertPin, INPUT);
pinMode(selPin, INPUT); // Set button select pin as input
digitalWrite(selPin, HIGH); // Pull button select pin high
delay(1000); // Short delay to let outputs settle
vertZero = analogRead(vertPin); // Get the initial values
horzZero = analogRead(horzPin); // Joystick should be in the neutral position when reading these
Serial.begin(115200); // Init serial communication
Mouse.begin(); // Init mouse emulation
Keyboard.begin(); // Init keyboard emulation
}
void loop()
{
vertValue = analogRead(vertPin) - vertZero; // Read vertical offset
horzValue = analogRead(horzPin) - horzZero; // Read horizontal offset
if (vertValue != 0)
Mouse.move(0, (invertMouse * (vertValue / sensitivity)), 0); // Move mouse on the y-axis
if (horzValue != 0)
Mouse.move((invertMouse * (horzValue / sensitivity)), 0, 0); // Move mouse on the x-axis
int zRaw = analogRead(zPin);
if (zRaw > zThreshold) {
// Text is O when Z position is above 330
Serial.println("O");
} else {
// Text is X when Z position is below or equal to 330
Serial.println("X");
}
if ((digitalRead(selPin) == 0) && (!mouseClickFlag)) // If the joystick button is pressed
{
mouseClickFlag = 1;
Mouse.press(MOUSE_LEFT); // Click the left button down
}
else if ((digitalRead(selPin)) && (mouseClickFlag)) // If the joystick button is not pressed
{
mouseClickFlag = 0;
Mouse.release(MOUSE_LEFT); // Release the left button
}
// Check if the first pin button is pressed
if (digitalRead(3) == LOW) {
Keyboard.press('N'); // Press 'N' on the keyboard
delay(100); // Delay for debouncing
Keyboard.release('N'); // Release 'N' on the keyboard
}
delay(200);
}"
So we wondered if the accelerometer and joystick are interfering since they both use analog inputs and why the buttons are so slow to respond. All analog and digital inputs are set correctly on the Arduino Leonardo board.
Maybe someone can help us with fixing the code or has a solution for making it all work together.