NS.One dan Keypad

Berikut adalah sebuah program contoh untuk operasional NS.One dengan keypad 3×4, dengan rujukan pin pada keypad, sbb:

KeypadPin 1 – 7 pada keypad dihubungkan dengan pin 24 (PA0) – 30 (PA6).

Setiap key yang ditekan akan dikirim ke Serial Monitor.

const int numRows = 4;       
const int numCols = 3;       
const int debounceTime = 20; 

const char keymap[numRows][numCols] = {
  { '1', '2', '3'  } ,
  { '4', '5', '6'  } ,
  { '7', '8', '9'  } ,
  { '*', '0', '#'  }
};

const int rowPins[numRows] = { 25, 30, 29, 27 }; 
const int colPins[numCols] = { 26, 24, 28 };   

void setup()
{
  Serial.begin(9600);
  for (int row = 0; row < numRows; row++)
  {
    pinMode(rowPins[row],INPUT);       
    digitalWrite(rowPins[row],HIGH);   // pull up enable
  }
  for (int column = 0; column < numCols; column++)
  {
    pinMode(colPins[column],OUTPUT);     
    digitalWrite(colPins[column],HIGH);  
  }
}

void loop()
{
  char key = getKey();
  if( key != 0) {       
    Serial.print("Got key ");
    Serial.println(key);
  }
}

// returns with the key pressed, or 0 if no key is pressed
char getKey()
{
  char key = 0;                                  // 0 indicates no key pressed

  for(int column = 0; column < numCols; column++)
  {
    digitalWrite(colPins[column],LOW);         
    for(int row = 0; row < numRows; row++)     
    {
      if(digitalRead(rowPins[row]) == LOW)     // Is a key pressed?
      {
        delay(debounceTime);                   
        while(digitalRead(rowPins[row]) == LOW);
        delay(debounceTime);
        key = keymap[row][column];             
      }
    }
    digitalWrite(colPins[column],HIGH);     
  }
  return key;  
}