Add multiple digits to keypad entry

Hello,

I am having a difficult time understanding how to get the attached code to accept more digits than 1. The IF statements work like I would like except I cannot enter a number like 12, it will just do number 1. I believe I have to assign it to an array and then reference that array but I am missing the necessary knowledge to make this work. Any help that you can provide that would be greatly appreciated. I am using this keypad on my iPhone: http://viccarre.mx/ishield/documentation.html#using and the single digits work. It looks like “i” is what is sent when I hit the “Enter” button. How can I modify this code to accept digits from 1-4 in length and have the Enter button indicate that I am done entering that number and to perform the corresponding IF statement?

Thank you

Chad

#include <Adafruit_NeoPixel.h>
#include <SPI.h>
#include <boards.h>
#include <RBL_nRF8001.h>
#include “iShield.h”

#define PIN 10
byte byteRead;
iShield myiShield;
int cont = 0;

Adafruit_NeoPixel strip = Adafruit_NeoPixel(200, PIN, NEO_GRB + NEO_KHZ800);

//Grid Conversion
int a1 = 0;
int a2 = 1;
int a3 = 2;
int a4 = 3;
int a5 = 4;
int a6 = 5;
int a7 = 6;
int a8 = 7;
int a9 = 8;
int a10 = 9;
int a11 = 10;
int a12 = 11;
int a13 = 12;
int a14 = 13;
int a15 = 14;
int a16 = 15;
int a17 = 16;
int a18 = 17;
int b18 = 18;
int b17 = 19;
int b16 = 20;
int b15 = 21;
int b14 = 22;
int b13 = 23;
int b12 = 24;
int b11 = 25;
int b10 = 26;
int b9 = 27;
int b8 = 28;
int b7 = 29;
int b6 = 30;
int b5 = 31;
int b4 = 32;
int b3 = 33;
int b2 = 34;
int b1 = 35;
int c1 = 36;
int c2 = 37;
int c3 = 38;
int c4 = 39;
int c5 = 40;
int c6 = 41;
int c7 = 42;
int c8 = 43;
int c9 = 44;
int c10 = 45;
int c11 = 46;
int c12 = 47;
int c13 = 48;
int c14 = 49;
int c15 = 50;
int c16 = 51;
int c17 = 52;
int c18 = 53;
int d18 = 54;
int d17 = 55;
int d16 = 56;
int d15 = 57;
int d14 = 58;
int d13 = 59;
int d12 = 60;
int d11 = 61;
int d10 = 62;
int d9 = 63;
int d8 = 64;
int d7 = 65;
int d6 = 66;
int d5 = 67;
int d4 = 68;
int d3 = 69;
int d2 = 70;
int d1 = 71;
int e1 = 72;
int e2 = 73;
int e3 = 74;
int e4 = 75;
int e5 = 76;
int e6 = 77;
int e7 = 78;
int e8 = 79;
int e9 = 80;
int e10 = 81;
int e11 = 82;
int e12 = 83;
int e13 = 84;
int e14 = 85;
int e15 = 86;
int e16 = 87;
int e17 = 88;
int e18 = 89;
int f18 = 90;
int f17 = 91;
int f16 = 92;
int f15 = 93;
int f14 = 94;
int f13 = 95;
int f12 = 96;
int f11 = 97;
int f10 = 98;
int f9 = 99;
int f8 = 100;
int f7 = 101;
int f6 = 102;
int f5 = 103;
int f4 = 104;
int f3 = 105;
int f2 = 106;
int f1 = 107;
int g1 = 108;
int g2 = 109;
int g3 = 110;
int g4 = 111;
int g5 = 112;
int g6 = 113;
int g7 = 114;
int g8 = 115;
int g9 = 116;
int g10 = 117;
int g11 = 118;
int g12 = 119;
int g13 = 120;
int g14 = 121;
int g15 = 122;
int g16 = 123;
int g17 = 124;
int g18 = 125;
int h18 = 126;
int h17 = 127;
int h16 = 128;
int h15 = 129;
int h14 = 130;
int h13 = 131;
int h12 = 132;
int h11 = 133;
int h10 = 134;
int h9 = 135;
int h8 = 136;
int h7 = 137;
int h6 = 138;
int h5 = 139;
int h4 = 140;
int h3 = 141;
int h2 = 142;
int h1 = 143;
int i1 = 144;
int i2 = 145;
int i3 = 146;
int i4 = 147;
int i5 = 148;
int i6 = 149;
int i7 = 150;
int i8 = 151;
int i9 = 152;
int i10 = 153;
int i11 = 154;
int i12 = 155;
int i13 = 156;
int i14 = 157;
int i15 = 158;
int i16 = 159;
int i17 = 160;
int i18 = 161;
int j18 = 162;
int j17 = 163;
int j16 = 164;
int j15 = 165;
int j14 = 166;
int j13 = 167;
int j12 = 168;
int j11 = 169;
int j10 = 170;
int j9 = 171;
int j8 = 172;
int j7 = 173;
int j6 = 174;
int j5 = 175;
int j4 = 176;
int j3 = 177;
int j2 = 178;
//int j1 = 179;
int k1 = 180;
int k2 = 181;
int k3 = 182;
int k4 = 183;
int k5 = 184;
int k6 = 185;
int k7 = 186;
int k8 = 187;
int k9 = 188;
int k10 = 189;
int k11 = 190;
int k12 = 191;
int k13 = 192;
int k14 = 193;
int k15 = 194;
int k16 = 195;
int k17 = 196;
int k18 = 197;

