Help with a 3*4 Keypad

Basically I want to store a 4 digit number or smaller.

/* Keypadtest.pde
*

  • Demonstrate the simplest use of the keypad library.
  • The first step is to connect your keypad to the
  • Arduino using the pin numbers listed below in
  • rowPins[] and colPins[]. If you want to use different
  • pins then you can change the numbers below to
  • match your setup.
  • Note: Make sure to use pullup resistors on each of
  • the rowPins.
  • Note: Pins are use multiple times in this example.
    */
    #include <Keypad.h>
    #include <LiquidCrystal.h>

const byte ROWS = 4; // Four rows
const byte COLS = 3; // Three columns
// Define the Keymap
char keys[ROWS][COLS] = {
{'1','2','3'},
{'4','5','6'},
{'7','8','9'},
{'*','0','#'}
};
// Connect keypad ROW0, ROW1, ROW2 and ROW3 to these Arduino pins.
byte rowPins[ROWS] = { 9, 8, 7, 6 };// Connect keypad COL0, COL1 and COL2 to these Arduino pins.
byte colPins[COLS] = { 12, 11, 10 };

// Create the Keypad
Keypad kpd = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );

// LiquidCrystal display with:
// rs on pin 12
// rw on pin 11
// enable on pin 10
// d4, d5, d6, d7 on pins 5, 4, 3, 2
LiquidCrystal lcd(12, 11, 10, 5, 4, 3, 2);

void setup()
{
Serial.begin(9600);
}

char charGain[4];
int charindex =0;

void loop()
{
keyArray();
printArray();
}

