storing integers in arrays

hi,

I'm inputting a series of 4 single integer from a keypad to compare with an existing array to disarm an alarm. Is there anyway to store integers in an array.-- if the 4 digits in existing array are declared then they are stored and printed as integers. when I write into array with 4 keystrokes they are returned as ascii values,which makes it complicated to compare the 2 codes, any ideas? would using a string be better?

If you post your code we have a better chance of helping you fix it. Please read the forum guidelines before posting code.

The itoa() function will convert a number from it's ASCII representation to an integer.

A site search of 'keypad combination' may prove fruitful.

Just store the alarm code as a string and read the keystrokes into a string and then just compare the strings.

Hi and thanks for replies. I had problems using itoa() function, not finding anything in the reference.
I have got it working by using a string array compared to char array, but it has led to another problem, although both return the same digits in a if loop they dont appear to be recognised as the same. I have commented where the problem appears with //++++++++
Also marked the same is an end bracket of an if loop which seems to be ignored.
Hope ive posted code properly, any help greatfully received.

 [code]
#include <Key.h>
#include <Keypad.h>


//  Keypad decarations:
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] = {5, 4, 3, 2}; //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 );

// end of keypad decl.

byte mycode[] = {1, 9, 6, 6};
char codein[] = {0, 0, 0, 0};
char alarmset = 'n';
unsigned long timer1;
unsigned long timer2;
unsigned long timer3;
int temp[] = {2, 5, 8, 0};
byte flag = 0;
byte x = 0;
byte y = 0;
int timeallowed = 60000;
char codegood = 'n';


void setup()
{
  // put your setup code here, to run once:
  Serial.begin(9600);
}

void loop()
{
startlabel:
  timer1 = millis();             //  set timer 1
  if (alarmset = 'y'  ) {                          //  alarm not live goto to keypad for input (2580)
      flag = 0;
      for (y = 0; y < 4; y = y + 1){codein[y]=' ';} // empt codein[]
      byte key = keypad.getKey();
      if (key)          {                      //  check for 1st key input                         
          codein[0] = key;              
          flag = 1;                         
          codegood ='n';
          x = 1;                // 1st digit already entered
label:
         if( x < 4 )     
            {  key = keypad.getKey();
            if (key)             // key is empty, skips to   ========   
                {  codein[x]= key; 
                x = x+1;
// Serial.print("2nd x =  ");Serial.println(x);                            //--
// check time taken etc here         
                }      // needs to go back to get key                              
           }                // skipped to here      ======= 
//++++++++++this bracket should return to 'if( x < 4)' but doesnt        
            if (x <4) goto label;//++++  making this necessary
                                      
Serial.print("3rd x =  ");Serial.println(x);    //  gets here( x=1)
// is codein correct? compare codein with mycode
            codegood = 'n';
    for( y = 0; y < 4 ;  y = y + 1) //   only needed for debugging   
    { Serial.print(   "codein=  ");Serial.println(codein[y]);
    Serial.print( "goodcode =  ");Serial.println(mycode[y]); }        
            for( y = 0; y < 4 ;  y = y + 1)
                { if(codein[y]== mycode[y])
                   {
//+++++++++++++  why does this skip if loop, although codein[] = mycode[] 
                   Serial.println("in compare for loop");
                   codegood = 'y';
                   Serial.print(codein[y]); Serial.println(mycode[y]); 
                   }
               }
            if (codegood == 'y' && y == 3)
                { 
 Serial.print(codegood);
 Serial.println( "   Code agreed");
                  alarmset = 'n';
                  goto startlabel;                    // inputting finshed either timed out or alarm is set
                }
 Serial.println("code not good");
           
                    
//              delay(10000);
           flag = 3 ;         
             
          }     
       }      // back to check if alarm set   
              // if past here alarm is set check for sensor activated etc...

}                                              //end void loop

[/code]

Don't use goto; it will result in spaghetti code and eventually in code that is impossible to follow.

You can use a while-loop to read the keypad. It's not the perfect solution but better than what you currently have.

To give you the idea

byte key;
x=0;
while (x<4)
{
  key = keypad.getKey();
  if(key)
  {
    codein[x++] = key;
  }
}

Further I advise not to use single letter global variables; I doubt that x needs to be global but its function is a counter so give it a name that reflect that (e.g. inputCount).

You don't have a 'string array', you have an array of characters. c-strings have a terminating NUL character. That does not pose a problem in your code though.

      for ( y = 0; y < 4 ;  y = y + 1)

{
        if (codein[y] == mycode[y])
        {
          //+++++++++++++  why does this skip if loop, although codein[] = mycode[]
          Serial.println("in compare for loop");
          codegood = 'y';
          Serial.print(codein[y]); Serial.println(mycode[y]);
        }
      }

The result of this will be that codegood will be 'y' if one of the characters match. You should reverse the logic by initially setting codegood to 'y' and setting it to 'n' if there is no match.

