Pages: [1]   Go Down
Author Topic: Logical problem involving arrays  (Read 596 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Full Member
***
Karma: 1
Posts: 110
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
//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
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 303
Posts: 26353
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Convert the number to decimal, and store it in an "int".
Code:
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:
if((reference_no >= '0') && (reference_no <= '9'))
Logged

"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.

0
Offline Offline
Full Member
***
Karma: 1
Posts: 110
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 303
Posts: 26353
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

"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.

0
Offline Offline
Full Member
***
Karma: 1
Posts: 110
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 303
Posts: 26353
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Multiply the thousands digit by one thousand, add in the hundreds digit multiplied by one hundred...
Logged

"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.

0
Offline Offline
Full Member
***
Karma: 1
Posts: 110
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Damn lol
Tnx
Logged

Pages: [1]   Go Up
Jump to: