Go Down

Topic: Logical problem involving arrays (Read 643 times) previous topic - next topic

Daniel Formosa

Dear all, with a keypad and LCD interfaced to the arduino mega I have managed to display what the user presses on the LCD.  My problem is here; if for example the user writes 0043 - this value is saved into an array;

How can I save this value as a whole in a variable ??

This is the code used:
Code: [Select]
//Display refernce no on lcd
  for(int x =0; x<4;x++)      //loop to save amount entered (4 digit no)
    {
   
      do
      {
        reference_no = customKeypad.getKey();        //get amount from keypad
        Serial.println(reference_no);                //serial print for testing
     
     
        if(reference_no == 0)                        //amount is always 0 when waiting for key to get pressed
         {
           flag = true;         
         }
       
         else if((reference_no == '0')||(reference_no == '1') ||(reference_no == '2')||(reference_no == '3')
                 || (reference_no == '4') || (reference_no == '5') || (reference_no == '6') || (reference_no == '7')
                 || (reference_no == '8') || (reference_no == '9'))                        //for pressing any digit no
         {
           reference_no = reference_no - 48;        //since from keypad, the characters are displayed as ASCII
           lcd.setCursor(x,0);          //move cursor to desplay nos pressed
           lcd.print(reference_no);
           tempreferarray[x] = reference_no;         //saving in array
           reference_no = reference_no - reference_no;  //so that reference_no is equal to the value before when loop is begun again
           flag = false;
         }
       }
      while(flag == true);
    }
    lcd.setCursor(4,0);       
    lcd.print("ref no");
    delay(2000);
   
    Serial.println(tempreferarray[3]);  //display last no in array


tnx

AWOL

Convert the number to decimal, and store it in an "int".
Code: [Select]
if((reference_no == '0')||(reference_no == '1') ||(reference_no == '2')||(reference_no == '3')
                 || (reference_no == '4') || (reference_no == '5') || (reference_no == '6') || (reference_no == '7')
                 || (reference_no == '8') || (reference_no == '9'))

aka
Code: [Select]
if((reference_no >= '0') && (reference_no <= '9'))
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Daniel Formosa

That still leaves the number stored in an array, since I need to input a 4 digit no.

I want the 4 digit no to be saved as a integer as a whole.

What this program does is store the 4 digit no in an array[4].
Now I need to store the whole values in the array to an integer

Tnx

AWOL

Still not sure what you're saying - an array is a variable.
Are you saying you need a two dimensional array?

If you store it as an int, you can infer any leading zeroes, and any trailing or infix ones will be explicit.
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Daniel Formosa

No.

The user enters a 4 digit number.
The digits are saved in an array[4]

For example before the usre enters the nos pressed by the keypad, the array is like this :
arr[] = ,_ ,_, _, _
when he presses 1, the array is now like this :
arr[] = 1 ,_, _, _
pressing 2,...:
arr[] = 1,2,_,_
pressing 3
arr[] = 1,2,3,_
pressing 4:
arr[] = 1,2,3,4

Now I want the values stored in the array; i.e. arr[0], arr[1],...arr[3] to be stored in a single integer. 
Meaning i will have an integer having an amount of 1234.

That is my problem
Tnx

Go Up