Hello willgreen98
I like arrays too. And specially structured arrays. I´ve made simply an object containing all relevant data:
ANALOGREAD analogreads[] {
{0,"A0 Works: ", A0, 1000, 0, Blink},
{0,"A1 Works: ", A1, 1000, 0, Blink},
{0,"A2 Works: ", A2, 1000, 0, Blink},
{0,"A3 Works: ", A3, 1000, 0, Blink},
};
The first entry contains the read value, the second the name of the analogue port, with 1000msec update rate and some control data for the timer per member.
The loop() controls the timer functions for each member to read the analogue value. Check it and try it.
/* BLOCK COMMENT
ATTENTION: This Sketch contains elements of C++.
https://www.learncpp.com/cpp-tutorial/
Many thanks to LarryD
https://europe1.discourse-cdn.com/arduino/original/4X/7/e/0/7e0ee1e51f1df32e30893550c85f0dd33244fb0e.jpeg
https://forum.arduino.cc/t/help-coding-multiple-potentiometers/993521
Tested with Arduino: Mega[ ] - UNO [ x - Nano [ ]
*/
#define ProjectName "Help coding multiple potentiometers"
// VARIABLE DECLARATION AND DEFINITION
unsigned long currentTime;
enum {Stopp, Start, Blink};
struct TIMER { // has the following members
unsigned long duration; // memory for interval time
unsigned long stamp; // memory for actual time
int onOff; // control for stop/start/blink
};
struct ANALOGREAD {
int value;
String name_;
byte pin;
TIMER wait;
};
ANALOGREAD analogreads[] {
{0,"A0 Works: ", A0, 1000, 0, Blink},
{0,"A1 Works: ", A1, 1000, 0, Blink},
{0,"A2 Works: ", A2, 1000, 0, Blink},
{0,"A3 Works: ", A3, 1000, 0, Blink},
};
bool timerEvent (TIMER &timer) {
bool reTurn = currentTime - timer.stamp >= timer.duration && timer.onOff;
if (reTurn) timer.onOff == Blink ? timer.stamp = currentTime : timer.onOff = Stopp;
return reTurn;
}
// -------------------------------------------------------------------
void setup() {
Serial.begin(9600);
Serial.println(F("."));
Serial.print(F("File : ")), Serial.println(__FILE__);
Serial.print(F("Date : ")), Serial.println(__DATE__);
Serial.print(F("Project: ")), Serial.println(ProjectName);
pinMode (LED_BUILTIN, OUTPUT); // used as heartbeat indicator
}
void loop () {
currentTime = millis();
digitalWrite(LED_BUILTIN, (currentTime / 500) % 2);
for (auto &analogread : analogreads) if (timerEvent (analogread.wait)) analogread.value=analogRead(analogread.pin), Serial.print(analogread.name_), Serial.println(analogread.value);
}
Have a nice day and enjoy coding in C++.
Дайте миру шанс!