Author Topic: Getting started with microcontrollers  (Read 7568 times)

0 Members and 1 Guest are viewing this topic.

Offline dobsonr741

  • Frequent Contributor
  • **
  • Posts: 674
  • Country: us
Re: Getting started with microcontrollers
« Reply #50 on: March 16, 2024, 05:21:14 pm »
Quote
The big hurdle is that I need to learn C and/or C++.

No need to learn C, learn Python instead. Worderfully simple, CircuitPython will get you going fast, with modern MCUs.

This is my recommendation: https://www.seeedstudio.com/XIAO-RP2040-v1-0-p-5026.html
You can upgrade yourself later on to Arduino IDE, and after that straight RP2040 C SDK, if you want to.
 

Offline pcprogrammer

  • Super Contributor
  • ***
  • Posts: 3710
  • Country: nl
Re: Getting started with microcontrollers
« Reply #51 on: March 16, 2024, 07:24:56 pm »
Quote
The big hurdle is that I need to learn C and/or C++.

No need to learn C, learn Python instead. Worderfully simple, CircuitPython will get you going fast, with modern MCUs.

This is my recommendation: https://www.seeedstudio.com/XIAO-RP2040-v1-0-p-5026.html
You can upgrade yourself later on to Arduino IDE, and after that straight RP2040 C SDK, if you want to.

As if learning Python is any simpler than learning C.  :palm:

The Arduino IDE and all the examples that can be found, have you started quick on a simple target board like the UNO or nano.

As a beginner it is much easier to do things with Arduino without having to directly learn about a lot of other stuff, but also allows you to go quicker if you want to and dive deep into the internal interfaces in an MCU. And the latter is much simpler on an ATmega328 than on any of the more powerful MCU's like ESP32, RP2040, STM32 devices, PIC controllers and what more can be found.

Documentation also plays a big role into this. The datasheets for the ATmega series are rather good compared to for instance what STM32 devices bring to the table.

Offline dobsonr741

  • Frequent Contributor
  • **
  • Posts: 674
  • Country: us
Re: Getting started with microcontrollers
« Reply #52 on: March 16, 2024, 07:36:58 pm »
The question is what OP want: get successful real world applications of a microcontroller through a high level language, - or - to dive deep, to learn how to read datasheets, set register bits and do everything from scratch.

Both approach has merits, can be fun, let OP have choices.
 

Offline Doctorandus_P

  • Super Contributor
  • ***
  • Posts: 3365
  • Country: nl
Re: Getting started with microcontrollers
« Reply #53 on: March 17, 2024, 01:30:49 am »
"Arduino" has become very popular because it's easy to get started with, but very unfortunately it is quite a kludge and I don't like the platform much.

* The form factor of the "classic" arduino's is atrocious (I sort of like the "arduino nano" which is breadboard compatible).
* C & C++ programs start with a main( ) function (after startup code). and "arduino" have hidden that and added a ridiculous "setup() and "loop( )".
* The Java contraption they call an "IDE" is hardly worth that abbreviation. (Apparently it has gained some improvements in the last few years).
* How does hiding compiler messages make it "simpler"? Those messages are valuable.
* The whole framework is an ugly hodgepodge of mixed C and C++. If you study it, you learn from bad examples.

Also, for learning C or C++, using only a PC (with software) is a much better option than any uC board. When you use a decent IDE, it has an integrated debugger, and you can step though your code line for line effortlessly. This is a great extra tool during the learning process and helps with figuring out how the details work. Also, C and C++ are very much standardized languages (although the standards have evolved a lot over the last 50 years) I quite often design and debug algorithms on my PC, and only port them to an uC after the algorithm is debugged and working. If you know you want to port it later to a microcontroller, you can also do some extra tricks. For example, you can declare the special uC registers as simple variables. This does not get timers or uarts working, but if you start with the right names, you don't have to modify them when you port the code to an uC.

