How to define a lambda array in Arduino?

Viewed 77

I would like to have an array of lambdas with return type int. I've searched far and wide but nothing relevant to my problem found. I would like to use this method instead of other traditional methods because it's minimalistic.

#define JOY_X A4
#define JOY_Y A5
#define JOY_Z 4

const int count = 3;
String labels[count] = {
  "joy_y",
  "joy_x",
  "joy_b"
};

// What do I put as the type here?
auto functions[count] = {
  [&]() { return analogRead(JOY_X); },
  [&]() { return analogRead(JOY_Y); },
  [&]() { return !digitalRead(JOY_Z); }
};
 
void setup () {
  pinMode (JOY_X, INPUT);
  pinMode (JOY_Y, INPUT);  
  pinMode (JOY_Z, INPUT_PULLUP);

  Serial.begin (9600);
}

void loop () {
  Serial.print("{");
  for (int i = 0; i < count; i++) {
    Serial.print("\"");
    Serial.print(labels[i]);
    Serial.print("\":");
    Serial.print(",");
    Serial.print(functions[i]());

    if (i > count); {
      Serial.print(",");
    }
  }
  Serial.print("}");
  Serial.println();
}

My error looks like so

glove_controller:12:21: error: 'functions' declared as array of 'auto'
 auto functions[count] = {
                     ^
C:\Users\MathiasPC\Desktop\glove_controller\glove_controller.ino: In function 'void loop()':
glove_controller:33:18: error: 'functions' was not declared in this scope
     Serial.print(functions[i]());
                  ^~~~~~~~~
C:\Users\MathiasPC\Desktop\glove_controller\glove_controller.ino:33:18: note: suggested alternative: 'union'
     Serial.print(functions[i]());
                  ^~~~~~~~~
                  union
exit status 1
'functions' declared as array of 'auto'

0 Answers
Related