Create an object placeholder?

This one may be difficult to answer since I dont quite know how to explain what im looking for!

I have made a .h/.cpp in my project to make an object of a battery.
In my sketch, I have 4 battery objects instantiated.

My loop is currently structured like this:

void loop(){

  if (battery_1.voltage > 4.25)
    displayBatt1warning();

  if (battery_2.voltage > 4.25)
    displayBatt2warning();

  if (battery_3.voltage > 4.25)
    displayBatt3warning();

  if (battery_4.voltage > 4.25)
    displayBatt4warning();
}

How can i change it so that its something like this, with a kind of a placeholder i can insert wherever:

void loop(){

  for (int batteryNum = 1; batteryNum < 5; batteryNum++){  //  loop 4 times

    if ("batteryNum".voltage > 4.25)
      displayBatt"batteryNum"warning();

  } // end for 

}

Is this even possible?

class Batterie {
    const char* pmName;
  public:
    Batterie(const char* pmNamed) : pmName(pmNamed) {}
    float voltage() {
      return random(100) * 0.05;
    }
    void displayWarning() {
      Serial.print(F("Warning for "));
      Serial.println((__FlashStringHelper*) pmName);
    }
};

const char PROGMEM b1[] = "Batt1";
const char PROGMEM b2[] = "Batt2";
const char PROGMEM b3[] = "Egon";
const char PROGMEM b4[] = "Wolfram";

Batterie batts[] = { b1, b2, b3, b4 };

void setup() {
  Serial.begin(250000);
  for (auto bat : batts) {
    if (bat.voltage() > 4.25)
      bat.displayWarning();
  }
}
void loop() {}
Code:

---



```
class Battery {
private:
const static float MAX_VOLTAGE = 4.25;
const static float V_REF = 5.0;
const static int ADC_RESOLUTION = 1 << 10;
const static Print &p = Serial;
public:
Battery (uint8_t pin) : pincolor=#000000[/color] {}
void checkVoltagecolor=#000000[/color] {
if (getVoltagecolor=#000000[/color] > MAX_VOLTAGE)
displayWarningcolor=#000000[/color];
}
float getVoltagecolor=#000000[/color] {
return color=#000000[/color] analogReadcolor=#000000[/color] * V_REF / ADC_RESOLUTION;
}
void displayWarningcolor=#000000[/color] {
p.print(F("Battery on pin "));
p.printcolor=#000000[/color];
p.println(F(" exceeds the maximum voltage!"));
}
private:
const uint8_t pin;
};

Battery batteries[] = {
 { A0 },
 { A1 },
 { A2 },
 { A3 },
 { A4 }
};

void setupcolor=#000000[/color] {
 Serial.begincolor=#000000[/color];
}

void loopcolor=#000000[/color] {
 for (Battery &battery : batteries)
   battery.checkVoltagecolor=#000000[/color];
}
```

|

Whandall:

  for (auto bat : batts) {

That'll create a copy of the battery, which is probably not what you want :wink:

While the copy does not hurt in this case, just use the non copy version

 for (Batterie& bat : batts) {

OR the classic version with no worries about that sort of thing:

+1 in Spades

Delta_G:
OR the classic version with no worries about that sort of thing:

for (int i = 0; i < 4; i++){

if(batteries[i].voltage > 4.25){
      batteries[i].displayWarning();
    }
}

The main reason I like the range-based for-loops is that you don't have to update all loop limits if you decide to add an extra battery to the array.

Thanks for the suggestions guys i really appreciate it.
Honestly I dont undertand the first couple of posts, i get the array idea but the classes stuff..not so much
I tried the simple methods like:

Battery battery_1;
....

for (int i = 1; i < 5; i++){

  if(battery_[i].voltage > 4.25){
  ....
  }

}

but then it doesnt know what "battery_" is

Battery 1;

for (int i = 1; i < 5; i++){

  if([i].voltage > 4.25){
  ....
  }

}

and that wont let you create a object name beginning with a number

You have been shown how to do it.

In this context classes are just array elements like any other array element.

You have to understand that names are only meaningful at compile time, and the for-loop is executed at run time. So you cannot use the index of the loop to change the name of a variable.

Start from the working code Whandall and I posted, and try to understand it.
Then ask concrete questions if you get stuck.

Of course I have a lot more learning do before I can make sense of your solutions. Not wanting anybody to do the heavy lifting for me.
Here is my entire project, working as-is, if you want to get a better idea of the way its been written.

18650_x4_class.ino (17.5 KB)

18650_x4_config.h (4.7 KB)

Cell.cpp (734 Bytes)

Cell.h (581 Bytes)

I only gave the project the project in its previous form as it was a complete mess when started messing with the class modifications and array ideas.
As i said, i do not want people to do work for me nor do i deserve it.
I thought i had understood the array part but clearly not, whatever mental block was making me a dumbass for not seeing the obvious. I should'nt have needed a forum post tbh!
I went about the arrays in some over complicated tangent.

I can rework a lot of the structure around this new idea and get it they i had imagined.
Thanks again for all the collective help

Strings (especially with a capital S) are a terrible way to pass a configuration to a function or to keep states. Use enums instead.

Why do you create classes with public data members only, and then write the routines to update their state multiple times outside of the class? That doesn't make any sense. Encapsulate that into methods, as shown in my first reply.

Another method that works is to create a static linked list using code in the constructor:

class Battery {
  static Battery *head;
  Battery *next;

  void foo() {
  }

public:
 
  Battery() {
    next = head;
    head = this;
  }

  static fooAll() {
    for(Battery b = head; b; b = b->next) {
      b.foo();
    }
  }
  
};

Battery *Battery::head = NULL;