Hello,
I have an Arduino device where I need to monitor status of 32 pins for reed sensors. To make things easier, I put everything in for loop as much as I can, but I'm running into some roadblocks and I could use a little help.
I started off with this example (https://github.com/mysensors/MySensorsArduinoExamples/blob/master/examples/BinarySwitchSensor/BinarySwitchSensor.ino) and examples from forums on this site (https://forum.mysensors.org/topic/264/help-with-arrays-and-debouncing). Second one was good for helping me simplify, but still would be too cluttered for 32 pins. I tried my modifications seen below:
#define MY_DEBUG
#define MY_GATEWAY_SERIAL
#define MY_BAUD_RATE 9600
#include <MySensors.h>
#include <Bounce2.h>
Bounce debouncer[32];
int oldValue[32];
MyMessage msg[32];
void setup() {
for (byte i = 22; i <= 54; i++) {
debouncer[i] = Bounce();
oldValue[i] = -1;
}
for (byte i = 22; i <= 54; i++) {
pinMode(i, INPUT);
digitalWrite(i, HIGH);
debouncer[i].attach(i);
debouncer[i].interval(5);
}
}
void presentation() {
for (byte i = 22; i <= 54; i++) {
present(i, S_DOOR);
}
}
void loop() {
for (byte i = 22; i <= 54; i++) {
debouncer[i].update();
int value = debouncer[i].read();
if (value != oldValue[i]) {
send(msg[i].set(value == HIGH ? 1 : 0));
oldValue[i] = value;
}
}
}
I understand that since MyMessage objects are created at definition, I can't use "msg (i, V_TRIPPED)" in setup(). I can't seem to use constructor methods from MyMessage.cpp properly either. I tried code below in setup() but that threw error message 'unresolved overloaded function type' for both lines.
msg[i].setSensor[i];
msg[i].setType[V_TRIPPED];
Is there a better way to set up 32 instances of the message object with a for loop? I plan to add more sensors of different types once I get these working, so I would like to keep this in a for loop to make it maintainable.
Thank you.