Below the idea

codegood = 'y';
for ( y = 0; y < 4 ;  y = y + 1)
{
  Serial.println("in compare for loop");
  Serial.print(codein[y]); Serial.println(mycode[y]);
  if (codein[y] != mycode[y])
  {
    codegood = 'n';
    Serial.print("Mismatch at position "); Serial.println(y); // for debugging
  }
}

Note:
snippets not tested nor compiled

As sterretje stated DO NOT use gotos. I've been coding for 35 years and I can't remember the last time I saw a goto in code. Trust me, it is bad practice. I would also suggest making your formatting and indentation consistent. While that sounds trite or trivial it makes it a lot easier to debug your code.

sterretje had some good advice. I just wanted to point out that when you are comparing the strings you can break out of the loop as soon as you find a mismatch. Alternatively you can use the memcmp function.

codegood = 'y';
for ( y = 0; y < 4 ;  y++)
{
  Serial.println("in compare for loop");
  Serial.print(codein[y]); Serial.println(mycode[y]);
  if (codein[y] != mycode[y])
  {
    codegood = 'n';
    Serial.print("Mismatch at position "); Serial.println(y); // for debugging
    break;
  }
}

Great, some really useful advice, which I will take on board and utilise, many thanks
In my defence at least 1 goto was only there because of the apparent failure of the end }
and my formatting got messed up in the debugging

Yes, i'm afraid its me again! Using while(x<4) instead of if(x<4) sorted the end } problem, but why would that be?
I can understand the disadvantages of multiple goto statements, but how do you get to the same place from several different points of the sketch?
Lastly changing the logic when comparing the 2 arrays looks tidier, it still does not work, it reads them as different even when they arnt, instead of always returning arrays the same even when there not.
As a Qbasic user some years ago i'm finding stepping up to C language a steep learning curve, at least so far - far less intuitive.

 [code]
#include <Key.h>
#include <Keypad.h>


//  Keypad decarations:
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] = {5, 4, 3, 2}; //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 );

// end of keypad decl.

byte mycode[] = {1, 9, 6, 6};
char codein[] = {0, 0, 0, 0};
char alarmset = 'n';
unsigned long timer1;
unsigned long timer2;
unsigned long timer3;
int temp[] = {2, 5, 8, 0};
byte flag = 0;
byte x = 0;
byte y = 0;
int timeallowed = 60000;
char codegood = 'n';


void setup()
{
  // put your setup code here, to run once:
  Serial.begin(9600);
}

void loop()
{
startlabel:
  timer1 = millis();             //  set timer 1
  if (alarmset = 'y'  )
      {                          //  alarm not live goto to keypad for input (2580)
      flag = 0;
      for (y = 0; y < 4; y = y + 1){codein[y]=' ';// empt codein[]
      } 
      byte key = keypad.getKey();
      if (key)                               //  check for 1st key input                         
          {
          codein[0] = key;              
          flag = 1;                         
          codegood ='n';
          x = 1;                // 1st digit already entered
          while( x < 4 )     
            { 
            key = keypad.getKey();
            if (key)             //  if key is empty, skips to   ========   
                { 
                codein[x]= key; 
                x = x+1;
// check time taken etc here         
                }      // needs to go back to get key between keystrokes                              
           }                //          skipped to here      ======= 

                                      
           Serial.print("3rd x =  ");Serial.println(x);    //  gets here( x=1)
// is codein correct? compare codein with mycode
            codegood = 'y';
            for( y = 0; y < 4 ;  y = y + 1) //   only needed for debugging   
               { 
               Serial.print(   "codein=  ");Serial.print(codein[y]);
               Serial.print( "   goodcode =  ");Serial.println(mycode[y]); 
          }        
           for( y = 0; y < 4 ;  y = y + 1)
               {
               if(codein[y]!= mycode[y])
                   {
                   Serial.println("in compare for loop");
                   codegood = 'n';
                   Serial.print(" codegood =" ); Serial.println(codegood);
                   Serial.print(codein[y]); Serial.println(mycode[y]); 
                   }
               }
               if (codegood == 'y' && y == 3)
               { 
                Serial.println(codegood);
                Serial.println( "   Code agreed");
                alarmset = 'n';
                  goto startlabel;                    // inputting finshed either timed out or alarm is set
                Serial.println("code not good");
               }
          }            //  back to check if new key being pressed
     }      // back to check if alarm set   
              // if past here alarm is set check for sensor activated etc...

}            //end void loop

[/code]

Using while(x<4) instead of if(x<4) sorted the end } problem, but why would that be?

while(x<4)will cause the code in the action block to be repeated until the condition is true

if(x<4)will cause the code in the action block to be run once if the condition is true. That is not to say that you can't use if to cause code to run until the condition is true but you must provide your own looping structure. Strangely enough, the loop() function is good at at