Looking for a simple way to code the input of a PIN number

Hi Everyone,

I’m doing a simple project just to get a better understanding of coding. I’m looking to connect four momentary pushbuttons (numbered 1 through 4) and have a Green LED light up if they are pushed in a certain order.

I want it to have several “passwords” that work for it, such as “1 2 3 4” and “1 3 2 2”.

I currently have the following code ready:

int ledPin = D0;
int buttonPin1 = D1;
int buttonPin2 = D2;
int buttonPin3 = D3;
int buttonPin4 = D4;

void setup()
{
  pinMode( buttonPin1 , INPUT_PULLUP);
  pinMode( buttonPin2 , INPUT_PULLUP);
  pinMode( buttonPin3 , INPUT_PULLUP);
  pinMode( buttonPin4 , INPUT_PULLUP);
  pinMode( ledPin , OUTPUT);
}

void loop()
{
  int buttonState1 = digitalRead( buttonPin1 );
  int buttonState2 = digitalRead( buttonPin2 );
  int buttonState3 = digitalRead( buttonPin3 );
  int buttonState4 = digitalRead( buttonPin4 );

but I’m not sure what to put next in the loop in order to facilitate the input of the passwords.

Can anyone guide me in the right direction?

Thanks!

This instructable is doing the exact same with a regular arduino. Have a read through it, and let us know if you need any help.

1 Like

I had done this in Arduino a long while ago.... I tried to update it for Spark, so it compiles but I didnt't really test it...


#define DEBOUNCE_TIME 75UL
#define KEYPAD_TIMEOUT 5000UL
typedef enum {
  LOCKED, UNLOCKED, UNKNOWN
}
deviceState;

deviceState state = LOCKED;

int buttonPin[] = {
  D2,D3,D4,D5};
int lastButtonState[sizeof(buttonPin)/sizeof(buttonPin[0])];
unsigned long lastButtonTime[sizeof(buttonPin)/sizeof(buttonPin[0])];
int buttonIndex = 0;
int pinCode[4];
unsigned long lastPushTime;
unsigned long refreshTime;

int unlockPins[3][4] = {
  {0,1,2,3},
  {3,2,1,0},
  {0,0,0,0}
};

bool timeOut = false;

void setup()
{
  Serial.begin(9600);
  pinMode(D7, OUTPUT);
  for (int i = 0; i < sizeof(buttonPin)/sizeof(buttonPin[0]); i++)
  {
    pinMode(buttonPin[i], INPUT_PULLUP);
  }

}
void loop()
{
  if (state == LOCKED)
  {
    if (millis() - refreshTime > 5000UL)
    {
      digitalWrite(D7, HIGH);
      Serial.println("SYSTEM LOCKED");
      refreshTime += 5000UL;
    }
    if (millis() - lastPushTime > KEYPAD_TIMEOUT && timeOut == true)
    {
      Serial.println("pin timed out...");
      clearCodes();
      timeOut = false;
    }
    for (int i = 0; i < sizeof(buttonPin)/sizeof(buttonPin[0]); i++)
    {
      int buttonState = digitalRead(buttonPin[i]);
      if (buttonState == LOW && lastButtonState[i] == HIGH)
      {
        if (millis() - lastButtonTime[i] > DEBOUNCE_TIME)
        {
          unsigned long pressed = millis();
          lastButtonTime[i] = pressed;
          lastPushTime = pressed;
          timeOut = true;
          pinCode[buttonIndex] = i;
          Serial.println(i);
          Serial.print("Pincode: ");
          for (int j = 0; j < 4; j++ )
          {
            Serial.print(pinCode[j]);
            if (j != 3)
              Serial.print(",");
            else
             Serial.println();
          }
          buttonIndex++;
        }
      }
      lastButtonState[i] = buttonState;
    }
    if (buttonIndex > 3)
    {
      int value = pinCheck();
      if (value == 1)
      {
        Serial.println("pin matched");
        state = UNLOCKED;
      }
      else
      {
        Serial.println("Nice try buddy...");
      }
      //
      clearCodes();
    }
  }
  else if (state == UNLOCKED)
  {
    if (millis() - refreshTime > 5000UL)
    {
      digitalWrite(D7, LOW);
      Serial.println("UNLOCKED!!");
      refreshTime += 5000UL;
    }
    // do your Unlocked Stuff here!!!
  }
  else
  {

  }
}
int clearCodes()
{
  buttonIndex = 0;
  for (int i = 0; i < sizeof(pinCode)/sizeof(pinCode[0]); i++)
  {
    pinCode[i] = -1;
  }
}
int pinCheck()
{
  Serial.println("Pin Entered");
  bool matched = false;
  for (int i = 0; i < 3; i++)
  {
    for (int j = 0; j < 4; j++)
    {
      if (unlockPins[i][j] != pinCode[j])
        break;
      if (j == 3) matched = true; // EDIT: sorry I made a typo!
    }
    if (matched == true)
      return 1;
  }
  return 0;
}
1 Like

@Moors7, Thanks - I’m going to take a look at this.

@BulldogLowell, Awesome! I’m gonna try this code. Thanks for your help.

1 Like

It works!

But I’m having trouble with the serial monitor. My COM port isn’t showing up at all, it’s only showing my bluetooth input. I have the Spark plugged into my USB port, and I’m running CoolTerm for the monitoring, but no dice. Do you know what might be causing this?

I’m using a Mac, not sure if that makes any difference.

1 Like

Is it in listening mode (blinking blue)?

It’s showing as breathing light blue, so I’m assuming that means it’s connected to the internet.

Does it need to be in listening mode to communicate with the serial monitor?

Yeah, that’s the “breathing cyan”, but not “blinking blue” :wink:
As far as I’m aware, it does indeed need to be in listening mode for Serial connection. Try this: http://docs.spark.io/connect/#connecting-your-core-listening-mode

Got it, thanks! It worked once, but then when I tried a second time, it has stopped working. It’s “blinking blue” but now it’s not showing up in the Port options again. I’ve tried unplugging it and restarting, as well as moving it to other USB ports, but no dice.

Try re-opening the program you’re using, that often helps.

Still no luck. Thanks for your help - I think I’m going to just keep messing with it.

have you already tried using your mac’s terminal:

in terminal:

ls /dev/tty.*

you get output like this:

/dev/tty.Bluetooth-Incoming-Port	/dev/tty.Bluetooth-Modem		/dev/tty.usbmodem1421 // <<<this is my spark

then enter:

screen /dev/tty.usbmodem1421

When I execute that line, the output is:

/dev/tty.Bluetooth-Incoming-Port
/dev/tty.Bluetooth-Modem

And that's it, even though my spark is plugged in and blinking blue. It's perplexing. I've tried restarting my computer as well as unplugging and re-plugging the spark.

I’d try a factory reset on the core (while powering from another USB power source if you can)

again, reboot your mac and try again.

That’s really strange… I was about to reply with pretty much the same thing @BulldogLowell said about screen which is my preferred serial terminal now… however the Core that I just grabbed won’t open up a Serial port for the life of me. I put my Core in listening mode and nothing shows up for ls /dev/tty.* I grabbed another Core though and it works fine. I think I’ll do some reprogramming of this one that doesn’t work and see what it takes to make it work.

EDIT: Just put Tinker back on my non-working Core with Spark CLI command spark flash --usb tinker with Core in DFU mode… and it’s opening up a serial port now. Not sure what I had programmed on it… but that was the issue. Factory Reset sounds like a great idea for you @skrotseng :wink:

1 Like