Rearranging an array in the arduino

hussain.omail

Feb 15, 2015
6
Joined
Feb 15, 2015
Messages
6
I am taking an array input called attempt[] from the user then according to the value of attempt I rearrange another array called user[] but the problem here is that I am able to do this only one time while I am suppose to be able to do that whenever I enter a value from the keypad

I am not sure if the problem is from the way I monitor the code using the serial or from the code itself

hope u can help me with that

thanx,







#include <Keypad.h>
const byte ROWS = 4; // four rows
const byte COLS = 3; // three columns

char keys[ROWS][COLS] = {
{'1','2','3'},
{'4','5','6'},
{'7','8','9'},
{'*','0','#'}
};

byte rowPins[ROWS] = {2, 3, 4, 5}; // connect to the row pinouts of the keypad
byte colPins[COLS] = {8, 7, 6}; // connect to the column pinouts of the keypad

Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
char attempt[5]={0,0,0,0,0};
int load1=2;
int load2=4;
int load3=7;
int load4=8;
int load5=12;
int user[5]={load1,load2,load3,load4,load5};


int i=0;
void setup() {
Serial.begin(9600);
}

void loop() {

char key = keypad.getKey();
if (key != NO_KEY) {
switch(key)
{
case '*': i=0;
break;
default:{ attempt=key;
if (attempt == '1'){
user= 2;
}
else if (attempt == '2'){
user= 4;
}

else if (attempt == '3'){
user= 7;
}
else if (attempt == '4'){
user= 8;
}
else if (attempt == '5'){
user= 12;
}
i++;}

}
}
Serial.println(user[0]);
Serial.println(user[1]);
Serial.println(user[2]);
Serial.println(user[3]);
Serial.println(user[4]);
}
 

FenTrac

May 26, 2015
1
Joined
May 26, 2015
Messages
1
It appears that when you are assigning values to the array "int user[5]", that you are not specifiying the position of the value in the array.
For example, should "if(attempt == 5) { user = 12;}" be something like "if(attempt == 5) {user[4] = 12;}" ?
 
Top