I am reading 6 potentiometers and sending those values out to control LEDs. I am trying to understand which way will have 1)less memory use 2)less latency
Is it better to have 6 variables , read them each individually, and then send the values out
> void loop() {
> input pot1 = analogRead(pinA0);
> input pot2 = analogRead(pinA1);
> input pot3 = analogRead(pinA2);
> input pot4 = analogRead(pinA3);
> input pot5 = analogRead(pinA4);
> input pot6 = analogRead(pinA5);
> Serial.println(pot1);
> Serial.println(pot2);
> Serial.println(pot3);
> Serial.println(pot4);
> Serial.println(pot5);
> Serial.println(pot6);
> }
or
loop an index through an array like so
> char potPins[] = {
> A0,A1,A2,A3,A4,A5
> };
> int potCount = 6;
> void setup() {
> for (int thisPot = 0; thisPot < potCount; thisPot++)
> pinMode(potPins[thisPot], INPUT);
> }
>
> void loop() {
> for (int thisPot = 0; thisPot < potCount; thisPot++)
> inputPot = analogRead(potPins[thisPot]);
> Serial.println(inputPot);
> }
I know something is wrong with my loop because I get the error on compilation that
'inputPot' was not declared in this scope
though I dont know why.
Hi,
Your second code looks good, arrays for that many inputs is ideal.
You need to declare inputPot as an integer
int inputPot
Before your void setup()
Tom....
1 Like
Hello skypickle
Try this:
int inputPot = analogRead(potPins[thisPot]);
to 1. the compiler is publishing these information
to 2. use the micros() function to make some timing measurements
Have a nice day and enjoy coding in C++.
Дайте миру шанс!
1 Like
That will not compile. What is the input part for?
You don't need to set the pins to input for analog inputs.
Where do you declare inputPot?
Try:
int inputPot = analogRead(potPins[thisPot]);
That declares inputPort and reads a value into it.
1 Like
Hello
Try this small example for your own "R&D" activities.
constexpr byte AnalogPins[] {A0,A1,A2,A3,A4,A5};
int inputPot;
void setup() {
Serial.begin(9600);
}
void loop() {
for (auto AnalogPin:AnalogPins) inputPot = analogRead(AnalogPin), Serial.println(inputPot);
}
Have a nice day and enjoy coding in C++.
Дайте миру шанс!
Sorry, but that seems a bit esoteric for a beginner. Is that explained somewhere in the Arduino reference?
Hello
The Arduino reference is missing some smart C++ instruction to design a proper code.
for (auto AnalogPin:AnalogPins)
is a so called range based for loop
analogRead(AnalogPin), Serial.println(inputPot);
the comma is used a chain operator
hth
This reference is a good start to learn how to.
https://www.learncpp.com/
Have a nice day and enjoy coding in C++.
Дайте миру шанс!
Thank you all for your generous replies.
@groundFungus ’input pot1’ should have been ‘inputpot1’, etc
In reviewing your replies I also noticed that I don’t have define statements. Isn’t that a problem?
Also I wanted to ask if all the pots are turned fully ‘on’ , how many amps will the chip be using? Isn’t there a danger of a ‘short’ circuit?
You won't draw anymore current if they are all clockwise or anticlockwise.
Can you please post a circuit diagram of how you aim to connect the pots?
Thanks.. Tom...
Left pot leg to ground
Right pit leg to vcc on chip
middle pot leg to analog pin
Since these are 10 kOhm pots, on a 5v line each will draw 0.5 milliamps. 6 of these will draw 3 mA from the chip total
Pro micro
I’m not using the pin labeled’raw’. My impression was that the pin labeled vcc comes from a regulator in the chip
system
Closed
October 24, 2022, 2:32pm
15
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.