Hi there... I'm new to Arduino programming. I'm building a counter with keypad to enter value. After searching the internet, I found sketch in this forum and it seems that this sketc is suitable for me. Here is my code, completely:
#include <Key.h>
#include <Keypad.h>
#include <LCD.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <Keyboard.h>
LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE); // Set the LCD I2C address
long startTime; // start time for stop watch
long inputTime; // entered time
long remainingTime; // remaining time
const int NRow = 2; // no of LCD rows
const int NCol = 16; // no of LCD columns
const byte ROWS = 4; // four rows of keypad
const byte COLS = 4; // four columns of keypad, replace with 3 if 3column is used
char keys[ROWS][COLS] =
{
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'.', '0', '#', 'D'}
};
byte rowPins[ROWS] = {11, 10, 9, 8}; //connect to the row pinouts of the keypad //four rows
byte colPins[COLS] = {7, 6, 5, 4}; //connect to the column pinouts of the keypad //four column
Keypad keypad = Keypad (makeKeymap(keys), rowPins, colPins, ROWS, COLS );
void setup()
{
lcd.begin(16, 2); // intialise the LCD.
}
void loop()
{
inputTime = GetNumber();
{
startTime = millis(); // store the start time
remainingTime = (inputTime - startTime); // sisa waktu berjalan
if (remainingTime < 0)
{
lcd.setCursor(0, 1);
lcd.print(inputTime - inputTime); // display input time
lcd.setCursor (12, 1);
lcd.print("STOP");
}
if (remainingTime > 0)
{
lcd.setCursor(0, 1);
lcd.print(remainingTime); // countdown function
lcd.setCursor (12, 1);
lcd.print("RUN!");
}
// Digit manipulation
if (remainingTime < 100000)
{
lcd.setCursor(5, 1);
lcd.print(" ");
}
if (remainingTime < 10000)
{
lcd.setCursor(4, 1);
lcd.print(" ");
}
if (remainingTime < 1000)
{
lcd.setCursor(3, 1);
lcd.print(" ");
}
if (remainingTime < 100)
{
lcd.setCursor(2, 1);
lcd.print(" ");
}
if (remainingTime < 10)
{
lcd.setCursor(1, 1);
lcd.print(" ");
}
}
}
int GetNumber() // adopted from https://forum.arduino.cc/index.php?topic=57627.0
{
int num = 0;
char key = keypad.getKey();
while (key != 'D') // Pressing "D" to run the program
{
switch (key)
{
case NO_KEY:
break;
case '0': case '1': case '2': case '3': case '4':
case '5': case '6': case '7': case '8': case '9':
case '.':
lcd.print(key);
num = num * 10 + (key - '0');
break;
case 'A': // Select mSec mode.
num = 0;
lcd.setCursor(0, 0);
lcd.print("mSec");
// mSec();
lcd.clear();
break;
case 'B': // Select Sec. mode.
num = 0;
lcd.setCursor(0, 0);
lcd.print("Sec.");
//tSec();
lcd.clear();
break;
case 'C': // Test solenoid.
num = 0;
lcd.clear();
//to be done
break;
case '#': // Clear screen.
num = 0;
lcd.clear();
break;
}
key = keypad.getKey();
}
return num;
}
But, it doesn't work with my counter: When I press "D" button to start, the counter is not counting. Pressing "D" change the counter value. Next counting attempt (after pressing previous "D") cause the entry line goes to another row.
I am expecting that I can enter some values. The counter starts counting when I press "D" and stops when the counter show "0" then ready for the next counting process.
I need somebody to help fixing my problem. Any help is appreciated. Thanks in advance.