# ChatGPT generated code doesn't work

**URL:** <https://forum.arduino.cc/t/chatgpt-generated-code-doesnt-work/1109628>\
**Category:** Programming\
**Created:** [April 1, 2023, 11:14am UTC](https://forum.arduino.cc/t/chatgpt-generated-code-doesnt-work/1109628 "2023-04-01T11:14:46Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![errmad](https://avatars.discourse-cdn.com/v4/letter/e/ed655f/32.png) [@errmad](https://forum.arduino.cc/u/errmad)\
**Post date:** [April 1, 2023, 11:14am UTC](https://forum.arduino.cc/t/chatgpt-generated-code-doesnt-work/1109628/1 "2023-04-01T11:14:46Z")

</div>

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?

```cpp
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
    }
  }
}

```

---

<div class="post-metadata">

**Author:** ![alto777](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/alto777/32/317385_2.png) [@alto777](https://forum.arduino.cc/u/alto777)\
**Post date:** [April 1, 2023, 11:19am UTC](https://forum.arduino.cc/t/chatgpt-generated-code-doesnt-work/1109628/2 "2023-04-01T11:19:24Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![srnet](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/srnet/32/238868_2.png) [@srnet](https://forum.arduino.cc/u/srnet)\
**Post date:** [April 1, 2023, 11:19am UTC](https://forum.arduino.cc/t/chatgpt-generated-code-doesnt-work/1109628/3 "2023-04-01T11:19:28Z")

</div>

> [@errmad](#):
>
> ChatGPT

Maybe ask ChatGPT why the code does not work ?

---

<div class="post-metadata">

**Author:** ![david\_2018](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/david_2018/32/1013044_2.png) [@david\_2018](https://forum.arduino.cc/u/david_2018)\
**Post date:** [April 1, 2023, 11:21am UTC](https://forum.arduino.cc/t/chatgpt-generated-code-doesnt-work/1109628/4 "2023-04-01T11:21:14Z")

</div>

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.

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

```

---

<div class="post-metadata">

**Author:** ![alto777](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/alto777/32/317385_2.png) [@alto777](https://forum.arduino.cc/u/alto777)\
**Post date:** [April 1, 2023, 11:21am UTC](https://forum.arduino.cc/t/chatgpt-generated-code-doesnt-work/1109628/5 "2023-04-01T11:21:26Z")

</div>

> [@errmad](#):
>
> 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.

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

> **[Wokwi - Online ESP32, STM32, Arduino Simulator](https://wokwi.com/)**
>
> IoT and Embedded System Simulator: ESP32, STM32, Arduino, Raspberry Pi Pico, displays, sensors, motors and WiFi simulation.

please share a link to it.

a7

---

<div class="post-metadata">

**Author:** ![camsysca](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/camsysca/32/1015228_2.png) [@camsysca](https://forum.arduino.cc/u/camsysca)\
**Post date:** [April 1, 2023, 11:22am UTC](https://forum.arduino.cc/t/chatgpt-generated-code-doesnt-work/1109628/6 "2023-04-01T11:22:19Z")

</div>

> [@How to get the best out of this forum](https://forum.arduino.cc/t/how-to-get-the-best-out-of-this-forum/681310/1):
>
> Welcome to the Arduino forum! A guide explaining how to get the best out of this forum is available at the link below. Please read it:

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?

---

<div class="post-metadata">

**Author:** ![errmad](https://avatars.discourse-cdn.com/v4/letter/e/ed655f/32.png) [@errmad](https://forum.arduino.cc/u/errmad)\
**Post date:** [April 1, 2023, 11:24am UTC](https://forum.arduino.cc/t/chatgpt-generated-code-doesnt-work/1109628/7 "2023-04-01T11:24:23Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![errmad](https://avatars.discourse-cdn.com/v4/letter/e/ed655f/32.png) [@errmad](https://forum.arduino.cc/u/errmad)\
**Post date:** [April 1, 2023, 11:26am UTC](https://forum.arduino.cc/t/chatgpt-generated-code-doesnt-work/1109628/8 "2023-04-01T11:26:12Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![anon56112670](https://avatars.discourse-cdn.com/v4/letter/a/c5a1d2/32.png) [@anon56112670](https://forum.arduino.cc/u/anon56112670)\
**Post date:** [April 1, 2023, 11:26am UTC](https://forum.arduino.cc/t/chatgpt-generated-code-doesnt-work/1109628/9 "2023-04-01T11:26:25Z")

</div>

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

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

---

<div class="post-metadata">

**Author:** ![anon56112670](https://avatars.discourse-cdn.com/v4/letter/a/c5a1d2/32.png) [@anon56112670](https://forum.arduino.cc/u/anon56112670)\
**Post date:** [April 1, 2023, 11:28am UTC](https://forum.arduino.cc/t/chatgpt-generated-code-doesnt-work/1109628/10 "2023-04-01T11:28:52Z")

</div>

> [@errmad](#):
>
> ```arduino
> if (digitalRead(countPin) == LOW) {
> // Increment the button count
> buttonCount++;
> }
> 
> ```

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?

---

<div class="post-metadata">

**Author:** ![errmad](https://avatars.discourse-cdn.com/v4/letter/e/ed655f/32.png) [@errmad](https://forum.arduino.cc/u/errmad)\
**Post date:** [April 1, 2023, 11:29am UTC](https://forum.arduino.cc/t/chatgpt-generated-code-doesnt-work/1109628/11 "2023-04-01T11:29:45Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![errmad](https://avatars.discourse-cdn.com/v4/letter/e/ed655f/32.png) [@errmad](https://forum.arduino.cc/u/errmad)\
**Post date:** [April 1, 2023, 11:41am UTC](https://forum.arduino.cc/t/chatgpt-generated-code-doesnt-work/1109628/12 "2023-04-01T11:41:07Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![anon56112670](https://avatars.discourse-cdn.com/v4/letter/a/c5a1d2/32.png) [@anon56112670](https://forum.arduino.cc/u/anon56112670)\
**Post date:** [April 1, 2023, 11:43am UTC](https://forum.arduino.cc/t/chatgpt-generated-code-doesnt-work/1109628/13 "2023-04-01T11:43:10Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![alto777](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/alto777/32/317385_2.png) [@alto777](https://forum.arduino.cc/u/alto777)\
**Post date:** [April 1, 2023, 11:44am UTC](https://forum.arduino.cc/t/chatgpt-generated-code-doesnt-work/1109628/14 "2023-04-01T11:44:48Z")

</div>

> [@errmad](#):
>
> Nothing works.

Doesn't compile? Errors?

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

a7

---

<div class="post-metadata">

**Author:** ![b707](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/b707/32/854321_2.png) [@b707](https://forum.arduino.cc/u/b707)\
**Post date:** [April 1, 2023, 11:45am UTC](https://forum.arduino.cc/t/chatgpt-generated-code-doesnt-work/1109628/15 "2023-04-01T11:45:19Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![FernandoGarcia](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/fernandogarcia/32/214937_2.png) [@FernandoGarcia](https://forum.arduino.cc/u/FernandoGarcia)\
**Post date:** [April 1, 2023, 12:25pm UTC](https://forum.arduino.cc/t/chatgpt-generated-code-doesnt-work/1109628/16 "2023-04-01T12:25:31Z")

</div>

Look for this:

> **[six buttons password - Wokwi Arduino and ESP32 Simulator](https://wokwi.com/projects/356107382137458689)**
>
> Run IoT and embedded projects in your browser: ESP32, Arduino, Pi Pico, and more. No installation required!

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.

---

<div class="post-metadata">

**Author:** ![FernandoGarcia](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/fernandogarcia/32/214937_2.png) [@FernandoGarcia](https://forum.arduino.cc/u/FernandoGarcia)\
**Post date:** [April 1, 2023, 12:41pm UTC](https://forum.arduino.cc/t/chatgpt-generated-code-doesnt-work/1109628/17 "2023-04-01T12:41:34Z")

</div>

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

> [@Creating a password that needs to be entered through buttons to activate a servo motor](https://forum.arduino.cc/t/creating-a-password-that-needs-to-be-entered-through-buttons-to-activate-a-servo-motor/1080352/9):
>
> The code using [Bounce2](https://github.com/thomasfredericks/Bounce2) library. #include \<Bounce2.h\> // include the Bounce2 library const int buttonPin[] = { 2, 3, 4, 5, 6, 7 }; // array of button pins connected to digital pins 2, 3, 4, 5, 6 and 7 Bounce button[6]; // array of Bounce objects for each button int buttonCount[6] = { 0 }; // array to store the number of button presses const int numberOfButtons = 6; …

---

<div class="post-metadata">

**Author:** ![TomGeorge](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/tomgeorge/32/215261_2.png) [@TomGeorge](https://forum.arduino.cc/u/TomGeorge)\
**Post date:** [April 1, 2023, 12:46pm UTC](https://forum.arduino.cc/t/chatgpt-generated-code-doesnt-work/1109628/18 "2023-04-01T12:46:43Z")

</div>

Hi, @errmad

> [@errmad](#):
>
> 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?

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.. 😃 👍 ☕ 🇦🇺

---

<div class="post-metadata">

**Author:** ![mrburnette](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/mrburnette/32/702556_2.png) [@mrburnette](https://forum.arduino.cc/u/mrburnette)\
**Post date:** [April 1, 2023, 12:48pm UTC](https://forum.arduino.cc/t/chatgpt-generated-code-doesnt-work/1109628/19 "2023-04-01T12:48:06Z")

</div>

> [@errmad](#):
>
> My problems started when I asked ChatGPT for some code (because I am such a noob) for a simple two-button code lock.

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](https://duckduckgo.com/?q=Arduino+example+%222-button%22+%22+lock%22&ia=web)

 ![image](https://europe1.discourse-cdn.com/arduino/original/4X/0/7/a/07aedffed1875081787b3ea7de9a73667f478a00.jpeg)

---

<div class="post-metadata">

**Author:** ![errmad](https://avatars.discourse-cdn.com/v4/letter/e/ed655f/32.png) [@errmad](https://forum.arduino.cc/u/errmad)\
**Post date:** [April 1, 2023, 1:15pm UTC](https://forum.arduino.cc/t/chatgpt-generated-code-doesnt-work/1109628/20 "2023-04-01T13:15:53Z")

</div>

Compiles without errors. No print statements.

[Next page](https://forum.arduino.cc/t/chatgpt-generated-code-doesnt-work/1109628.md?page=2)
