Closed LoveMyData closed 5 years ago
It could be done but because of the way this library works I am not sure if it makes sense to change the pin at run-time. By looking at your code I can not figure out what are your goals. You may need to devote some time to study pointers and make sure if you really need an array of switch objects. In your code a setter would overwrite the pin of the object, and it would not create a different object to control every pin (which seems to be you intention).
I think that what you need (same code - different platforms) is to change the pin at compile time. I suggest you to have a look at preprocessor statements and conditional compilation. Something like this:
'
#define PIN 12
#define PIN 4
Switch pushButton = Switch(PIN); '
Please close the issue if this answers you question.
I am coding a web interface on the mega and would like to have the ability to change what pin to use as vswitch (input) at run time and depending on the action, ability to program which pin or pins to use as output.
But I do see your point
That is clearer. Let me know if the following suggestion helps:
Do you have a snippet of code that I can try?
I am kind of understand the concept on what you are saying but I have no idea on how CPP works in detail.
Many Thanks in advance
I recommend that you start by getting a deeper understanding of c++ before going for more complex hardware programming. See here a resource on the particular topic of "new" and "delete": http://www.cplusplus.com/doc/tutorial/dynamic/
I am closing the issue because this is no longer related to the library. Best luck in your quest for harnessing c++ and hardware.
Thanks for the information and suggestions
for anyone in the future may find this. I am testing this on Arduino mega and using pin 22 to 38 as input. Using dynamic memory allocation to assigned switch port based on a variable. Not sure is this right or not, but seems working so far based on Martin's suggestion.
#include <avdweb_Switch.h>
Switch *vSwitch[16];
int i;
void setup() {
Serial.begin(9600);
for ( unsigned int i=0; i<16; i++) {
vSwitch[i] = new Switch(22+i);
}
}
void loop() {
for ( unsigned int i=0; i<16; i++) {
vSwitch[i]->poll();
if(vSwitch[i]->switched()) { Serial.print("pushButton switched on switch "); Serial.println(i); }
if(vSwitch[i]->pushed()) { Serial.print("pushButton pushed on switch "); Serial.println(i); }
if(vSwitch[i]->released()) { Serial.print("pushButton released on switch "); Serial.println(i); }
}
}
Thanks for the library, this is the easiest one to use by far.
I have a challenge at this point as I am trying to create code that is can work on various boards and they have different PIN number to start.
I am trying to create something like this and I am hoping this make sense (sorry, pretty new to C++). With a 'setter', I think it may works.