« »

PComp 1, Part Deux: A Winning Combination

Ok, so I’ve figured out how to time a button press.  By looking at the stopwatch example on the arduino site I got a sense of how timing works, though that example is based on using a button as a click that both turns on and then turns of the timer just as it would on an analog stopwatch, so it needs to keep track of clicks to determine whether the timer is running in order to know to turn it on or off.  I’m simply interested in the time the button is actually depressed, which I’ve managed to do using the pulseIn() function:

void loop()
{
buttonState = digitalRead(buttonPin);
// read the button state and store

if (buttonState == 1) {
// if the button is pressed then start timing

digitalWrite(indicatorPin, HIGH);
// turn on indicator LED so user knows button's pressed
elapsedTime = pulseIn(buttonPin, HIGH, initialDelay);
// record and store the time the button was pressed
Serial.print(elapsedTime);
// shows me that it's working
}

 else {
digitalWrite(indicatorPin, LOW);
// if the button is not pressed, turn off the indicator LED
}
}

Still to do:

  1. Turn microsecond output into a generic “long” or “short” entry for combination
  2. Make sure that the program remembers three presses and either turns on the unlock light or the no good light
  3. Build in a reset so that the lock resets after three presses

Two hours have elapsed, I’ve reduced the duration of the presses to 0 or 1 (short or long), but I still can’t figure out how to make the program loop over three times.

 

Push the button...

Push the button...

...and the indicator lights and the timer starts

...and the indicator lights and the timer starts

The length of the press is output as either 1 ("long press") or 0 ("short press")

The length of the press is output as either 1 ("long press") or 0 ("short press")

I think in the interest of sleep and sanity I’m going to settle for the very inefficient and extremely inelegant solution of manually repeating the instructions three times.  Hmm, that didn’t work either.  I’ll ask someone tomorrow.

/* My goal here is to create a unique sequence of
three button pushes of differing lengths that
"opens the lock" by lighting a green LED */

/* LED connected to digital pin 4
(turns on to signal correct combination) */
#define ledPin  4

/* LED connected to digital pin 19
(turns on to signal incorrect combination) */
#define indicatorPin 3

// LED that signals the button is pressed
#define ledWrong 19

// button on pin 2
#define buttonPin 2

// variable to store button state
int buttonState;

// elapsed time during button press
unsigned long elapsedTime ;

// variable to control inPulse() listening window
unsigned long initialDelay = 5000000;

// the correct combination to open the lock
int combination[]= {
1,1,0};

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

pinMode(ledPin, OUTPUT);
pinMode(ledWrong, OUTPUT);
pinMode(indicatorPin, OUTPUT);
pinMode(buttonPin, INPUT);
}

void loop() {
// read the button state and store
buttonState = digitalRead(buttonPin);

// if the button is pressed then start timing
if (buttonState == 1) {
// turn on indicator LED so user knows button's pressed
digitalWrite(indicatorPin, HIGH);
// record and store the time the button was pressed
elapsedTime = pulseIn(buttonPin, HIGH, initialDelay);
//converts the time into a 1 or a 0 (long or short)
elapsedTime = (int)(elapsedTime / 100000L);
if (elapsedTime < 10) {
elapsedTime = 0;
}
else {
elapsedTime = 1;
}
// allows me to check that it's working
Serial.println(elapsedTime);
}
// if the button's not pressed, turn off indicator LED
else {
digitalWrite(indicatorPin, LOW);
}
}

Comments

Comments are closed.