Programming in Arduino is SO linear.

I don't know, but I feel that programming in Arduino (or C or C++ or whatever we should call it) is sooooo linear. Before programming in Arduino I used to program in Visual Basic. I never thought that Arduino will change your view in programming. For example timers in Visual Basic is very easy to use. But when it comes to Arduino you will be like "what the heck? why is it so cumbersome to set up a timer?" or "damn I cant do this while im doing that... what the heck is an interrupt!!!!!!??????" Nah... but its fun you know. Unlike programming in visual basic, your software can actually interact with physical things and you get the gist of how computers really work.

Sorry for my bad english by the way. I'm trying hard to improve my english. =D

Actually, before Visual Basic, every program was linear :slight_smile: Start to Finish ... then optionally loop back to start if you want.

But when it comes to Arduino you will be like "what the heck? why is it so cumbersome to set up a timer?" or "damn I cant do this while im doing that... what the heck is an interrupt!!!!!!??????"

But don't forget, you're doing all of these in a 32K memory space. (or less!)

You're also programming very very close to the hardware. i.e. there is no BIOS, no operating system, no runtime library, between your program and the chip.

I can think of a few other words to describe Visual Basic, but perhaps they are not suitable here. :wink:

Larger operating systems can hide the basic linearity of a processor chip behind preemptive multitasking operating systems, where things "seem" to happen "all at once" but they don't really, not unless you have multiple processors and even then you usually only have one piece of RAM and one disk drive.

So yes, it's fun. And informative. You are learning what really happens in a modern computer.

By the way, modern operating systems use interrupts extensively, you just don't need to know that. :stuck_out_tongue:

Nikkoderic, Vasquo,..... you speak from what you know but you don't know know as much as you let on especially about what was before you were maybe born. It's not a big deal, every new generation does it.

There was LOTS of parallel processing and multitasking long before VB was built. I had a multi-tasking BASIC on my PC-XT back in 1988, it cost me all of $50.