You can also use an RTOS such as FreeRTOS or ChibiOS. Often this is not really necessary for a small uC (think arduino), but for a bit more complex uC (ARM Cortex) it's a reasonable fit. Using an RTOS can give you some structure that helps with keeping the code better readable, and it also makes code re-use easier.
 

Offline Rick Law

  • Super Contributor
  • ***
  • Posts: 3442
  • Country: us
Re: Getting started with microcontrollers
« Reply #54 on: March 17, 2024, 01:54:02 am »
Thanks for all the replies. I think I will start by getting the Arduino Uno REV3 and go from there. I should have no problem interfacing hardware as I am very familiar with that kind of thing. The big hurdle is that I need to learn C and/or C++. I will have to find some tutorials and start practicing. We shall see if I can do it or if it proves to be more effort than I am willing to do.

Since you already have experience programming in assembler, learning C/C++ is easy.  I suggest starting with the Arduino IDE and load up one of their examples.  Something link Blink is a good one.  The sample C code is easy to understand for someone like you with assembler experience.  You will pick up C rather quickly.  Blinking an LED gives you immediate feedback.

Using C++ is a choice you can make later.  C (without the object oriented structure C++) works well enough to do every you can do with Arduino.  Some add-on stuff (for example, an LCD display) has their library written in C++.  If you are not already familiar with object oriented programming, that could be somewhat confusing.  You just define your copy of that LCD display object.  After that, just look at those object methods as function calls to functions specifically for that object.

I am not sure if you will have this confusion I had: One thing I found confusing when I start with Arduino (vs just a plain microprocessor) is the boot-loader.  I too moved from programming microprocessor in assembler decades ago, to playing with an Arduino (UNO first, mostly NANO now).  You grab a microprocessor (6502/8080, what not), there is no boot-loader.  You burn your EEPROM and the 8080/6502 will just start running what is in the boot rom/eeprom.  Took me some thinking to connect conceptually that the boot-loader is an area in flash reserved to emulate boot rom/eeprom.  This "boot rom" just starts the program already loaded in flashed, or communicated with the IDE to load a program into (the remaining areas of) flash.

If you get a NANO/UNO Arduino, they should have the arduino boot-loader, but I did have one experience of buying a 3-pack of NANOs that actually doesn't have the boot loader.  You would know the moment you try to use the Arduino IDE to flash one of the example programs to your arduino.  Make sure your first is an UNO with boot-loader.  If you have an Arduino UNO with boot loader that the IDE can talk to, it can download the bootloader flasher into the working UNO and make that UNO (or NANO) into a boot-loader flasher to flash the boot-loader onto another UNO/NANO.  UNO is easier since you can find a lot of info on how to hard-wire connect the working UNO to the UNO w/o bootloader.  NANO certainly can also do it, but lacking on-line helpful info, you have to be already familiar with how to hard-wire the pins of the flasher arduino and the other arduino to be flashed.  Having run into that problem, I now have a NANO dedicated to flashing boot-loader.

Good luck, starting may look intimidating, but for someone who can program 6502 with assembler, it should not be hard...
 

Online xrunner

  • Super Contributor
  • ***
  • Posts: 7518
  • Country: us
  • hp>Agilent>Keysight>???
Re: Getting started with microcontrollers
« Reply #55 on: March 17, 2024, 02:17:31 am »
Google "children using Arduino"

It really ain't that hard to learn.  :-+
I told my friends I could teach them to be funny, but they all just laughed at me.
 
The following users thanked this post: tooki

Offline Doctorandus_P

  • Super Contributor
  • ***
  • Posts: 3365
  • Country: nl
Re: Getting started with microcontrollers
« Reply #56 on: March 17, 2024, 02:26:59 am »
hp>Agilent>Keysight>Siglent  >:D
 

Online xrunner

  • Super Contributor
  • ***
  • Posts: 7518
  • Country: us
  • hp>Agilent>Keysight>???
