Pages: [1]   Go Down
Author Topic: how to count time between two btn presses ?  (Read 311 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Jr. Member
**
Karma: 1
Posts: 97
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

First of all Thank you all for your help !

I am trying to create a text messenger ! I will pressent the code in a sec ! Now everything works just fine ( if i press the same btn agen it changes characters in the same cursor point, if i press another one the cursor moves 1 point to the right and does the same thing, when it reaches the end of a row to the lcd screen goes to the next) everything is perfect. BUT ..... i want to have a function that counts the time between btn press when i press the same bt, so if it is less than a sec to continue changing the characters but if it more than a sec to move the cursor 1 point to the right and start the same all over agen.

Here is the code so far :


Code:
#include <Keypad.h>
#include <ctype.h>
#include <LiquidCrystal.h>

// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
const byte ROWS = 4; //four rows
const byte COLS = 4; //three columns
// Define the keymaps.  The blank spot (lower left) is the space character.
char alphaKeys[ROWS][COLS] = {
    { 'a','d','g', },
    { 'j','m','p', },
    { 's','v','y', },
    { ' ','.','#', }
};

char numberKeys[ROWS][COLS] = {
    { '1','2','3', },
    { '4','5','6', },
    { '7','8','9', },
    { ' ','0','#', }
};

boolean alpha = false;   // Start with the numeric keypad.
char* keypadMap = (alpha == true) ? makeKeymap(alphaKeys) : makeKeymap(numberKeys);

byte rowPins[ROWS] = {48, 49, 50, 51};    //connect to the row pinouts of the keypad
byte colPins[COLS] = {40, 41, 43, 42};    //connect to the column pinouts of the keypad

//create a new Keypad
Keypad keypad = Keypad(keypadMap, rowPins, colPins, sizeof(rowPins), sizeof(colPins));

unsigned long startTime;
const byte ledPin = 13;    
int i = 0;
int j = 0;
int flag = 0;
long LastDebounceTime = 0;
long FirstDebounceTime = 0;

void setup(){
    lcd.begin(16, 2);
    Serial.begin(9600);
    pinMode(ledPin, OUTPUT);
    digitalWrite(ledPin, LOW);                                                // Turns the LED on.
    keypad.addEventListener(keypadEvent);                                      // Add an event listener.
    keypad.setHoldTime(500);
};

void loop(){
  FirstDebounceTime = millis();
  
  char key = keypad.getKey();

    if (alpha && millis()-startTime>100) {           // Flash the LED if we are using the letter keymap.
        digitalWrite(ledPin,!digitalRead(ledPin));
      startTime = millis();
    }
char waitForKey();
};

// Take care of some special events.
void keypadEvent(KeypadEvent key) {
    static char virtKey = NO_KEY;      // Stores the last virtual key press. (Alpha keys only)
    static char physKey = NO_KEY;      // Stores the last physical key press. (Alpha keys only)
    static char buildStr[12];
    static byte buildCount;
    static byte pressCount;

    switch (keypad.getState())
    {
    case PRESSED:
        if (isalpha(key)) {          // This is a letter key so we're using the letter keymap.
            if (physKey != key) {        // New key so start with the first of 3 characters.
                pressCount = 0;
                virtKey = key;
                physKey = key;
            }
            else {              // Pressed the same key again...
            
                LastDebounceTime = millis() - startTime;
                if ((LastDebounceTime - startTime)>1000){
                virtKey++;               // so select the next character on that key.
                pressCount++;            // Tracks how many times we press the same key.
                i--;
                }
            }
            if (pressCount > 2) {        // Last character reached so cycle back to start.
                pressCount = 0;
                virtKey = key;
            }
            if (i == 16 & flag == 0 & j == 0){
              i = 0;
              j = 1;
              flag = 1;
              
            }
            lcd.setCursor(i,j);
            Serial.print(i);       // Used for testing.
            Serial.print(j);      // Used for testing.
            lcd.print(virtKey);
            if (i == 15 & j == 1){
              i = 0;
              j = 0;
              flag = 0;
            }
            else {
              i++;
            };
              
      }
        if (isdigit(key) || key == ' ' || key == '.')  lcd.print(key);
        if (key == '#')  Serial.println();
        break;

    case HOLD:
        if (key == '#')  {                   // Toggle between keymaps.
            if (alpha == true)  {            // We are currently using a keymap with letters
                keypad.begin(*numberKeys);   // and want to change to numbers.
                alpha = false;
                digitalWrite(ledPin, LOW);
            }
            else  {                          // Or, we are currently using a keymap with numbers
                keypad.begin(*alphaKeys);    // and want to change to letters.
                alpha = true;
            }
        }
        else  {                             // Some key other than '#' was pressed.
            buildStr[buildCount++] = (isalpha(key)) ? virtKey : key;
            buildStr[buildCount] = '\0';
            Serial.println();
            Serial.println(buildStr);
        }
        break;

    case RELEASED:
        if (buildCount >= sizeof(buildStr))  buildCount = 0;    // Our string is full. Start fresh.
        break;

    }  // end switch-case
}  // end keypad events


it doesnt work

any help ?


Moderator edit: [code] [/code] tags added.
« Last Edit: February 10, 2012, 12:34:34 am by Coding Badly » Logged

Global Moderator
Dallas
Offline Offline
Shannon Member
*****
Karma: 208
Posts: 12932
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset


PLEASE do not multipost.
Logged

Pages: [1]   Go Up
Jump to: