Row of variables

Hey, I want to program a row of variables that can be set to a value in a for loop.
The meaning of this code is nonsense but it shows what I mean.

``````variable[] = {variable0, variable1, variable2};

void setup() {
}
void loop() {
for(int X=0;X<3;X++){
variable[X] = X*3;}

}
``````

Except from the variable0, variable1, variable2 part and the fact there is no type defenition for variable it perfectly fine code…

``````int variable[] = {0, 0, 0};

void setup() {
}

void loop() {
for(int X=0; X < 3; X++){
variable[X] = X * 3;
}

}
``````

okay thx, so in the program it knows that variable[0] = 0 and variable[1] = 3 and variable[2] = 6?

You want an array of integers or an array of pointers to integers?

kayakkie87: okay thx, so in the program it knows that variable[0] = 0 and variable[1] = 3 and variable[2] = 6?

After you run the for loop once, yes.

But do you want them as a constant (never change them)? Then this is a bit of unnecessary to code... You can just multiply x by 3 every time you use it. That's a simple thing to do for an Arduino. So maybe you have to explain more what you want to do. It's called XY-problem ;)

I am working on a MIDI drumpad with potentiometers, I have 5 potentiometers with all the same code for the signal to send to the pc only some different variables so I was wondering if I could put it in a for loop and save up some space.

this is ,my whole code, it is a matrix 4x4 (3 analog inputs and 1 digital input and 4 digital outputs), I set the rows HIGH one by one and check the coloms one by one, it worked but it kept sending data and that was not the purpose so I inserted the lines wich contain buttonstateold but then I didn’t light up the led or send data, How to fix the simple button state problem?
The latchpin, datapin and clockpin are for the 74hc595 which drives the led (that works so no mistakes there.
The Serial.write commands are for sending MIDI data

``````int row[] = {5, 7, 2, 3};         //output
int colom[] = {A5, A7, A6, 4};    //input
int latchPin = 8;
int clockPin = 6;
int dataPin = 9;
int note[] = {60, 61, 62, 63, 64, 65, 67, 68, 69, 70, 71, 72, 73, 74, 75};            //note
int number[] = {23, 39, 71, 135, 27, 43, 75, 139, 29, 45, 77, 141, 30, 46, 78, 142};
//led
int buttonstate[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
int buttonstateold[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};

void setup() {
Serial.begin(9600);
for(int x=0;x<4;x++){
pinMode(row[x], OUTPUT);
pinMode(colom[x], INPUT);}
pinMode(latchPin, OUTPUT);
pinMode(clockPin, OUTPUT);
pinMode(dataPin, OUTPUT);}

void loop() {
for(int i=0;i<4;i++){
int val = row[i];
digitalWrite(val, HIGH);
for(int z=0;z<3;z++){
int var = i*4+z;
if(buttonstate[var] > 0 && buttonstate[var] == 0){
Serial.write(144);
Serial.write(note[var]);
Serial.write(127);
digitalWrite(latchPin, LOW);
shiftOut(dataPin, clockPin, LSBFIRST, number[var]);
digitalWrite(latchPin, HIGH);
buttonstateold[var] = buttonstate[var];
delay(1);}
if(buttonstate[var] == 0 && buttonstateold[var] > 0){buttonstateold[var] = 0;}}
int var = i*4+3;
if(buttonstate[var] == HIGH && buttonstateold[var] == LOW){
Serial.write(144);
Serial.write(note[var]);
Serial.write(127);
digitalWrite(latchPin, LOW);
shiftOut(dataPin, clockPin, LSBFIRST, number[var]);
digitalWrite(latchPin, HIGH);
buttonstateold[var] = buttonstate[var];
delay(1);}}
digitalWrite(rij[i], LOW);
delay(1);}}}
``````

thx

I can spot multiple errors,points of interest and questions.

First of all, how is everything wired? Matrixing multiple pot's isn't really possible I think (without drawing...). You need to multiplex them.

Second, the code make up is a big mess. Press ctrl+t for fun and look how much more clear the code becomes...

``````digitalWrite(rij[i], LOW);
``````

If you sneak in some Dutch it's not going to work ;)

colom, you mean column? But no matter what, if you call it lolom it's not going to work....

``````if(digitalRead(lolom[3]) == HIGH ){
``````

And remove the stupid delay()'s.

``````if(buttonstate[var] > 0 && buttonstate[var] == 0){
``````

Wait, what? It need to be bigger then 0 AND 0 at the same time?

``````int number[] = {23, 39, 71, 135, 27, 43, 75, 139, 29, 45, 77, 141, 30, 46, 78, 142};
``````

Indeed, those are numbers... But what are they for? You really need to use better variable names. row => rowPins colom => columnPins note => notes (plural to remind you it's an array) buttonstate => buttonState buttonstateold => buttonStateOld For a start.

And now we're on the subject of variables, you say you want to "save up some space". Start by using the proper variable type and make the ones you don't what to change in the program to const.

``````//Pins
const byte RowPins[] = {5, 7, 2, 3};         //output
const byte ColumnPins[] = {A5, A7, A6, 4};    //input
const byte LatchPin = 8;
const byte ClockPin = 6;
const byte DataPin = 9;

//Lookup tables
const byte Notes[] = {60, 61, 62, 63, 64, 65, 67, 68, 69, 70, 71, 72, 73, 74, 75};  //Notes
const byte Numbers[] = {23, 39, 71, 135, 27, 43, 75, 139, 29, 45, 77, 141, 30, 46, 78, 142};
``````
``````Serial.write(144);
Serial.write(127);
``````

Not clear what that does, where is the command? Or it it's a clear command, just make it a const byte with it.

``````const byte CommandX = 144;

Serial.write(CommandX);
``````

Is pretty self explaining

And you try to cram everything in the loop() although it are saeperate tasks. Why not split everything and make it more clear. That way every piece can be simple etc

``````void loop(){
playNotes();
setLeds();
}
``````

For simplicity you can even make a function to update the leds etc

``````void updateLeds(byte data){
digitalWrite(latchPin, LOW);
shiftOut(dataPin, clockPin, LSBFIRST, data);
digitalWrite(latchPin, HIGH);
}
``````

A simple function, yes. But this way every piece of the puzzle is simple ;)