How to move blynk operations into a class

not the answer you’re looking for, but for such cases when “classes” would be needed, i use arrays instead.
lets take your example, with the room, and say each room has the following members:

  • temperature actual
  • temperature target
  • heater relay gpio pin

i would create 3 arrays with the max number of the rooms (let’s say we have 10 rooms), with the needed types:

float roomActualTemp[ROOMS];
float roomTargetTemp[ROOMS];
byte roomHeater[ROOMS];       // output pins for heater relays

because all the arrays have the same length and indexing order, one can easily refer and write logic to any room any “member”, in all possible combinations:

#define HYST   1  // hysteresis
#define ROOMS 10

void checkRoomsTemp()
{
  for (byte i = 0, i < ROOMS, i++) {
    if (roomTemp[i] < roomTargetTemp[i])             digitalWrite(roomHeater[i], HIGH);
    else if (roomTemp[i] > roomTargetTemp[i] + HYST) digitalWrite(roomHeater[i],  LOW);
  }
}

you can do the same for timers, timestamps, buttons, etc. for more complex projects you can even use multi dimensional arrays, like this:

int rooms[ROOMS][3];

rooms array has 3 registers, where:
[0] = actual temp
[1] = target temp
[2] = heater relay gpio pin

then, the above code example would look like this:

void checkRoomsTemp()
{
  for (byte i = 0, i < ROOMS, i++) {
    if (rooms[i][0] < rooms[i][1])             digitalWrite(rooms[i][2], HIGH);
    else if (rooms[i][0] > rooms[i][1] + HYST) digitalWrite(rooms[i][2],  LOW);
  }
}

hope this makes sense… i used this strategy in several bigger projects and it worked great. if one designs the code carefully, it is very easy to scale the project:

for example, if you need to go from 10 room to 100, just replace the #define ROOMS 10 and ready to go. you do not have to change anything else in the code!

also, a bit different topic, but it is helpful for code organising and fragmenting, to use the tabs functionality in the arduino ide. i have wrote about this some time ago, here:

3 Likes