Help coding multiple potentiometers

I'm trying to find some information/advice on how to go about coding to read multiple analog inputs. I'm creating a device in Max and am creating a controller with 4 potentiometers for the parameters on my device, I have got all of the pots to work individually but I'm lost when it comes to coding them all together. I'm using the simple 'AnalogReadSerial' and 'ReadAnalogVoltage' to try and achieve this, but I often get error messages. I am very new to this, I can't seem to find anything online which leads me to believe I am looking for the wrong thing. Any help would be great!

Here's what I've put so far:

// the setup routine runs once when you press reset:
void setup() {
// initialize serial communication at 9600 bits per second:
Serial.begin(9600);
}

// the loop routine runs over and over again forever:
void loop() {
// read the input on analog pin 0:
int sensorValue = analogRead(A0);
Serial.println("A0 Works");
delay(1);
int sensorValue = analogRead(A1);
Serial.println("A1 Works");
delay(1); // delay in between reads for stability
}

The logic behind this is I want to be able to read the pots separately.
This is the error message:

Arduino: 1.8.19 (Mac OS X), Board: "Arduino Uno"

/var/folders/2h/1v85b0t1461gl8qnzhw42bv40000gn/T/arduino_modified_sketch_162508/AnalogReadSerial.ino: In function 'void loop()':
AnalogReadSerial:25:7: error: redeclaration of 'int sensorValue'
int sensorValue = analogRead(A1);
^~~~~~~~~~~
/var/folders/2h/1v85b0t1461gl8qnzhw42bv40000gn/T/arduino_modified_sketch_162508/AnalogReadSerial.ino:22:7: note: 'int sensorValue' previously declared here
int sensorValue = analogRead(A0);
^~~~~~~~~~~
exit status 1
redeclaration of 'int sensorValue'

This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.

What, exactly, do you want the Arduino code to do? How does the pot information get to the PC app? What does the PC app expect?

Post the code. Read the forum guidelines to see how to properly post code and some information on how to get the most from this forum.
Use the IDE autoformat tool (ctrl-t or Tools, Auto format) before posting code in code tags.

Tell us what the code actually does and how that differs from what you want.

Please include the entire error message. It is easy to do. There is a button (lower right of the IDE window) called "copy error message". Copy the error and paste into a post in code tags. Paraphrasing the error message leaves out important information.

Like that?

Please remember to use code tags when posting code.

I want the arduino to read the voltages of multiple potentiometers so I can use the 4 controls to control my device (this can be done using Max, but I don't know how to tell the arduino to read and output the readings of the different pots.)

Hope that makes sense, apologies for not formatting correctly in original post.

Yes this is what I've tried so far

Read what I posted, again.

In the code in the previous post you declared the sensorValue variable twice. That results in an error.

int sensorValue = analogRead(A0);
The int sensorvalue is a variable declaration because it names the variable's data type (int). You can only declare a variable one time. When you say int sensorvalue again a couple of lines later, it's an error. Declare the variable one time and there after, leave the data type off to use the variable.

If I were going to read 4 pots and transfer the data, I would read them into and array.

Use arrays like this:

// the setup routine runs once when you press reset:
const byte inputsArray[] = {A0, A1, A2, A3};
const byte NUM_POTS = sizeof(inputsArray) / sizeof(inputsArray[0]);
int potValueArray[NUM_POTS];

void setup()
{
   // initialize serial communication at 9600 bits per second:
   Serial.begin(9600);
}

// the loop routine runs over and over again forever:
void loop()
{
   static unsigned long timer = 0;
   unsigned long interval = 1000;
   if(millis() - timer >= interval)
   {
      timer = millis();
      for (byte n = 0; n < NUM_POTS; n++)
      {
        potValueArray[n] = analogRead(inputsArray[n]);
        Serial.print("Channel A");
        Serial.print(n);
        Serial.print(" = ");
        Serial.println(potValueArray[n]);
        Serial.println();
      }
   }
}

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++.
Дайте миру шанс!

This is fantastic thank you so much! It works great, at the moment I'm trying to get it to communicate with Max but it's quite difficult

Would anyone be able to suggest ways to have the arduino directly output MIDI instead of the serial numbers? I'm struggling to find a way to get Max to read the output messages from the arduino

Control Surface: Multiple-Control-Change-Potentiometers.ino

Control Surface: MIDI over USB

I've tried following the first link but I receive this error message:

Arduino: 1.8.19 (Mac OS X), Board: "Arduino Uno"

sketch_may22a:1:10: fatal error: Control_Surface.h: No such file or directory
 #include <Control_Surface.h> // Include the Control Surface library
          ^~~~~~~~~~~~~~~~~~~
compilation terminated.
exit status 1
Control_Surface.h: No such file or directory


This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.

You have to install the library first. See Control Surface: Installation

My mistake! Will this work with an Arduino Uno? I've uploaded the code all fine but it's not being discovered by my DAW etc.

See the section "Arduino boards with an ATmega16U2 (ATmega8U2)" on Control Surface: MIDI over USB

I've followed all of the steps and completed the dfu-programmer steps, but when I go to MIDI map the device, it's just coming up as pitch bend for every pot and not moving once it's mapped. Ableton is recognising the fast tat some MIDI data is coming in, but I'm lost as to why I can't map stuff individually

What's the code you're using? Which firmware? Which baud rates?

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.