Thread: need help storing input values into an array

  1. #1
    Registered User
    Join Date
    Jun 2015
    Posts
    40

    need help storing input values into an array

    Hi, I need some urgent help with arrays please, the purpose of the program is to check for a key pressed and store it into an empty array. And then print the entered sequence on the LCD after 'A' is pressed.
    the program compiles but nothing is being stored in the array or printed.
    Once this works, I would like to try and take that stored array and use it as an integer for calculations using the '.' and '-' as math functions(which I am also unclear about)
    I'm sure the solution is really simple fix.
    they are currently no compilation issues.

    Hope someone can help

    Code:
    #include <Keypad.h>
    #include <LiquidCrystal.h> // initialize the library with the numbers of the
    
    
    LiquidCrystal lcd(8, 9, 4, 5, 6, 7); 
    char lonarray[16]; 
    int i;
    
    
    const byte ROWS = 4; //four rows
    const byte COLS = 4; //four columns
    char hexaKeys[ROWS][COLS] = {
    {'1','2','3','A'},
    {'4','5','6','B'}, //define the cymbols on the buttons of the keypads
    {'7','8','9','C'},
    {'.','0','-','D'}
    };
    byte rowPins[ROWS] = {13, 12, 11, 10}; //connect to the row pinouts of the keypad
    byte colPins[COLS] = {33, 32, 31, 30}; //connect to the column pinouts of the keypad
    
    
    
    
    Keypad customKeypad = Keypad( makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS); //initialize an instance of class NewKeypad
    
    
    void setup()
    {
    lcd.begin(16, 2);
    Serial.begin(9600);
    }
    
    
    void loop()
    {
    char customKey = customKeypad.getKey();
    
    
    if (customKey == '0'){ lonarray[i] == '0'; i++;}
    if (customKey == '1'){ lonarray[i] == '1'; i++;}
    if (customKey == '2'){ lonarray[i] == '2'; i++;}
    if (customKey == '3'){ lonarray[i] == '3'; i++;}
    if (customKey == '4'){ lonarray[i] == '4'; i++;}
    if (customKey == '5'){ lonarray[i] == '5'; i++;}
    if (customKey == '6'){ lonarray[i] == '6'; i++;}
    if (customKey == '7'){ lonarray[i] == '7'; i++;}
    if (customKey == '8'){ lonarray[i] == '8'; i++;}
    if (customKey == '9'){ lonarray[i] == '9'; i++;}
    if (customKey == '.'){ lonarray[i] == '.'; i++;}
    if (customKey == '-'){ lonarray[i] == '-'; i++;}
    if (customKey == 'A')
      {
        //lcd.clear;  
        lcd.print("nmbrs prsd were:");
        lcd.setCursor(0,1);
        lcd.print(lonarray);
        
      }
    delay(250);
    Serial.print(customKey);
    }
    Last edited by DA93; 02-09-2016 at 04:10 PM.

  2. #2
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Code:
    if (customKey == '0'){ lonarray[0] == '0'; i++;}
    if (customKey == '1'){ lonarray[i] == '1'; i++;}
    if (customKey == '2'){ lonarray[1] == '2'; i++;}
    if (customKey == '3'){ lonarray[i] == '3'; i++;}
    if (customKey == '4'){ lonarray[i] == '4'; i++;}
    if (customKey == '5'){ lonarray[i] == '5'; i++;}
    if (customKey == '6'){ lonarray[i] == '6'; i++;}
    if (customKey == '7'){ lonarray[i] == '7'; i++;}
    if (customKey == '8'){ lonarray[i] == '8'; i++;}
    if (customKey == '9'){ lonarray[i] == '9'; i++;}
    if (customKey == '.'){ lonarray[i] == '.'; i++;}
    if (customKey == '-'){ lonarray[i] == '-'; i++;}
    You probably want those to be assignments (single '=').

  3. #3
    Registered User
    Join Date
    Jun 2015
    Posts
    40
    Thanks, I knew it was something simple, is there an easier more sound way of turning an array into an integer without using the "atoi" function? I would like it perform some sort of mathematics with the entered keys.
    I should imagine if its possible, it would be tricky due to a potential character being a '-' or a '.'.

    Thanks again.

  4. #4
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Are the expected inputs simple integers, or real numbers? This will determine the reasonable options to consider.

    If you are able to use library functions (if they are supported in your environment, and you have enough space to do so), this would probably be best. "strtol", "strtod", and "sscanf" are some good options.

  5. #5
    Registered User
    Join Date
    Jun 2015
    Posts
    40
    Code:
    if (customKey == 'B')
      {
        int x;
        float y;
        lcd.clear();  
        lcd.print("nmbrs prsd were:");
        lcd.setCursor(0,1);
        sscanf(lonarray, "%d", &x);
         y=(2*x);
        lcd.print(y);
        
      }

    when I press B the array successfully multiplies by two providing the decimal point isn't pressed.

    E.G if I entered -2*2 it would display -4.0
    but if I entered -2.7*2 it would still display -4.0, and not -5.4 ignoring everything after the decimal place.
    I tried floats but the only difference that made was extra decimal places after the point.
    Edit: changing the sscanf to %f instead of %d it doesn't print digits at all, just "OVF"
    Last edited by DA93; 02-09-2016 at 05:25 PM.

  6. #6
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    I can tell that you're working on an arduino chip. That means you have some limitations.

    One idea to work with decimal points without a special type would be to read values like this: sscanf (lonarray, "%d.%d", &x, &y);

    Then you can use a sliding scale (the sign variable is to handle negative values properly):

    sign = x < 0;
    x *= -1;
    x = x * 10 + y;
    if (sign) x += -1;

    y = 2 * x;

    You would have to take care to display the answer correctly though. For example if you had access to printf it would work like this:
    printf ("%d.%d", y / 10, y % 10);
    Negatives are slightly different:
    if (sign) y *= -1;
    printf ("%c%d.%d", sign? '-' : ' ', y/10, y%10);

    I hope you can figure it out from here.

  7. #7
    Registered User
    Join Date
    Jun 2015
    Posts
    40
    Hi, thanks for the advice, I'll give it a go, could you please explain why the function "atof" appears to work fine, I had a look around and the only disadvantage I could find was to do with strange problems occurring if certain characters were included.
    If this is indeed the only limitation, atof should be sufficient as only the specified characters from the if statements will ever be included in the array to begin with.
    I do intend on performing trigonometric functions with the variables, but I assume that won't be an issue.

    I know I have to use floats for decimal numbers but if the answer is a whole number, e.g. 1, the answer still prints 1.00, not that its a problem, but would be nice if could save space, bearing this in mind, the answerers that i have acquired so far have indeed been correct, however they only seem to display to 2 decimal places which is insufficient(I have a feeling though, if I increase the decimal places, whole numbers will print lik "1.00000000" etc.

    Anyway back to your post, how would the program respond (using both your method and atof) if for example, a "-" is pressed at any point other to the beginning, or the "." is detected more than once in the array?

    And to answer your printing query: printf could just be replaced by Serial.print and as for lcd.print i think its just a case of quotes for text then a comma then the variable, but to be honest i think its clearer just to have two lcd.print statements one for the text and one for the variable.

    Thanks again

  8. #8
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    know I have to use floats for decimal numbers ...
    So the arduino does have floating point numbers, cool.

    Anyway back to your post, how would the program respond (using both your method and atof) if for example, a "-" is pressed at any point other to the beginning, or the "." is detected more than once in the array?
    The biggest problem with the atof() function is that if the string is malformed - as you say for example, it could have two hyphens like "-2.-3" - the function will stop and return something. This erroneous value is indistinguishable from an actual result. So if you get 0 from atof() it is impossible to know if the good input was 0, or if the string was just garbage. If you stuck with atof(), the sane thing to do is to write some code that manually checks the string, and if it is good, you then pass it to atof(). But a lot of programmers, myself included, think that if you go this far to use atof() properly then the function really isn't helping you very much.

    My method would work better only because the sscanf() would return 2 if the input was right, and something smaller if it wasn't. But what I would actually prefer is that you use strtod(), which I found that the arduino supports with some light research. The second argument will allow you to determine where in the string the conversion stopped, so that you can decide if the input is good.

    The page that I linked is also the same one that informed me that %f conversions are not supported on the arduino.
    Last edited by whiteflags; 02-10-2016 at 06:10 AM.

  9. #9
    Registered User
    Join Date
    Jun 2015
    Posts
    40
    Something like this?
    if something is wrong is just displays "0.00..."
    is that correct?
    on another note, do you know why by default without the 5 for decimal places, it is by default 2 decimal places and always ends in "00"
    for example if I do -1.5x2 (without the 5 in lcd print) it returns "-3.00"
    if I do -1.028*2(with the 5 dp's) it returns -2.056"00"
    it would make more sense if it just displayed -2.056, only go to 5 decimal places as long as the value continues to be not 0.

    Code:
    if (customKey == 'B')  {
        float x;
        float y;
        lcd.clear();  
        lcd.print("nmbrs prsd were:");
        lcd.setCursor(0,1);
        x=strtod(lonarray, NULL);
         y=(x*-2);
        lcd.print(y,5); //5 decimal places
        Serial.print(y);
      }

  10. #10
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Something like this?
    if something is wrong is just displays "0.00..."
    is that correct?
    No, calling strtod with a NULL is not any better than atof(). You should be doing something more like this:
    Code:
    #include <errno.h>
    char *end;
    errno = 0;
    x = strtod(lonarray, &end);
    if (errno || end == lonarray || *end != '\0') {
       lcd.print("Error");
       /* reset the input loop, abort, whatever's appropriate */
    }
    y = -2 * x;
    What you do with y and x mathematically changes between examples so I hope I got it right.
    on another note, do you know why by default without the 5 for decimal places, it is by default 2 decimal places and always ends in "00"
    for example if I do -1.5x2 (without the 5 in lcd print) it returns "-3.00"
    if I do -1.028*2(with the 5 dp's) it returns -2.056"00"
    it would make more sense if it just displayed -2.056, only go to 5 decimal places as long as the value continues to be not 0.
    I don't think you can print y directly. At least according to reference, you should not; float is not among the supported types for data. You should use code to convert y to a separate string variable. There you will have an opportunity to format your answer to N decimal places.

  11. #11
    Registered User
    Join Date
    Jun 2015
    Posts
    40
    hi, is the following what is intended:

    if I enter "9.8.44"
    when I multiply by two it prints""error-19.6"
    another example:
    "-3.8-5"
    prints "Error 7.60"

  12. #12
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    It is roughly what is intended. You are responsible for getting the code to work exactly how you want.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Storing input values into Array
    By nelly26 in forum C Programming
    Replies: 1
    Last Post: 11-30-2011, 09:42 AM
  2. Storing values in an array
    By kpop in forum C Programming
    Replies: 3
    Last Post: 04-12-2011, 01:46 PM
  3. Storing values into a dynamic array
    By porsche911nfs in forum C++ Programming
    Replies: 5
    Last Post: 04-24-2009, 09:08 AM
  4. Storing input values while iterating
    By russel1013 in forum C Programming
    Replies: 11
    Last Post: 07-29-2008, 08:32 AM
  5. Storing values from Edit Box into an array
    By E_I_S in forum C++ Programming
    Replies: 10
    Last Post: 06-05-2008, 06:24 AM

Tags for this Thread