Using an array or two?

Hello to all…
Basically I’m trying to display numeric values from a pot (A0) in the serial monitor. This is simple enough to execute but what i want to do is have those values in three separate columns (val1,val2 & val3). I want to use a second pot (A1) to select one of the columns and when I press the button it records the numeric value of pot (A0) in that column.

I figured I’ll need an array, or two? to make this work but not sure how to execute it fully. As the code stands I’m only printing the values from pot (A1). I know this isn’t correct but not sure which direction I should be going in. Any help will be appreciated :slight_smile:

*/
#include <MIDI.h>

int buttonPin = 2;
boolean currentState = LOW;//stroage for current button state
boolean lastState = LOW;//storage for last button state
int potValue1 = 0;
int potValue2 = 0;

int val1 = 0;
int val2 = 0;
int val3 = 0;

int myvalues[] = {val1, val2, val3};

void setup() {
  Serial.begin(9600);
}


void loop() {
  currentState = digitalRead(buttonPin);
  if (currentState == HIGH && lastState == LOW) { //if button has just been pressed
    delay(10); //Button debounce
    potValue1 = analogRead(A0) / 4; 

    potValue2 = analogRead(A1);
    int index = potValue2 / 342;  //returns a value between 0 and 2
    //int myValue = myvalues[index];

    Serial.println(index);

  }

  lastState = currentState;

}

What do you mean by record? (keep in memory or just print out to Serial in columns 1,2 or 3)

yes, just print out to Serial in columns 1,2 or 3

So read A1, map it to 0, 1 or 2
Use that value to print 0,1 or 2 tabs then print the value of A0
Then loop and do it again

Thanks J-M-L, I'll give that a shot

Just to check....Do i need to use an array at all if I'm mapping A1 to 0,1 or 2?

How do I "Use that value to print 0,1 or 2 tabs"?

Should I be using "if" statements to do that?

You don’t really need an array as you don’t store any value, just print them

To map A1 you can
read value of A1 in a variable columnSlection
If columnSlection is between 0 and 341, remember nbTab is 0
else If columnSlection is between 342 and 682, remember nbTab is 1
else nbTab is 2

To print the right number of tabs you can use a for loop
for Index = 0 to nbTab-1 by increment of 1, print one tab

Thanks for that J-M-L...

int buttonPin = 2;
boolean currentState = LOW;//stroage for current button state
boolean lastState = LOW;//storage for last button state



void setup() {
  Serial.begin(9600);
}


void loop() {
  currentState = digitalRead(buttonPin);
  if (currentState == HIGH && lastState == LOW) { //if button has just been pressed
    delay(10); //Button debounce
    int potValue2 = analogRead(A1) / 342;
    int potValue1 = analogRead(A0) / 8;
    if (potValue2 == 0) {
      Serial.println(potValue1);
    }
    if (potValue2 == 1) {
      Serial.print("\t");
      Serial.println(potValue1);
    }
    if (potValue2 == 2) {
      Serial.print("\t");
      Serial.print("\t");
      Serial.println(potValue1);
    }
  }
  lastState = currentState;


}

Managed to make that work with your advice. In itself it seems like a pointless objective but it was an example to help me modify another piece of code I'm working on...

good!

you could use some else between those if to make your program go a bit faster.

Also as in all cases you print the value, Serial.println(potValue1); could be outside all the if - save some program memory

As said above printing the tabs could just be a for loop then the whole things becomes

for (byte i = 0; i< potValue2; i++) Serial.write('\t'); // write for 1 char is faster than calling print
Serial.println(potValue1);

using also meaningful variable names and constants for your pins would help readability

const byte buttonPin = 2;
const byte columnSelectionPin = A1;
const byte sensorPin = A0;
byte currentState = LOW;//stroage for current button state. LOW is not a boolean.
byte lastState = LOW;//storage for last button state

void setup() {
  Serial.begin(115200); // no need to go slow...
  pinMode(buttonPin, INPUT_PULLUP); // no need for an external pull down resistor pin -- button -- GND
}

void loop() {
  currentState = digitalRead(buttonPin);
  if (currentState == LOW && lastState == HIGH) { //if button has just been pressed
    delay(15); //poor man's button debounce
    int columnSelection = analogRead(columnSelectionPin) / 342; // 342 could be define as a  constant to avoid magic numbers in the code
    int sensorValue = analogRead(sensorPin) / 8;  // 8 could be define as a  constant to avoid magic numbers in the code
    for (byte i = 0; i< columnSelection; i++) Serial.write('\t'); // write for 1 char is faster than calling print
    Serial.println(sensorValue);
  }
  lastState = currentState;
}

Sorry for the late reply J-M-L...

I will try to work in your suggestions.
Your not the first to mention making meaningful variable names! :slight_smile:
Thanks for your time on this!!!