Re: Getting started with microcontrollers
« Reply #57 on: March 17, 2024, 02:53:45 am »
hp>Agilent>Keysight>Siglent  >:D

I'm sure Tautech would agree but we digress ...  ;)
I told my friends I could teach them to be funny, but they all just laughed at me.
 

Online radiolistener

  • Super Contributor
  • ***
  • Posts: 3378
  • Country: ua
Re: Getting started with microcontrollers
« Reply #58 on: March 17, 2024, 04:23:31 am »
 

Online radiolistener

  • Super Contributor
  • ***
  • Posts: 3378
  • Country: ua
Re: Getting started with microcontrollers
« Reply #59 on: March 17, 2024, 04:26:08 am »
here is how to start programming in Rust for STM32 microcontrollers:
https://medium.com/digitalfrontiers/rust-on-a-stm32-microcontroller-90fac16f6342

 :)
 

Online tooki

  • Super Contributor
  • ***
  • Posts: 11546
  • Country: ch
Re: Getting started with microcontrollers
« Reply #60 on: March 17, 2024, 09:58:17 am »
"Arduino" has become very popular because it's easy to get started with, but very unfortunately it is quite a kludge and I don't like the platform much.

* The form factor of the "classic" arduino's is atrocious (I sort of like the "arduino nano" which is breadboard compatible).
So what? It works just fine for getting started, and there's a huge ecosystem of expansion boards ("shields") designed around that form factor.

* C & C++ programs start with a main( ) function (after startup code). and "arduino" have hidden that and added a ridiculous "setup() and "loop( )".
Pointless elitism that has zero merit from a technical standpoint.

The Arduino structure
Code: [Select]
setup()
  {
    //your code that runs once goes here
  }
loop()
  {
    //your code that runs repeatedly goes here
  }
is literally just shorthand for
Code: [Select]
main()
  {
    //your code that runs once goes here
 
   while(true)
     {
       //your code that runs repeatedly goes here
     }
  }

They compile to the exact same thing, and frankly, the setup()/loop() syntax is easier to understand, especially for a beginner. Remember, Arduino was literally designed as an educational tool for non-techies, and it does well at that goal.

* The Java contraption they call an "IDE" is hardly worth that abbreviation. (Apparently it has gained some improvements in the last few years).
The Arduino 1.x IDE is indeed very, very basic, but it works, and it works reliably. Experienced MCU programmers forget that configuring a "real" IDE and MCU project can be daunting, requiring MCU knowledge that a beginner doesn't possess. The Arduino IDE just works, allowing a beginner to focus on learning the concepts of MCU programming without getting bogged down with, discouraged by, or outright blocked by IDE/project configuration. Not to mention that before Arduino, most MCU toolchains were prohibitively expensive.

Arduino IDE 2.x is a big improvement, in that it now has autocomplete and hardware debugging support. But as I have explained already, one can also program Arduino using other IDEs, like the very popular PlatformIO.

* How does hiding compiler messages make it "simpler"? Those messages are valuable.
Where'd you get the idea that it hides compiler messages? It doesn't by default, and you can set the verbosity to even higher if you want.

A real issue is that compiler messages are often completely useless to a beginner -- and often are useless even to an experienced programmer. They often refer to things that are completely meaningless unless you are intimately familiar with the entire toolchain. Or how the error messages are often entirely wrong, in that the errors are symptoms of a syntax error somewhere earlier in the code, so the line numbers in the errors aren't actually where the error is!

* The whole framework is an ugly hodgepodge of mixed C and C++.
Given that C++ is designed to be mostly a superset of C, how is this a problem? Who cares?

If you study it, you learn from bad examples.
Of your entire rant, this is one of only 2 parts that has a kernel of truth: there are a LOT of examples of really bad code out there. It's a simple consequence of there being a lot of Arduino users, some of whom are only casual programmers. But IMHO that's a small price to pay in exchange for the value of the massive ecosystem that exists around it, and the community support available.