void keyArray(){

for(charindex == 0;charindex < 4; charindex++){

char key = kpd.getKey();
if (key != NO_KEY){ //if there is any key being put
if (key = '#'){
for(charindex;charindex < 4;charindex++){ //if the user didn't enter 4 digits float from the keypad, then other char in the array is set to be 0
charGain[charindex]==0;
charindex==0;
}
}
else if (key = '*'){

else{ //if the user enter 0-9, then store it into array and display it
charGain[charindex]=key;
charindex++;
lcd.print(charGain[charindex]);
}
}
}

void printArray(){ //would like to show every char in the array after entering from keypad

for (charindex == 0;charindex < 4;charindex++){
//Serial.println("array [" ++ charindex ++ "]"++ charGain[charindex]);
lcd.print("array [");
delay (1000);
lcd.print(charindex);
delay (1000);
lcd.print("] ");
delay (1000);
lcd.print(charGain[charindex]);
delay (1000);

}

}

I looked at cutefatfat 's code to figure this much out, but I need to know how the number is ultimately stored, so I can multiply it by a different number and use it later.

Also I wanted the * key to cancel out all the digits of the code. If whoever helps me with this could explain it to me, so that I understand it and could do it on my own that would help a lot.

Oh I know the pins don't work out, but I may end up using an Arduino Mega. If someone could help me figure out how to fit 4 7-segment numbers, an LCD, and the key pad on just the digital though that would be great. I know I could get either the 7-segment or LCD to use the Arduino's serial port, but I think I'd still be a couple pins short.

Similar thread here: http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1276823678/3#3
hope it helps.

Also:

 if (key = '#'){  
      for(charindex;charindex < 4;charindex++){  //if the user didn't enter 4 digits float from the keypad, then other char in the array is set to be 0
      charGain[charindex][glow]==[/glow]0;
      charindex[glow]==[/glow]0;
      }

That is not assignment operator, but a comparison operator (returning a boolean I would think). The compiler might not complain about this, but it would not have the intended effect (IE charindex is never set to zero).

Oh, and the charindex is reset inside the for loop...which would never end (well, if the above had worked as intended that is.. kind of like two wrongs making almost right :p)

 if (key = '#'){  
      for(charindex=0; charindex < 4; charindex++){  //if the user didn't enter 4 digits float from the keypad, then other char in the array is set to be 0
        charGain[charindex]=0;
      }
      charindex=0;
/*  Keypadtest.pde
 *
 *  Demonstrate the simplest use of the  keypad library.
 *
 *  The first step is to connect your keypad to the
 *  Arduino  using the pin numbers listed below in
 *  rowPins[] and colPins[]. If you want to use different
 *  pins then  you  can  change  the  numbers below to
 *  match your setup.
 *
 *  Note: Make sure to use pullup resistors on each of
 *  the rowPins.
 *  
 *  Note: Pins are use multiple times in this example.
 */
#include <Keypad.h>
#include <LiquidCrystal.h>

const byte ROWS = 4; // Four rows
const byte COLS = 3; // Three columns
// Define the Keymap
char keys[ROWS][COLS] = {
  {'1','2','3'},
  {'4','5','6'},
  {'7','8','9'},
  {'*','0','#'}
};
// Connect keypad ROW0, ROW1, ROW2 and ROW3 to these Arduino pins.
byte rowPins[ROWS] = { 9, 8, 7, 6 };// Connect keypad COL0, COL1 and COL2 to these Arduino pins.
byte colPins[COLS] = { 12, 11, 10 }; 

// Create the Keypad
Keypad kpd = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );

// LiquidCrystal display with:
// rs on pin 12
// rw on pin 11
// enable on pin 10
// d4, d5, d6, d7 on pins 5, 4, 3, 2
LiquidCrystal lcd(12, 11, 10, 5, 4, 3, 2);

void setup()
{
  Serial.begin(9600);
}

char charGain[4];
int charindex =0;

//void enterGain(){  //block this sentence
void loop()
{
  keyArray();
  printArray();
}
  
void keyArray(){

for(charindex == 0;charindex < 4; charindex++){

  char key = kpd.getKey();
  if (key != NO_KEY){  //if there is any key being put
   if (key = '#'){  
        for(charindex;charindex < 4;charindex++){  //if the user didn't enter 4 digits float from the keypad, then other char in the array is set to be 0
        charGain[charindex]=0;
        charindex=0;
        }
          charindex==0;
    }
     else if (key = '*'){
            int charindex =0;
            }
       else{                        //if the user enter 0-9, then store it into array and display it
      charGain[charindex]=key;
      charindex++;
      lcd.print(charGain[charindex]);
    }
  }
}

void printArray(){                //would like to show every char in the array after entering from keypad

  for (charindex == 0;charindex < 4;charindex++){
    //lcd.print("array [" ++ charindex ++ "]"++ charGain[charindex]);
    lcd.print("array [");
    delay (1000);
    lcd.print(charindex);
    delay (1000);
    lcd.print("] ");
    delay (1000);
    lcd.print(charGain[charindex]);
    delay (1000);

  }

}

So would that work? I just keep getting this error:
"In function 'void keyArray()':
error: a function-definition is not allowed here before '{' token"

Thank you for your help thus far.

What is the problem with this code?

int weightkg = int(char charGain[4]) * int(0.45359237);

I haven't played too much with this, but you need to look into the atoi() function: http://www.cplusplus.com/reference/clibrary/cstdlib/atoi/
I think the string needs to be 0-terminated for atoi() to work.

You cannot take a string containing numbers and calculate with those directly, as all characters are encoded (usually) in the ASCII code. For example the character "A" is 65 decimal, the character "1" is 49 decimal, etc.

Also:-
int(0.45359237)

will equate to zero so even if you get the string converted into a number the result will be zero.

I was wondering about that. How would I get around that? Divide by sum number? probably 2.

Actually I figured out my own question. I'll just multiply the decimal by some factor of 10 before multiplying it to the number I wanted to multiply it by and then divide by that same factor of 10.

What I really need now is to figure out how to display this number on a 4 digit, 7-segment display.

I redid the math problem and it still isn't working.

  i = atoi (charGain[charindex]);
  weightg = i * int(454);
  weightkg = weightg * int(1000);

error: invalid conversion from 'char' to 'const char*'

Anyone?

I'm sorry I can't help you with your problem (I didn't really understand the solution you posted about multiply by 10, then another number and then divide by 10 again either).

Just one or two things:

  i = atoi (charGain[charindex]);
  weightg = i * 454;
  weightkg = weightg [glow]/[/glow] 1000;

"454" and "1000" are already integers. Integer Constants - Arduino Reference

Also I'm uncertain if you need to supply the "charindex" to the atoi() function. In case it is something else than 0 I suspect it will return a wrong number.

Well it's a conversion factor. I can't multiply the input by a number less than one, (.45434534) or w/e, so instead I multiplied it by 454 and then divided.

Say if the input was 120 instead of dividing by 2 and getting 60. I'd multiply 120 by 454 then divide by 1,000 and get a more accurate conversion.

I figured that they already were integers, but I figured there would be no harm in being redundant.

The error is only with the first line I believe. I just need to convert the user's input from pounds to kilograms.

Thanks for your help though. I think after I figure out that one line it should go more smoothly from there on.

i = atoi (charGain[charindex]);
weightg = i * int(454);
weightkg = weightg * int(1000);

error: invalid conversion from 'char' to 'const char*'

Here, the compiler is telling you what is wrong.
"atoi" (ASCII to integer) expects a pointer to a string, or "const char *" to convert to an integer, but you're giving it just a single character.

What would I change to get that to work though?

Sorry I'm not too knowledgeable about these things. I'm just trying to take the user input and then multiply it by other numbers and use it later on.

You've stored the user input in an array, charGain. The atoi function wants an array as the input.

i = atoi(charGain);

You must make sure, when you are storing data, that charGain is always NULL terminated, in order for atoi to work properly.

Thank you for your help!

Does that mean it has to end in a zero?

So could I multiply the input times 10 within the atoi function and then divide out the 10 latter?

Never mind that won't work.

So will their be a problem if the number starts with 0?

How can I fix it if the user enters a number not ending in zero?