ChatGPT generated code doesn't work

Sound like fun. Since I enjoy debugging so much, I’ll ask ChatGPT for my own buggy program. I wonder if I could lead it into providing its own debugging tips

Adding serial print thingies is good advice. Add more until you can understand what the code is doing, both right and wrong.

Also, compare your code to other solutions to the same problem.

Not even the one you put in the setup function, immediately after the Serial.begin?

I think (in his first version) OP had pin 0 an1 connected to the buttons.
Serial.print during a button press may then damage the tx pin I guess...

Doesn't this AI know anything?

It depends on the product. Usually there are series resistors between those pins and the USB-serial device, to prevent that.

#all the awards from Star Trek

2 Likes

OK, I'll have no respect for myself in the morning, but I have managed to make ChatGPT's code function:

Passcode entered correctly

Yay!

I looked as little as possible at the logic, and tried as best I could not to fix or alter any of it. I'm pretty good at hacking and can usually do without causing any such harm.

As far as I can still see straight, the sketch functions perfectly if the two digitalRead()s are replaced with variables that become true when the corresponding button is pressed.

As suggested by @anon56112670 in #10 above, THX! That was a, in fact the, problem.

I had no trouble inserting Serial.print() statements, that as is said helped me see the values of key variables and confirm the logical flow through the program that they inform.

Score 1 for ChatGPT. :expressionless:


So @errmad, if you want to actually learn something and make this code your own, in a way, go study

 Files/Examples/02.Digital/StateChangeDetection

from the IDE and see if you can figure out how to get those if statements to trigger because a button gets pressed, rather than because a button is being held down.

a7

Someone mentioned something about "state change". I am now guessing that it is here that things go straight down in the AI generated code.
I wish soooo much that the Arduino programming language was less awkward and at least had a "wait" command like great Cow Basic has. C++ has its place but not for microcontrollers.

Wait {While | Until} condition

https://gcbasic.sourceforge.net/help/_wait.html
Can't be better than this, can it?

Thanks a lot alto777! I will have a look at it but I'm out of time for today. Maybe tomorrow..

LOL. What is this, 1976?

a7

If you want to use AI, make one simple part of your final code work in setup() or loop(), then put that in a function. Move on to the next part, and put it in its own function. Repeat.

C++ is the more efficient language for microcontrollers as the compiler(s) are tuned to create small, optimized executables.

If you really want

You should consider using a microcontroller such as ESP32 or RP2040 and utilize freeRTOS to synchronize threading.

In fact, Arduino on ESP32 (dual-core) has freeRTOS automatically bound into the binary and active across core_0 and core_1 by default.
ESP32 Dual Core using FreeRTOS and Arduino IDE - Multitasking (microcontrollerslab.com)

I need help with a code.

This code appears to be an implementation of a simple passcode entry system...

ChatGPT did a pretty good job of describing the code it couldn't even remember writing. I recommend that everyone plug some random code in and ask for an analysis. Very spooky.

The problem is that digitalRead reacts when the button is pressed, I need those if statements that the buttons control to trigger only when the button goes down, not because it is being held down.

In that case, you can modify the code to use a state machine...

That works better, but only if I use clean switches with no contact bounce. Can you add some debouncing for both buttons so I can use real switches?

Yes, I can help you add debouncing for both buttons...

(new version. Completely foobar).

You broke it. Now it says "Max attempts" immediately...

I apologize for the mistake. The issue might be that the debouncing delay is too long...

First I ever heard that explanatrion…

(another version, same problem)

No - bouncing isn't the problem because I am still using switches that do not bounce...

I apologize for the mistake. I believe I see the problem.

(Same problem, another version)
I asked it to try again and reminded it of a few things I tought it might be overlooking again.

I apologize for the confusion. I see what the problem is now...

(Same problem)

THX, I will try to fix this myself. You are in over your head it seems.

I'm sorry if my previous suggestions didn't work for you. I understand that debugging code can be a challenging task, especially if you're not familiar with the codebase. If you need further assistance, please feel free to ask, and I'll do my best to help you.

didn't work for you

LOL! Dint work full stop.

So for now my money is on @anon56112670 and the rest of the human race.

a7

Did you see

and

while(condition) delay(0); or while(!condition) delay(0); will do as you ask, but folks avoid them because it makes it impossible to do several things at the same time or to multitask the arduino

You greatly overestimate the clairvoyant abilities of both human users in the forum here and chatGPT.

Which code compiles without errors and has no print statements?
Such short postings are useless.
You are in a non-AI userforum you are not at snapchat.

If you want really help you have to provide precise information.
= actual code-version
Otherwise go snap-chattering at snapchat.

best regards Stefan

I'm not familiar with Cow Basic, but I suspect that it blocks any further program execution until it completes, just like 'while() {}' and 'delay()' in C++.

As for the second sentence, thanks for the first laugh of the day.

Don't count me in with that lot - I'm an also-ran.

while (condition);

I am probably opening up a can of worms here but....
It seems like almost everyone, for some reason, although I stated it clear as day in the first sentence, disregards the fact that I am a noob when it comes to microcontrollers and programming. Some also want me to to explain things that are obvious to them but not to me. How the # can I do that? I am an old fart, probably 20-30 years older than many of you. Never tried C++, ever! Never done programming, except a tiny bit of GCBasic about 12 years ago. I hope you don't expect me to just slide into this like you younger guys who almost got C++ in with your mothers milk. Do you now understand why I'm saying that C++ is "awkward" (or confusing if you will) and that many actually have problems understanding the illogical nature (for a noob) of this language? Have a look around the interwebs - it's not just me!
Yea, yea, yea... Have a good LOL at me, all day long, for mentioning GCBasic. But lookylooky here:

https://www.youtube.com/watch?v=qdloFhULa3I

Still rolling around on the floor, laughing?

Anyway.. Thanks to EVERYONE (despite the snickering and weird demands) for the help, especially alto777!
The code runs now and I will try to add some debouncing.

The can was opened by ChatGPT, or rather its creators. You might like to read this:

Never done programming, except a tiny bit of GCBasic about 12 years ago.

No problem, this is a good place to learn, Arduino was invented to help people like you.

I am 61, there are many here a lot older.

The problems you encountered here are mostly down to you getting code from somewhere else and expecting the people here to fix it. No one here minds you writing beginner code and asking for help to learn and fix the errors, the opposite in fact, the people here are happy to help you and are delighted when you get your project working and understand more about programming as a result of your effort. Unfortunately, that's not what you did, you asked people to fix code you didn't write.

Such as? I noticed instances where you were asked to explain what you expected the code to do, it's your project, only you know what the code is meant to do, no one else.

I am pleased you got an answer you are happy with. Please do come back with requests for more help on future projects but please post code you wrote, however bad you might think it is. People will gladly help you with that. Start by following the tutorials in the IDE and on this website. Do learn that delay() is not your friend (there are tutorials for that too).

Good luck with your projects.