char currentProblem;

void setup() {

ble_set_name(“Chad”);
ble_begin();
strip.begin();
strip.show();
Serial.begin(9600);

}

void loop() {

myiShield.getInbox();
currentProblem = myiShield.getKeypadData();

int prob = int(currentProblem);

if (currentProblem == ‘1’){

colorWipe(strip.Color(0, 0, 0), 200);
strip.setPixelColor(f18, 0xFF0000); //Green - Finish
strip.setPixelColor(h18, 0xFF0000); //Green - Finish
strip.setPixelColor(f15, 0x00FF00); //Red
strip.setPixelColor(g14, 0x00FF00); //Red
strip.setPixelColor(f12, 0x00FF00); //Red
strip.setPixelColor(j11, 0x00FF00); //Red
strip.setPixelColor(j7, 0x00FF00); //Red
strip.setPixelColor(f4, 0x0000FF); //Blue - Start
strip.show();

}

if (currentProblem == ‘2’){

colorWipe(strip.Color(0, 0, 0), 200);
strip.setPixelColor(h18, 0xFF0000); //Green - Finish
strip.setPixelColor(g14, 0x00FF00); //Red
strip.setPixelColor(f12, 0x00FF00); //Red
strip.setPixelColor(f4, 0x0000FF); //Blue - Start
strip.show();

}

}

void colorWipe(uint32_t c, uint8_t wait) {

int i;
for (i = 0; i < strip.numPixels(); i++) strip.setPixelColor(i, 0);

strip.show();

delay(wait);
}

KeypadTestwithProblemSingleDigit.ino (4.1 KB)

Ow.
My scroll finger hurts.

Where did you get this code? A link to the original source would probably help us know what it’s supposed to do.

It looks like you’re trying to merge two pieces of code. Is this correct? Please link to both sources.

This is the original code. It works the way it is except I can not add IF statements past 9. The program lights a string of addressable LEDs that are on a climbing wall. The attached code is the whole code minus additional IF statements.

Thank you again.

If you want multiple digits you will have to accumulate them through several reads of the keypad and then have some button that you use for an "enter" key to let the code know you're done entering numbers. Let's say we use '#' for enter.

Pseudocode:

int code = 0;  // put this at global scope

int key = get a new key press

if (key == '#') {
   call a function to process code through your if's or a switch / case
}
else {
   key = key - '0';  // turn an ascii character of a digit into a real number
   code = code * 10;
   code = code + key;
}

Or, instead of accumulating them into one big number, you could store them in a char array and use strcmp to compare them in your if statements. But that seems a bit harder.

Here is a sketch that will let you enter single digits, followed by the enter key, into the serial monitor and when you type ‘i’ prints the total of the digits entered. The digits are stored in a char array and converted to an integer when input is complete. It works works for up to 4 digits.

The thing that makes this a bit tricky is that you do not know in advance how many digits there will be in the end. The first number entered could be the ones, 10s, 100s or 1000s digit, so you need to store all the characters and calculate the value at the end.

I looked at the library docs and cannot figure out what they mean by “getKeypadData(), returns the current char value but just one time. If you use this method again (in the same loop) it returns ‘/’”. I think I would have to use the hardware to see how it works but hopefully this code will give you an idea of how to handle multiple digits.

/* 
 enter sigle digits in serial monitor, finish by entering 'i'
 */

void setup()
{

  Serial.begin(9600);

}

const int digitSize = 5;
char digits[digitSize];
int idx=0;

void loop()
{

  // if there is a character ready
  if( Serial.available() )
  {
    // read it
    char c = Serial.read();

    // if a number
    if( (c >= '0') && (c <= '9') )
    {
      // add to digits array 
      if(idx < (digitSize-1) ) { 
        digits[idx++] = c; 
      }

    } 
    else if ( c == 'i' ) {

      // entry complete, terminate string
      digits[idx] = '\0';

      int val=0;
      int mult=1;

      // calculate multplier for digits after ones
      for(int i=0; i < strlen(digits)-1; i++, mult*=10);

      // add in 10s, 100s, etc
      for(int i=0; i < strlen(digits)-1; i++)
      {
        val += ((digits[i] - '0') * mult);
        mult /= 10;

      } // for

      // add ones digit
      val += digits[strlen(digits)-1] - '0';
      Serial.println(val); 

      // reset index
      idx = 0;

    } // else 

  }

}

Why not simply store the number entered as . . .a number?
Radical huh?

Simply initialise a variable to zero, and if the new key entered is a digit, multiply the variable byten and add in the value of the key.

I agree with Groove. If all you want is a number at the end then you should use Delta_G's less convoluted method.

There may be a need to store characters in an array, maybe if you decide to accept characters as well as numbers and look for specific patterns, but for your purpose it just complicates maters.