Some help understanding Classes

Hi,

I've recently been learning about Classes and see now the benefit they hold in making programs run smoother and having less code in your sketch overall. Here is where I'm stuck a little:

Let's say I want to control 4 motors, and I make an intance for each one with my desired functions (or methods). Most of the time I will want to control each one individually but sometimes I will want to run commands that affect all of them (increase speed overall, shutoff, etc...).

I suppose I could write the same command four times to each one and control them that way, but it got me wondering as to whether there exists some special functions I can use to control all instances of a class. Like change a state or a flag accross all of them. Is this available?

Thanks in advance.

but it got me wondering as to whether there exists some special functions I can use to control all instances of a class. Like change a state or a flag accross all of them. Is this available?

Static methods belong to the class, not a specific instance of the class. You might be able to create a static method that does what you want.

A more usual technique, though, is to put the instances in an array, and simply iterate over the instances using a for loop.

PaulS: Static methods belong to the class, not a specific instance of the class. You might be able to create a static method that does what you want.

A more usual technique, though, is to put the instances in an array, and simply iterate over the instances using a for loop.

Is iterating over class intances the most economic way of doing it? I thought that using these static methods would make it more efficient to give commands to all motors at once.

I thought that using these static methods would make it more efficient to give commands to all motors at once.

That would depend on whether you are able to. Only you have the hardware; only you can tell whether you can write a static method to do what you want.

Telling the instances to do something takes next to no time. For the instance to implement the change will require more time, but that doesn't necessarily mean blocking code.

And why are you concerned about finding the "most economic way of doing it"? You should instead focus on writing clean, safe code that works as expected. That will generally also be a reasonably "economic" way of doing it. Only worry about being "most economic" in the unlikely event you find yourself running out of bandwidth or memory. There is a tremendous amount of computing power in these things, and well-written code should not be having resource problems for 99.9% of all typical applications.

Regards, Ray L.

RayLivingston:
And why are you concerned about finding the “most economic way of doing it”? You should instead focus on writing clean, safe code that works as expected. That will generally also be a reasonably “economic” way of doing it. Only worry about being “most economic” in the unlikely event you find yourself running out of bandwidth or memory. There is a tremendous amount of computing power in these things, and well-written code should not be having resource problems for 99.9% of all typical applications.

Regards,
Ray L.

Cheers Ray, but my program is running at ~27kB at the moment and I need to make it as simple as possible. For debugging purposes. As I have some larger projects in mind I’m trying to learn the methods that require the least amount of computing time to avoid issues later on.

lacanau: Cheers Ray, but my program is running at ~27kB at the moment and I need to make it as simple as possible. For debugging purposes. As I have some larger projects in mind I'm trying to learn the methods that require the least amount of computing time to avoid issues later on.

As cheap as Arduinos are, just buy a bigger one. I buy ProMinis for $2 each, Mega2560s for about $11 each, Dues (80MHz ARM7, 512K FLASH, 96K RAM) for $15 each. It's kinda silly to waste time optimizing code when more horsepower costs, literally, pennies.

Regards, Ray L.