ChatGPT generated code doesn't work

I am quite a bit of a newb when it comes to programming, microcontrollers and stuff, so bear with me. I am more of an analog guy.
My problems started when I asked ChatGPT for some code (because I am such a noob) for a simple two-button code lock. The code didn't work and the AI suggested to put in some serial monitor thingies to se where things got stuck, but that didn't work either. Nothing works. Absolutely nothing! Can't see why the code shouldn't work either. No errors when compiling. So something is stuck, and so am I. Yes, I have checked it with a several types of Ardu's and even online simulators also. Tried everything I can think of. Can someone give some hints on where and why it doesn't work?

const int countPin = 2;   // Pin for the count button
const int enterPin = 3;   // Pin for the enter button
const int outputPin = 13; // Pin for the output

const int maxAttempts = 3;  // Maximum number of attempts
const int codeLength = 4;   // Length of the passcode
const int passcode[codeLength] = {1, 2, 3, 4};  // The correct passcode

int attemptCount = 0;   // Number of attempts made so far
int buttonCount = 0;    // Number of times the count button has been pressed
int currentDigit = 0;   // The current digit being entered

void setup() {
  pinMode(countPin, INPUT_PULLUP);  // Set the count button as input
  pinMode(enterPin, INPUT_PULLUP);  // Set the enter button as input
  pinMode(outputPin, OUTPUT);  // Set the output as output
  
  Serial.begin(9600); // Initialize serial communication with baud rate 9600
}

void loop() {
  // Read the count button
  if (digitalRead(countPin) == LOW) {
    // Increment the button count
    buttonCount++;
  }

  // Read the enter button
  if (digitalRead(enterPin) == LOW) {
    // Check if the current digit is correct
    if (buttonCount == passcode[currentDigit]) {
      // Move on to the next digit if the current one is correct
      currentDigit++;
      buttonCount = 0;
      Serial.print(currentDigit); // Send the current digit over serial
    } else {
      // Reset the count and the current digit if the current one is incorrect
      buttonCount = 0;
      currentDigit = 0;
      attemptCount++;
    }
    // Check if the maximum number of attempts has been reached
    if (attemptCount >= maxAttempts) {
      attemptCount = 0;
      currentDigit = 0;
      Serial.print("Max attempts reached"); // Send a message over serial
    }
    // Check if the entire code has been entered correctly
    if (currentDigit == codeLength) {
      digitalWrite(outputPin, HIGH);
      currentDigit = 0;
      attemptCount = 0;
      Serial.print("Passcode entered correctly"); // Send a message over serial
    }
  }
}

Perhaps it would help if you told us at least as much as you must have told ChatGPT.

About what the sketch is supposed to do…

Srsly, no one wants to study a buggy program to figure out what it isn't doing and is supposed to.

a7

4 Likes

Maybe ask ChatGPT why the code does not work ?

10 Likes

I would think that even ChatGPT would know to avoid using pins 0 and 1, since they are used by the Serial interface on most arduinos.

const int countPin = 1;   // Pin for the count button
1 Like

Please, details. How could adding serial print thingies not have worked?

What does "nothing works" mean?

If you did out the code in a sim like

please share a link to it.

a7

2 Likes

You got the code posting right, but how about a quick schematic sketch, so we have some idea of the hardware configuration?
Or did ChatGPT tell you that wasn't necessary?

3 Likes

Sorry, that's my mistake and something I tried. Post corrected now.

1 Like

It is just two button switches connected to 2 and 3, and a LED on 13. Nothing more than that.

1 Like

Do you know any other chat-bots you could ask?

You'd be doing the AI community a favour by providing training data

3 Likes

It looks like the bot needs to take a look at the state change detection example.

Didn't this come up in another AI generated code topic?

2 Likes

That's exactly what I did and it incorporated some serial.print code for me, but that doesn't work either.

1 Like

It literally means: Nothing works.
Not more nothings. Not lesser nothings.

1 Like

No, "works"/ "doesn't work " means something specific, related to the intent.
We don't know what you asked the artificial dunce, so we need to know what "works" means.

Did you see my hint about the state change detection example?
It's included in the IDE

1 Like

Doesn't compile? Errors?

When you add print statements, they don't… print?

a7

1 Like

Please explain in detail i) what is your code should to do ii) what is the code from charGPT doing and iii) what the difference between first and second

1 Like

Look for this:

This code does more more or less what do you want. Just don't count the number of attempts.

It works with 6 buttons instead 2.

1 Like

I forgot to mention, this code doesn't do debounce so is better to use this version.

1 Like

Hi, @errmad

Please explain what your inputs and outputs are?
Please explain what you want your code to do?
What is the application?

What model Arduino are you using?

Can you please tell us your electronics, programming, arduino, hardware experience?

Thanks.. Tom.. :smiley: :+1: :coffee: :australia:

1 Like

The "current" state of ChatGPT is very much in an "genius-child" state; the bot has much of the Internet knowledge captured but has no "real-world" experience to judge code quality. Spoken many, many years ago: garbage in, garbage out... GIGO.

Had you rather asked an Internet search engine the same question, your results would have been spot-on:

Arduino example "2-button" " lock" at DuckDuckGo

1 Like

Compiles without errors. No print statements.

1 Like