Can chatbot write code for picaxe?

MartinM57

Moderator
No...but I've just tried with ChatGPT 3.5....

Me:
I want to program in picaxe basic

ChatGPT:
Sure, I can help you get started with programming in PICAXE BASIC. blah blah blah

Me: I need a program to run on a 08M2 that PWMs a LED brightness depending on the value of a potentiometer. Show me the hardware connections I need also

ChatGPT:
Certainly! To create a program that PWMs a LED brightness based on the value of a potentiometer using a PICAXE 08M2 microcontroller, you'll need the following hardware connections:
Hardware Connections:
  1. Connect the positive (VCC) and negative (GND) pins of the PICAXE 08M2 to the power supply (e.g., battery or regulated power supply).
  2. Connect the wiper (center pin) of the potentiometer to an analog input pin on the PICAXE. Let's say you connect it to pin C.0.
  3. Connect one end of the potentiometer to the positive power supply (VCC) and the other end to the ground (GND).
  4. Connect the anode (longer lead) of the LED to a digital output pin on the PICAXE. Let's say you connect it to pin C.1.
  5. Connect the cathode (shorter lead) of the LED to a current-limiting resistor (e.g., 220-330 ohms), and connect the other end of the resistor to the ground (GND).
Here's a simple PICAXE BASIC program to achieve the desired functionality:
Code:
symbol potPin = C.0   ; Potentiometer connected to pin C.0
symbol ledPin = C.1   ; LED connected to pin C.1
symbol pwmValue = b2  ; Variable to store PWM value

main:
    readadc potPin, pwmValue  ; Read analog value from potentiometer
    pwmout ledPin, pwmValue  ; PWM the LED brightness based on potentiometer value
    pause 20                 ; Optional delay for stability
    goto main                ; Repeat the loop
Explanation of the program:
  • readadc potPin, pwmValue: Reads the analog value from the potentiometer connected to pin C.0 and stores it in the variable pwmValue.
  • pwmout ledPin, pwmValue: Sets the brightness of the LED connected to pin C.1 based on the analog value read from the potentiometer.
  • pause 20: This optional pause provides a short delay for stability. You can adjust the value as needed.
  • goto main: Loops back to the main label to continuously read the potentiometer and update the LED brightness.
Adjust the values and pins according to your specific hardware setup. Make sure to refer to the PICAXE documentation for any specific details related to your PICAXE model.



Untested...but looks reasonable at first glance ;)
 
Last edited:

AllyCat

Senior Member
Hi,
Connect the wiper (center pin) of the potentiometer to an analog input pin on the PICAXE. Let's say you connect it to pin C.0.
pwmout C.1, pwmValue ; PWM the LED brightness based on potentiometer value
Except that #PICAXE 08m2 : READADC c.0 , b1 : PWMOUT c.1 , b2 will produce "Error: Pin C.0 does not have ADC capability" , then a syntax error for the PWMOUT structure and finally "Error: Invalid pwmout pin (use C.2)" since the only dedicated PWM output pin is C.2 on an 08M2. ;)

Actually you could use READDAC on C.0 but the PICaxe always starts with C.0 as an output (Low), so there's a chance it could destroy the chip if the pot wiper were in the Top (Vcc) position. :(

There was quite a long discussion thread early in the year HERE where the conclusion appears to be that it is "impressive" but rarely produces PICaxe code which actually works !

Cheers, Alan.
 

hippy

Technical Support
Staff member
Has anybody played with chatbot to write code?
Some people have. The verdict seems to range from 'perfect' to 'utterly useless' so it's not easy to answer how useful they are, and will likely depend on the chatbot or AI used, what one asks it to do.

The pertinent question is, can ChatGPT or other AI write valid code for a PICAXE which does the job a user wants them to do ?

