Combining Accelerometer Readings with Button Functionality

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.

You should make a fundamental decision:

either ask chatGPT so many times until the code works
or
learning programming yourself with the support of the arduino-forum

This is the reason why:

Analyzing and understanding AI code as a human is significantly more difficult than code written by humans.

This means you have to make the users five to ten times more work to understand the code!
And no PERSON does that!"

It is critical to write code in a way that people can understand, so we can find and fix bugs, and maintain and update code.

This will no longer be necessary when machines can do this reliably - which is probably the most difficult task!!! -
This requires very, very precise specifications that are clear, complete and unambiguous!

AI code structure requires much more “abstract thinking.” Maybe it's because the whole task is "abstract" for an AI without access to human reality...

So please be kind to those members who think their time is too valuable to waste on temporary improvements that will be gradually undone by an AI that has no real understanding of a specific task.

Either you switch to consistent programming. You as a human being learn programming.
And then you will get hundreds of questions answered here
or
you stick consistently with AI.
This AI is supposedly sooooooooo intelligent.
Keep writing prompts to the AI until the code works the way you want.

So make your decision:

100% chatGPT-code
or
100% human written code

best regards Stefan

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.