Also, for learning C or C++, using only a PC (with software) is a much better option than any uC board.
OP didn't say their goal was to learn C/C++. Their goal is to learn MCU programming, and they mentioned C/C++ as a hurdle, not a goal.

When you use a decent IDE, it has an integrated debugger, and you can step though your code line for line effortlessly. This is a great extra tool during the learning process and helps with figuring out how the details work. Also, C and C++ are very much standardized languages (although the standards have evolved a lot over the last 50 years) I quite often design and debug algorithms on my PC, and only port them to an uC after the algorithm is debugged and working. If you know you want to port it later to a microcontroller, you can also do some extra tricks. For example, you can declare the special uC registers as simple variables. This does not get timers or uarts working, but if you start with the right names, you don't have to modify them when you port the code to an uC.
This is the other part that has a kernel of truth: writing and debugging algorithms running on your computer is indeed much easier than on an MCU. I do the same thing. (Often, I just use www.onlinegdb.com rather than firing up Visual Studio or Xcode. Even if it didn't have debugging, which it does, the much shorter compile-run cycle saves tons of time compared to the compile-run-upload cycle of MCU programming.)

You can also use an RTOS such as FreeRTOS or ChibiOS. Often this is not really necessary for a small uC (think arduino), but for a bit more complex uC (ARM Cortex) it's a reasonable fit. Using an RTOS can give you some structure that helps with keeping the code better readable, and it also makes code re-use easier.
True, but as I've replied to the others who have suggested RTOS programming here: this isn't the time for that. Let people master the basics first!

(I also find it amusing that people bash Arduino for hiding the hardware beneath abstractions, supposedly preventing people from learning how things actually work, but then go on to suggest starting out with an RTOS, which... hides the hardware beneath abstractions!)






 
The following users thanked this post: brucehoult

Offline Picuino

  • Frequent Contributor
  • **
  • Posts: 730
  • Country: 00
    • Picuino web
Re: Getting started with microcontrollers
« Reply #61 on: March 17, 2024, 10:23:08 am »
Good luck, starting may look intimidating, but for someone who can program 6502 with assembler, it should not be hard...
That's what it might seem at first, but moving from the absolute flexibility of ASM to C is not so easy. You have to learn to submit to structured programming, forget GOTO/JMP and it's not immediate.
 

Offline brucehoult

  • Super Contributor
  • ***
  • Posts: 4039
  • Country: nz
Re: Getting started with microcontrollers
« Reply #62 on: March 17, 2024, 10:44:53 am »
"Arduino" has become very popular because it's easy to get started with, but very unfortunately it is quite a kludge and I don't like the platform much.

* The form factor of the "classic" arduino's is atrocious (I sort of like the "arduino nano" which is breadboard compatible).
So what? It works just fine for getting started, and there's a huge ecosystem of expansion boards ("shields") designed around that form factor.

I see nothing with which to disagree in this post. Saved me the effort.
 
The following users thanked this post: tooki

Offline brucehoult

  • Super Contributor
  • ***
  • Posts: 4039
  • Country: nz
Re: Getting started with microcontrollers
« Reply #63 on: March 17, 2024, 10:47:47 am »
Good luck, starting may look intimidating, but for someone who can program 6502 with assembler, it should not be hard...
That's what it might seem at first, but moving from the absolute flexibility of ASM to C is not so easy. You have to learn to submit to structured programming, forget GOTO/JMP and it's not immediate.

I'm not sure what language you're thinking of (maybe Java or Python?), but C is perfectly happy to let you write your whole program in one function, using labels, gotos, and shared variables.
 
The following users thanked this post: Rick Law

Offline pcprogrammer

  • Super Contributor
  • ***
  • Posts: 3710
  • Country: nl
Re: Getting started with microcontrollers
« Reply #64 on: March 17, 2024, 10:56:25 am »
Good luck, starting may look intimidating, but for someone who can program 6502 with assembler, it should not be hard...
That's what it might seem at first, but moving from the absolute flexibility of ASM to C is not so easy. You have to learn to submit to structured programming, forget GOTO/JMP and it's not immediate.

C is in a lot of ways easier then ASM and has similar levels of flexibility. Working with data is much more of a hassle in ASM where you have to do much more to access something in an array of data than it is in C. It might be faster in ASM, but with modern compilers the optimization is very good. With for instance ARM ASM you have to know a lot about addressing modes, conditional execution and what more, that is all hidden in C.

Many will say that goto in C is a no no, but it works just the same as in ASM and if you want you can program however you like in C. Using structured programming makes it easier to read and able to be used by others, but it is not a must.

And what do you mean with it is not immediate. Loading a peripheral register with a value is just as immediate as in ASM, unless your compiler does special things. And with modern processor architectures with pipe lining and other optimization schemes even your ASM might not execute in the order you expect it.

As someone whom has done a lot of ASM programming for various targets like Z80, 6502, 8051 to name just a couple and loved it very much, C is actually a relief in how easy it is to program stuff and it is much easier to reuse code for another project or on another processor platform.

Offline Picuino

  • Frequent Contributor
  • **
  • Posts: 730
  • Country: 00
    • Picuino web
Re: Getting started with microcontrollers
« Reply #65 on: March 17, 2024, 11:10:33 am »
Good luck, starting may look intimidating, but for someone who can program 6502 with assembler, it should not be hard...
That's what it might seem at first, but moving from the absolute flexibility of ASM to C is not so easy. You have to learn to submit to structured programming, forget GOTO/JMP and it's not immediate.

I'm not sure what language you're thinking of (maybe Java or Python?), but C is perfectly happy to let you write your whole program in one function, using labels, gotos, and shared variables.

Yes, you can make a spaghetti program in C, but that's not the way.
 

Offline brucehoult

  • Super Contributor
  • ***
  • Posts: 4039
  • Country: nz
Re: Getting started with microcontrollers
« Reply #66 on: March 17, 2024, 11:39:17 am »
Good luck, starting may look intimidating, but for someone who can program 6502 with assembler, it should not be hard...
That's what it might seem at first, but moving from the absolute flexibility of ASM to C is not so easy. You have to learn to submit to structured programming, forget GOTO/JMP and it's not immediate.

I'm not sure what language you're thinking of (maybe Java or Python?), but C is perfectly happy to let you write your whole program in one function, using labels, gotos, and shared variables.

Yes, you can make a spaghetti program in C, but that's not the way.

It's not the way you should program in ASM either.
 

Offline Picuino

  • Frequent Contributor
  • **
  • Posts: 730
  • Country: 00
    • Picuino web
Re: Getting started with microcontrollers
« Reply #67 on: March 17, 2024, 12:14:46 pm »
From experience with other programmers, the move from assembler to C is easy when it comes to understanding pointers, for example, but costly when it comes to implementing structured programming. I'm just saying that the change is not so immediate.
And of course programming everything in a single function with global variables and gotos does not seem to me a logical choice.
« Last Edit: March 17, 2024, 12:16:52 pm by Picuino »
 

Offline ozcar

  • Frequent Contributor
  • **
  • Posts: 322
  • Country: au
Re: Getting started with microcontrollers
« Reply #68 on: March 17, 2024, 04:39:46 pm »
...

* C & C++ programs start with a main( ) function (after startup code). and "arduino" have hidden that and added a ridiculous "setup() and "loop( )".
Pointless elitism that has zero merit from a technical standpoint.

The Arduino structure
Code: [Select]
setup()
  {
    //your code that runs once goes here
  }
loop()
  {
    //your code that runs repeatedly goes here
  }
is literally just shorthand for
Code: [Select]
main()
  {
    //your code that runs once goes here
 
   while(true)
     {
       //your code that runs repeatedly goes here
     }
  }

They compile to the exact same thing, and frankly, the setup()/loop() syntax is easier to understand, especially for a beginner. Remember, Arduino was literally designed as an educational tool for non-techies, and it does well at that goal.


Saying that those are equivalent could lead to wrong assumptions.

The traditional AVR main() contains this:

Code: [Select]
setup();

for (;;)
{
loop();
if (serialEventRun) serialEventRun();
}

Cores for other processors do other messing around on return from loop().
 
The following users thanked this post: tooki

Offline abeyer

  • Frequent Contributor
  • **
  • Posts: 292
  • Country: us
Re: Getting started with microcontrollers
« Reply #69 on: March 17, 2024, 10:36:17 pm »
if you want to add some large comment to document some details it's okay, but don't mix it with code. Just put that comment in one place, for example at the top of file and keep the code clean and free of spam.

For example:
...

There may be cases for using these comments, but please don't just use them to duplicate details stored in your vcs, and which will invariably get out of sync and not be correct. If you absolutely must have things like version, last changed, etc... in the text of your code, use your vcs and automation to populate them (and thus ensure they stay correct) rather than manually entering them.
 

Offline abeyer

  • Frequent Contributor
  • **
  • Posts: 292
  • Country: us
Re: Getting started with microcontrollers
« Reply #70 on: March 17, 2024, 10:51:04 pm »
As if learning Python is any simpler than learning C.  :palm:

The OP here might be an exception here as he isn't a beginner and comes in with a background in asm already... but python is 100% easier for beginners to get started/get productive with, and I say that having both formal and informal experience teaching multiple languages to university and adult learners. Things like manual memory management, pointers, and the associated operators are all fiddly bookkeeping details you have to keep straight in your head, and trying to do that while also learning the basic mechanics and logic of programming in general is an extra burden. It's not insurmountable, if someone wants to start with C/C++ it's certainly possible, plenty of people have learned that way, and you will end up with a deeper understanding of some things. But it's disingenuous and not helpful to underestimate the additional challenges. Some people may want that, and more power to them, but they should make that choice with their eyes open.
 
The following users thanked this post: tooki

Online radiolistener

  • Super Contributor
  • ***
  • Posts: 3378
  • Country: ua
Re: Getting started with microcontrollers
« Reply #71 on: March 18, 2024, 01:14:48 am »
Here is nice startup code for novice. This is a code example for LED blinking.
It is written in Rust language for STM32 bluepill arduino board.
Code: [Select]
// std and main are not available for bare metal software
#![no_std]
#![no_main]

extern crate stm32f1;
extern crate panic_halt;
extern crate cortex_m_rt;

use cortex_m_rt::entry;
use stm32f1::stm32f103;

// use `main` as the entry point of this application
#[entry]
fn main() -> ! {
    // get handles to the hardware
    let peripherals = stm32f103::Peripherals::take().unwrap();
    let gpioc = &peripherals.GPIOC;
    let rcc = &peripherals.RCC;

    // enable the GPIO clock for IO port C
    rcc.apb2enr.write(|w| w.iopcen().set_bit());
    gpioc.crh.write(|w| unsafe{
        w.mode13().bits(0b11);
        w.cnf13().bits(0b00)
    });

    loop{
        gpioc.bsrr.write(|w| w.bs13().set_bit());
        cortex_m::asm::delay(2000000);
        gpioc.brr.write(|w| w.br13().set_bit());
        cortex_m::asm::delay(2000000);
    }
}

You can find more details on how to compile it and upload into microcontroller here:
https://jonathanklimt.de/electronics/programming/embedded-rust/rust-STM32F103-blink/

As you can see, it is very easy, you can play with it and extend its functionality. That way you can learn programming for microcontrollers.
« Last Edit: March 18, 2024, 01:19:37 am by radiolistener »
 

Offline dobsonr741

  • Frequent Contributor
  • **
  • Posts: 674
  • Country: us
Re: Getting started with microcontrollers
« Reply #72 on: March 18, 2024, 01:34:50 am »
Still too much boilerplate needed in rust (unwrap, unsafe to name a few), and secret handshakes with the hardware the novice would not know what are they and where is it coming from (apb2enr, set bit, etc.)

IMO circuitpython is the simplest, and saves a lot of semicolons!

Code: [Select]

import time
import board
import digitalio

led = digitalio.DigitalInOut(board.LED)
led.direction = digitalio.Direction.OUTPUT

while True:
    led.value = True
    time.sleep(0.5)
    led.value = False
    time.sleep(0.5)
« Last Edit: March 18, 2024, 01:40:44 am by dobsonr741 »
 
The following users thanked this post: tooki

Offline brucehoult

  • Super Contributor
  • ***
  • Posts: 4039
  • Country: nz
Re: Getting started with microcontrollers
« Reply #73 on: March 18, 2024, 05:47:20 am »
Still too much boilerplate needed in rust (unwrap, unsafe to name a few), and secret handshakes with the hardware the novice would not know what are they and where is it coming from (apb2enr, set bit, etc.)

Agreed on this part.

The fewer lines (and tokens!) in the minimal "blink an LED" program the better!

Quote
IMO circuitpython is the simplest, and saves a lot of semicolons!

Code: [Select]

import time
import board
import digitalio

led = digitalio.DigitalInOut(board.LED)
led.direction = digitalio.Direction.OUTPUT

while True:
    led.value = True
    time.sleep(0.5)
    led.value = False
    time.sleep(0.5)

You lost me here.

Looking at my keyboard, the writing is completely worn off the A, S, C, and L keys and 90% gone on D, E, O and the Windows/Mac CMD keys. The semicolon key is not visibly worn at all, and I'm working in C, C++, Java, C# all day long.

Python take a slow processor and makes it MUCH slower. A 16 MHz AVR running native code is acceptable for lots of stuff and (~50x faster than a 6502), but saddle it with an interpreted language and it's going to be a lot slower than a 1970s micro. Still ok for many things, but that also cuts out a lot of possibilities.

Still faster than a 6502 or z80 running BASIC, probably, but I learned how to POKE 6502 instructions into RAM and run them on probably my 2nd or 3rd day with the machine, it was THAT necessary. Can CircuitPython even do that? On a Harvard machine like an AVR?

Your Python example is not significantly simpler than Arduino C++ code:

Code: [Select]
void setup() {
  pinMode(LED_BUILTIN, OUTPUT);
}

void loop() {
  digitalWrite(LED_BUILTIN, HIGH);
  delay(500);
  digitalWrite(LED_BUILTIN, LOW);
  delay(500);
}

In fact the Python has 13 more "words" than the C++. And 6 fewer punctuation characters. At best I'd call it a draw, but I think the cognitive advantage might even lie with C++ here. The use of semicolons and { } is highly stylized and learned in minutes.
« Last Edit: March 18, 2024, 10:02:00 pm by brucehoult »
 
The following users thanked this post: pcprogrammer

Offline pcprogrammer

  • Super Contributor
  • ***
  • Posts: 3710
  • Country: nl
Re: Getting started with microcontrollers
« Reply #74 on: March 18, 2024, 07:58:41 am »
Indeed the separation between functions and actions is much clearer in C than in Python, where it is based on the number of indents, for as far as I'm aware. Have not done much in Python to be honest, and there is a reason for it. To me the learning curve for Python seems much steeper than for plain C. Why because you have to dive into the standard functions much more to get things done. In C I can get everything I want done with just the basic syntax. No need for any external library.

Having the memory handling done for you might be easier, but it still needs you to think about what to put in it and how to do this. I tried ones to do something with Python and wanted to put some data in an array, but not in the first location. Turns out you have to set up the array first to be fully filled or use dedicated functions for it or something like that.

In C I find this to be much easier and clearer. Declare an array with some dimensions and write to the location you want with the standard array indexing. Sure you can write outside the array and do stupid things, but I expect you can do stupid things in any language.

But to each his own.


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf