Hi all,
what I'm trying to do is create a lamba function to use as an interrupt, to create a class that will automatically generate it's interrupt functions. The end use is for an encoder class, because each object will need four interrupts and I don't want to hardcode the number of encoders/interrupts etc.
Now what I'd like to do is create an array of pointers to all the encoders in use, and index from that array to create each interrupt. From my understanding I should be able to capture by value, not reference, any variables used in the lambda function. I can do all of this except the capture by value part. I've been testing the idea on a much simpler LED class and timerOne (because It provides quick visual feedback). The code
what I'm finding is that the code compiles and runs, but the led will change after the creation of the lamba if count is changed. Count has to be static, as well as the array of pointers - meaning the lambda is capturing the values by reference not by value; even though I'm using "[=]" which should make it capture by value. Can anyone tell me how to force it to capture by value?
thanks
what I'm trying to do is create a lamba function to use as an interrupt, to create a class that will automatically generate it's interrupt functions. The end use is for an encoder class, because each object will need four interrupts and I don't want to hardcode the number of encoders/interrupts etc.
Now what I'd like to do is create an array of pointers to all the encoders in use, and index from that array to create each interrupt. From my understanding I should be able to capture by value, not reference, any variables used in the lambda function. I can do all of this except the capture by value part. I've been testing the idea on a much simpler LED class and timerOne (because It provides quick visual feedback). The code
Code:
#include <TimerOne.h>
int numberOfLeds =0;
class LED{
public:
const byte _ledPin;
LED(const byte pin) : _ledPin(pin) {
numberOfLeds++;
}
void toggle() {digitalWrite(_ledPin, !(digitalRead(_ledPin)));}
};
LED led1(13);
LED led2(14);
void setup(void)
{
pinMode(14, OUTPUT); // I've hard coded this bit for now
pinMode(13, OUTPUT);
static LED ** pointers = new LED * [numberOfLeds]; // this array has to be static to work atm
pointers[1] = &led1;
pointers[0] = &led2;
static int count = 1;
Timer1.initialize(150000);
Timer1.attachInterrupt([=](){pointers[count]->toggle();}); // blinkLED to run every 0.15 seconds
//count = 0; // uncomment This line and led2 will blink instead
Serial.begin(9600);
}
void loop(void)
{
delay(100);
}
what I'm finding is that the code compiles and runs, but the led will change after the creation of the lamba if count is changed. Count has to be static, as well as the array of pointers - meaning the lambda is capturing the values by reference not by value; even though I'm using "[=]" which should make it capture by value. Can anyone tell me how to force it to capture by value?
thanks