It is possible but, from what I have seen, while they give the appearance of doing a fine job, they are usually lacking in some important respects. Take for example "pwmout ledPin, pwmValue", looks great on first reading but that's not a valid command, will give a Syntax Error, and will leave the user having to figure out what it should be.

One should also compare what's generated to what someone experienced with PICAXE coding would produce.

The problem with ChatGPT and similar is they aren't 'thinking', aren't applying knowledge, are just mashing together things they have seen others write using statistics and 'proprietary magic'. And it isn't adverse to simply making things up, 'hallucinating' as they call it. I might call it something else if not on a family friendly forum.

If one analyses the output in Post #2 it's no more impressive than anyone with some minimal programming experience and given the PICAXE manuals could produce. Most of it is verbiage, effectively cut and paste in its explanations, verbose expansions of 'C.1 analogue input for a pot', 'C.2 has a LED connected', 'read an ADC', 'output as PWM'. In some respects it's just padding.

It is perhaps good enough for writing a How To for a simple task, though for all it does output it doesn't actually compile and, as noted, doesn't necessarily work. I would also say it's not a great code example. It would be better IMO to have used READADC10 for a 10-bit pot value, use PWMOUT to initialise the PWM then update it with PWMDUTY. I would have personally used a DO-WHILE rather than GOTO.

What worries me is that if it can't get it right when less than a dozen lines of code are required; what chance of getting it right when there's far more complicated code needed ? And, when it gets simple stuff wrong; how can we trust it to get more complicated things right ?

I suspect those hoping AI will save them from the effort of coding will mostly find that it moves effort from writing code to figuring out what code does and why it doesn't work. And that may end up being more work than it would otherwise have been.
 

Gramps

Senior Member
What worries me is that if it can't get it right when less than a dozen lines of code are required; what chance of getting it right when there's far more complicated code needed ?
Thanks so much for this great overview on the subject! I suppose the developers would say, it will get more accurate as time goes on. But that makes it even more difficult to spot the errors whether it be in coding or other areas of science.
 

papaof2

Senior Member
From early 20th century US writings : TANSTAAFL
There Ain't No Such Thing As A Free Lunch
(The origin should be a 5 second lookup for the curious.)

So far, that seems to apply to AI in a number of fields...
 

hippy

Technical Support
Staff member
I suppose the developers would say, it will get more accurate as time goes on.
I am not so sure the developers of ChatGPT would as it's not specifically designed or intended for generating code.

It's perhaps best thought of as asking a friend who knows nothing about coding to get you a program to do what you want. They'll hit Google, find stuff which look like it will do the job, bash it into a report and hand it over. They won't actually know if it does do the job or is even correct. Because ChatGPT is software it can do this faster than a human and make better guesses as to what might be right but it doesn't actually know. If it finds something completely useless but thinks it's the best option it will deliver that. In practice it seems to me to be 'mostly right', a good first draft but not complete, not necessarily ideal.

Code generating AI should be better because they analyse what's being asked for, work out what's needed to implement that, string it all together, and throw it back. Those should get better over time but the drawback seems to be that they aren't necessarily tailored to knowing how to implement things in the language you may be using.

The interesting thing about PICAXE Basic is it's a very simple language to use, but there are semantic layers which, reasonably easy enough for humans to grasp, such as 'word variables are made of two byte variables, altering either will corrupt the other, so be aware of that', is quite a complicated thing for software to get to grips with. Especially as you can use a word variable in one place, use the byte parts elsewhere, when the corruption of the other doesn't matter. AI can supposedly figure all these sorts of things out for itself given appropriate training data but how well it can do that remains to be seen.

One interesting issue is, if an AI produces perfectly good code but it's so impenetrable that it can't be understood; how useful is it ?

I guess I am rather biased because I enjoy programming, enjoy the challenge of creating algorithms and crafting code which ultimately do what I want done, so wouldn't consider asking an AI to do my coding for me. But I do see the benefits in 'how do I do this?', having AI as a much better search engine.
 
Top