ChatGPT as a programmer?
23rd February 2023
David Stonier-Gibson
Does ChatGPT have any use in computer programming? I took it for a spin.
I saw something online about a fellow who used ChatGPT to write a WordPress plugin for his wife. Well, I barely know what WordPress is, let alone a plugin for it. But I am currently deeply engaged with programming a gadget I designed using an ESP32 board in the Arduino Integrated Development Environment (IDE). So I decided to give ChatGPT a go with that.
One of my favourite little “thangs” with microcontrollers is making them do, seemingly, many things at once. It’s something I’ve been doing through my whole career of designing electronic machine controls. There are many things going on at once in say a beer bottling and labelling machine, and the controller has to be able to monitor and control them all at once. When I want to teach people my methods, I use the easily understood example of making several LEDs all flash at different, independent rates.
So I decided to challenge CHatGPT with that. It requires a level of sophistication of maybe 3 on a scale of 1 to 10.
The first attempt was a dismal failure – it used the dreaded delay() function, so only one LED could blink at once although they would all blink in turn. But I learned a valuable lesson: Frame your question most carefully, and be prepared to do several runs with better worded questions.
Here’s the first successful attempt, with my question/request in bold.
Write an Arduino program to flash 5 LEDs asynchronously all at independent rates. The LEDs are on output 5, 6, 7, 8 and 9
|
ChatGPT added a description to this one. Very useful for a student.
This program uses the millis() function to keep track of the time elapsed since the Arduino board was powered on. The flash rate for each LED is set in the beginning of the code in milliseconds.
In the loop() function, the current time is compared to the flash rate for each LED. If the current time minus the flash rate is greater than or equal to the flash rate, the LED is turned on or off, depending on its current state.
This allows the LEDs to flash independently of each other at their own rates
Next I decided to amp up the sophistication by asking for the LED to be managed by a class. This is a much more sophisticated method of programming called Object Oriented Programming (OOP). This is probably moving us up from 3 to 5 on our scale of 1 to 10.
Write an Arduino program to flash 5 LEDs asynchronously all at independent rates. The LEDs are on output 5, 6, 7, 8 and 9. Use a class and 5 instances
|
I then tried to add a LED toggle function from a push button. I was not able to coax it into making a proper solution that would discriminate the off to on transition of the push button. It failed this one miserably, and I could not coax a working solution out of it without giving away the game in my query. I gave up after three attempts. It always produced something where the LED would continuously toggle while the button was held down, not just once for each press. I wonder if the problem for it was that I was basically asking it to find solutions to two separate challenges, LEDs and button, at the same time.
Write an Arduino program to flash 5 LEDs asynchronously all at independent rates. The LEDs are on output 5, 6, 7, 8 and 9. Use a class and 5 instances. Add a separate asynchronous function that will toggle an LED on output 2 once whenever a push button connected to input 3 is pressed and then released.
|
And this one was very impressive (to me!) …
Write an Arduino program to flash 5 LEDs asynchronously all at independent rates. The LEDs are on output 5, 6, 7, 8 and 9. Code must be non-blocking. All intervals to be prime numbers.
|
… and each time I click the Regenerate button it comes up with a different solution.
Recent Comments