Click here to Skip to main content
15,845,565 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I asked ChatGPT to write code to use attachInterrupt to a button on an Arduino UNO. Following is the code I copied exactly to a new sketch and attempted to compile.

C++
class Button {
private:
  int buttonPin;
  volatile bool buttonState;
  
public:
  Button(int pin) : buttonPin(pin), buttonState(false) {}

  void setup() {
    pinMode(buttonPin, INPUT_PULLUP);
    attachInterrupt(digitalPinToInterrupt(buttonPin), buttonChanged, CHANGE);
  }

  bool getState() {
    return buttonState;
  }

private:
  static void buttonChanged() {
    // You can only access static member variables and functions inside a static member function.
    // In this case, we can access the buttonState variable.
    buttonState = digitalRead(buttonPin);
  }
};

Button myButton(2);

void setup() {
  Serial.begin(9600);
  myButton.setup();
}

void loop() {
  bool state = myButton.getState();
  Serial.println(state);
  delay(100);
}


These are the errors I get when compiling:

D:\Arduino\Sketches\Test_Button\Test_Button.ino: In static member function 'static void Button::buttonChanged()':
D:\Arduino\Sketches\Test_Button\Test_Button.ino:22:5: error: invalid use of member 'Button::buttonState' in static member function
     buttonState = digitalRead(buttonPin);
     ^~~~~~~~~~~
D:\Arduino\Sketches\Test_Button\Test_Button.ino:4:17: note: declared here
   volatile bool buttonState;
                 ^~~~~~~~~~~
D:\Arduino\Sketches\Test_Button\Test_Button.ino:22:31: error: invalid use of member 'Button::buttonPin' in static member function
     buttonState = digitalRead(buttonPin);
                               ^~~~~~~~~
D:\Arduino\Sketches\Test_Button\Test_Button.ino:3:7: note: declared here
   int buttonPin;
       ^~~~~~~~~

exit status 1

Compilation error: invalid use of member 'Button::buttonState' in static member function


What I have tried:

I'm not familiar with C++, and coding CLASSES, so I tried to make sense of the compiler messages. I couldn't make any changes to the code that seemed to fix anything.
Posted
Updated 18-May-23 7:14am
v2
Comments
Gerry Schmitz 17-May-23 11:52am    
If ChatGPT is "so smart", it should be able to fix the errors in the code it generated. Or are you saying we're "smarter"? And if we're smarter, why would we bother with faulty ChatGPT code? ChatGPT can't "test" it's own answers so it can spew out any kind of nonsense code.
Robert Herzog 2023 17-May-23 11:59am    
Not much help Gerry. I already stated I'm new to C++ and used ChatGPT to show a sample code. I need help with the compile errors.
CHill60 17-May-23 11:57am    
If you are not familiar with C++ then I suggest first learning it.
Robert Herzog 2023 17-May-23 12:03pm    
Chill60, I'm trying to learn it, but it isn't easy learning something new on your own. I try to use ChatGPT to help in the learning process and in many cases it has. But this is something I have no idea how to resolve. Telling me I should learn C++ in order to learn it is like saying I should drown so I can learn to swim!
Dave Kreskowiak 17-May-23 12:05pm    
That's because ChatGPT can't write code. It treats code the same way it treats pictures. It mashes together stuff it's seen before into a new image. It can't tell if the new image works or will even compile.

ChatGPT, and other "AI"s today, do not have any understanding or creativity, and without those things, cannot possibly write original code.

Because ChatGPT is not a software developer: it's a "learning system" that reads data from the internet and regurgitates it combined with other chunks of other data.

As a result, what you get is GIGO: Garbage In, Garbage Out. If it finds code that looks like it might be close to what you asked for (but often not even that) it hands it to you as a fate accompli.

ChatGPT doesn't write code, doesn't understand code, doesn't know C++ or any other software language. It is pretty common for its "code samples" to not compile because it was assembled from several similar bit s of code that often didn't compile either, and weren't written to work together.
 
Share this answer
 
Your function
buttonChanged()
declared as static. You cannot access member variable buttonState inside static member functions. Static member function is global function that is located within class name namespace
 
Share this answer
 
v4
Not often I add a second "solution" but as a comment this was getting out of hand ...
Quote:
"Chill60, I'm trying to learn it, but it isn't easy learning something new on your own. I try to use ChatGPT to help in the learning process and in many cases it has."
But think about it: if ChatGPT is giving you code that doesn't compile, what it it teaching you? What can you learn from code that doesn't work, that can't work? If it compiled, would it even do what you wanted?

And that's ignoring the fact that we humans don't learn a skill by looking at what the skill produced: we learn by doing and building experience with the skill.
You can watch as much of the Tour de France as you want: it won't teach you how to ride a bicycle without falling off!

In this case, you think you are watching the Tour de France, but it's actually banger racing! :D
Quote:
"Telling me I should learn C++ in order to learn it is like saying I should drown so I can learn to swim!"
No, it's telling you that you need to start with the software equivelant of water wings to prevent you drowning. What you are trying to do is cut out all the "boring stuff" and move from zero to hero without the time consuming stuff that gets you from A to B. And that doesn't work with skills: riding a bike, swimming, coding, breadmaking, driving: you have get the basics first before you can start to really build the skill you need to be at the "hero" end of the scale.

ChatGPT doesn't do that: it has less skill in any of those subjects than you do! All it can do is regurgitate existing stuff in a (vaguely) new way. With text, sotories, pictures, ... that works. but with skill based stuff it doesn't have a chance!
 
Share this answer
 
Hi, this is nonsense code. Find out what "static" means regarding class variables and functions.

The static member function cannot access nonstatic members without class instance. I see here a poor and dysfunctional mix of concepts (IRQ handler using C++ object).

Forget ChatGPT, get a good book and try to use your brain.

If ChatGPT would be able to teach you C++, why should someone pay you money for coding if a machine could do the same? And why should you then learn C++? Would be really stupid or not?

In the case you fail on C++, you can change your goals and become consultant and later may be head of software development. Consultants are the people who tell bosses with no clue stupid things they and their staff have to do.

Payment is good, you just need to be able to tell nice stories using all the current buzzwords, no matter how crappy it is. ;-) ChatGPT can help on this... but who then needs... *hahaha*
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900