Vasquo if you stop practicing top-down coding then you can write real-time reactive code. If you need more then start using multiple processors. Until then you can do a very convincing many-things-at-once with a single AVR and still be wasting cycles. Look up and embrace Finite State Machine.
Also get solid with the BlinkWithoutDelay example that came with your IDE (except change the longs used for holding time values to unsigned longs, it works as is but it's not right and I leave it to you to find out why) and you should have very little need to actually mess with the AVR timers since you'll be using one via millis() or micros().

GFS, thanks for adding to the discussion.

I know FSM and use it in the firmware for my products. Yes to the user, It kinda "acts" like multitasking but you know behind the scenes, it's still a linear flow.

Use of interrupts is also another way to simulate multitasking. Again, to the user it seems the microprocessor is doing many things all at once, but behind the scenes, you're just switching single tasks quickly.

I think what the OP was talking about is VB being event-driven in its programming.

But when it comes to Arduino you will be like "what the heck? why is it so cumbersome to set up a timer?" or "damn I cant do this while im doing that... what the heck is an interrupt!!!!!!??????"

its very nice when you have gig's of DLL's, an OS, and whatnot shielding that from you, but its a simple system, means you have to get close to the metal. I recently wrote a little datalogger on a old pentium laptop using qbasic, and for accurate timer events what was I doing, setting up a timer interrupt, not that much different from arduino

vasquo:
GFS, thanks for adding to the discussion.

I know FSM and use it in the firmware for my products. Yes to the user, It kinda "acts" like multitasking but you know behind the scenes, it's still a linear flow.

Use of interrupts is also another way to simulate multitasking. Again, to the user it seems the microprocessor is doing many things all at once, but behind the scenes, you're just switching single tasks quickly.

I think what the OP was talking about is VB being event-driven in its programming.

What, you can't write an event-driven state machine?

if ( x = digitalRead( irSensePin ) { // respond

or

if ( Serial.available()) { // respond

or

if ( millis() - startTime >= waitTime ) { // respond to timeout

That's weird. I can and have been in one form or another since about 1981. But then I wrote self-modifying code in 1980 before I came up with a better way that I was later told is called a state machine. I had a job to do and that's what fit. It certainly wasn't linear.

Execution is only linear if you make it so but in my experience linear coding is what is taught in schools for at least the first 3 semesters. The more rigidly structured the language used, the more that people fall into that pattern. One of my mini-crusades is to try to help people break that pattern but not everyone is ready nor is everyone capable.

Program = code + data.
Sensor/User input is data.

If I branch on a condition that is not part of the code nor can be predicted through the code then my code will not execute in a linear fashion. This is true no matter what language you use, IME.

That's weird. I can and have been in one form or another since about 1981. But then I wrote self-modifying code in 1980 before I came up with a better way that I was later told is called a state machine. I had a job to do and that's what fit. It certainly wasn't linear.

Dang. You must be a genius.

BTW, have you programmed in VB? Maybe we're thinking about different things when I say "event programming." Based on your IF examples above, it seems to be.

vasquo:
Actually, before Visual Basic, every program was linear :slight_smile: Start to Finish ... then optionally loop back to start if you want.

This is so Untrue.

The whole point of function declaration and implementation (hidden by Arduino IDE when using ino and PDE file) used in pascal and C(++) (and many more) is to be able to implement event driven programming at compile time.

On top of that Pascal and C(++) can work with function pointers. This means that you can change the behavior not only at compile time but also at run time. This is event driven programming at run time. (which is something IMHO is not easily done in VB).
VB(A) event driven programming is functionally the same as the C callback mechanism.
IMHO OP finds event driven programming very interesting because he can create many objects all with their own event handler. Basically this is the object orientation of the language. There are many other ways to solve this problem. If you want to solve it with multiple objects each with their own event handler you need to use a object oriented language like C++ language.

[rant]
Many people do not make a difference between the IDE functionality and the language functionality. VB is a very high level programming language in a very high level IDE. So you kind of have best of both worlds at the cost of CPU, storage and memory usage at development time and at run time.
CPU, storage and memory usage at development time is mostly not an issue (I say mostly because it is not fun explaining a manager he has to replace 2000 developers machines because your new version requires a better CPU or more memory)
CPU, storage and memory usage at run time is a big problem when developing for a device (FYI Arduino is a device)
[end of rant]
IMHO OP is mixing IDE functionality with language functionality. As he clearly is a Microsoft user I would advice him to have a look at the visual micro plugin for Arduino. That won't solve the problem but it will ease the pain.

Best regards
Jantje

Some history lessons:
Windows was/is? originally written mainly in C.
VB was originally "interpreted only". Later Microsoft added a compiler functionality to VB. The compiler is the same compiler as used in the C++ tool chain of Microsoft.

(Hmm? I did write a post, and it isnt here? Maybe I pushed "next" instead of "post". Anyhow, here retyped as far as I remember it. It was better first time : )

What is "linear" programming? The first time you write a if/for/while (or whatever, lets call it a "conditional branch") it is not "linear" anymore.

Or, are you (the OP) using Linear as opposed to Event driven, Message based, Multithreaded, Object Oriented or .... . Well, that is just smoke and mirrors, behind is the same "linear" programming. The increased abstraction sometimes yields more efficient programmer time, though rarely more efficient machine code (but the Ghz and Gb RAM can cope with that).

I wrote basic for money for over 10 years using 4 different versions of basic. People had to have basic and I must have been a very bad person in a previous life. By the time VB was invented people were paying me to write in C++.

I have seen Visual Basic and Visual C as well.

The Visual development environment does a lot for you and at the same time keeps you from needing to know how so much works. You won't find a Visual IDE for Arduino today so no ride, you must take each step yourself and write your own bugs.

But you can put separate code pieces to do each sub-task of your program inside of loop() and control which piece(s) run on any pass through loop() by using the kinds of conditionals I showed in the last post and of course switch-case blocks where machine state applies, which is how I break down larger tasks and keep execution of loop() as fast as I can.

So I have the blinking led and user entry through serial to change blink timing as desired and both run as if they own the AVR as far as my eyes can see. My user input task eats serial characters and evaluates them immediately while the blinking, even fast blinking, goes on. To Arduino, serial data comes in with a substantial pause between "available()"s. 1 millisecond is time to do a useful thing. The code "knows nothing" about when a serial character may arrive or the led will need to change state (on/off) and I assure you that those are Events that drive the program. There is also serial out for user feedback and still that program spends by far most of the time waiting for something to happen.

It is not do steps 1->2->3->4. It is do the code that depends on the current condition including user-io-state (to evaluate incoming serial data) -- every time through loop.

I can make it do much more. I twisted it around to drive my capacitive sensing experiments and my ultrasonic sensor experiments and a few more testings. I helped a South African use a version to read, verify and send his electrical meter data to his laptop and pass commands via XBEE to pumps and sensors in his hydroponics farm all at the same time still not using the full potential of a single 328P chip.

If you think that is much then you should see the software packages I wrote to pay the bills. Some of those jobs were even fun like the one writing aircraft training system software. I got to attend jet systems courses and "fly" multi-million-dollar full-motion sim boxes for 3 different corporate jets. That was a fun job. We were ahead of our time, that was in 1987 and 1988.

Msquare:
(Hmm? I did write a post, and it isnt here? Maybe I pushed "next" instead of "post". Anyhow, here retyped as far as I remember it. It was better first time : )

What is "linear" programming? The first time you write a if/for/while (or whatever, lets call it a "conditional branch") it is not "linear" anymore.

I think that he means tasks A->B->C always in order code, or something closely as simple. You know, intro and the next course top-down code. And I doubt that he knows about CPU registers and opcodes except that they exist.

MODCOMPS running Fortran 66 had very little memory, or processing power, but they were very fast at handling interrupts and doing real time process control. 2 feet thick, 7 tonne ingots of aluminium being squished (ok rolled) into 1-inch thick, long ribbons of said aluminium. An incredible sight to see.

Not something I'd want VB controlling. :astonished:

PDP-8 or 11?

GoForSmoke:
PDP-8 or 11?

I honestly do not recall. I was sub-contracted in to help convert the Fortran 66 code + macros to Fortran 77 without macros. I had a tour of the factory, very impressive. And got to make some mods to the user-created grinds and hardness testing MS Access DB that was about as denormalised as a bowl of fine Italian pasta. Eesh!

Good times, good times.

Those 7 tonne ingots are a sight to behold.

This is funny. My very first contact with programmation was during my 2nd year at university, and it was with Fortran 90. We did dull, boring things, and the teacher refused to learn us more "modern" stuff like visual interfaces or newer/more widespread languages. I hated him for that, but is was perhaps not a bad thing, in retrospective.

I guess only time will tell. It somehow helped me getting started quickly with Arduino and ROOT, but I can't help thinking things would have been easier if I had directly learned C at the time.

I had a semester of FORTRAN 4/5 before money trouble got me out of school and months later into the Army. I bought a programmable calculator in service and learned more logic and arrangement in a month with that primitive thing than a semester of punch cards and waiting for printout results gave me.

The simple thing with fast results was the far better learning tool. IMO simple projects are better for learning many techniques and big projects are better avoided until the student has covered the basics and has a mental toolkit of tricks and techniques to work with. IE don't expect to climb big mountains with little or no gear, training or experience on small ones and perhaps a climbing wall.

What do you think is the best programming style for Arduino (or microcontrollers)? Should I use other paradigm like object oriented, event driven? Or should I keep it as linear as possible?

I must be very lucky that I'm not dealing with analog computers (oh god the tubes too, i thought those were light bulbs). But that would be fun to build. When I was a little bit younger, I searched the internet on how to create my own OS but now yeah... i'll pass, got no time for that. I realized that you will need to know more about the hardware.

Mostly that depends on what you want to do and how you define the task. It's best to spend time making that definition as logical, clear and simple as possible. It should be the first thing that you debug, shrink and debug again. I write the steps as comments and then fill in the code which can even still have me change those comments to suit what actually worked.

When I write something linear like an example to show how unsigned math works, I fit it all in setup() and leave loop() empty. Top-down does fit some tasks.

For most all real-time programs with the Arduino I think that setup() and loop() are the way to go with a constantly repeating fast loop() at the heart of it. Whether you use Objects or not it is good to divide the program into separate small parts and control which of those run during any single execution of loop(). That strategy gives you advantages, check it out:

Separate small parts don't have to be weaved inside control structures. Each one runs only on a certain condition which may be the value of a global or static variable holding the state of the overall process, a kind of "what am I doing now" value that survives from one execution of loop() to the next.
One part may read a serial char if available, do something with it and change the state variable so that next time through loop it does the next task, but only if a separator char was read. The first part may be gathering digits and evaluating them to a number that is complete when a space is read so the next part that uses that number is run next time around loop() by changing the state.
The first part only runs if serial is available, the second only if the state is right. Nothing holds up execution and loop() may run many many times between serial reads.
One part may be watching the time to blink a led on/off and run every time through loop(). It should probably be above the serial available check as it is more time-critical.
Other parts may be watching sensors or other time-checks and/or for other state values.
But ALL those parts are separate blocks of code. Each could be a function but for the sake of speed, readability and stack use, keep the condition checks inside of loop() and have the function called only when the condition is met.
It will be much easier to debug separate parts than to debug the same functionality inside of control structures made to handle what state and time values do. Re-arranging the order of parts, adding or removing parts is greatly simplified through using separate parts rather than highly structured code.

This works well for me. What I write for one thing often has the skeleton of a future sketch in it that I just knock the parts I can't use out, reform the parts that are close and keep the ones that don't need change and I have then new thing much faster than if I wrote it ground-up.

You can look up Finite State Machine for more details and of course BlinkWithoutDelay to see more about time checking. You can mix those together inside loop(), even have multiple state machines together if you need or want. Of course if you write tight code then you only want what you need, right?

Class objects are collections of data with related functions. They're a way to package related code and data. I'm all for OOP, have been since 1983 when I learned OOP with Forth-79.

One code practice I object to on Arduino is dynamic memory allocation. Another is wasting memory. Use of String Objects does both so I really don't like using Strings on the Arduino. I prefer to plan out memory use ahead of time and shift data in and out of buffers (maybe to/from SD card or external RAM) if necessary. I code as if I'm short of RAM because starting with 2k for heap and stack, I am!

nikkoderic:
What do you think is the best programming style for Arduino (or microcontrollers)? Should I use other paradigm like object oriented, event driven? Or should I keep it as linear as possible?

Assembler boils down to move, modify, compare, jump, return. Every language is built on top of that.

Linear vs event driven vs OO is really a facade.

If you are programming something that needs to respond quickly, the less layers the better.