I'm trying to get a modified code from a tutorial to have multiple loops.. is it possible?
void loop()
{
int l;
char key = keypad.getKey();
if (int(key) != 0) {
lcd.setCursor(14,3);
lcd.print(" ");
lcd.setCursor(12,3);
for (l=0; l<=currentPosition; ++l)
{
lcd.print("*");
}
if (key == ourCode4[currentPosition])
{
++currentPosition;
if (currentPosition == 3)
{
unlockDoor4();
currentPosition = 0;
}
} else {
invalidCode();
currentPosition = 0;
}
}
}
I want it to have multiple answers for the definition of the input code:
//set our code
char* ourCode = "123A";
int currentPosition = 0;
char* ourCode4 = "145";
int currentPosition4 = 0;
Why cant i just do the following?
void loop()
{
int l;
char key = keypad.getKey();
if (int(key) != 0) {
lcd.setCursor(14,3);
lcd.print(" ");
lcd.setCursor(12,3);
for (l=0; l<=currentPosition; ++l)
{
lcd.print("*");
}
if (key == ourCode4[currentPosition])
{
++currentPosition;
if (currentPosition == 3)
{
unlockDoor4();
currentPosition = 0;
}
} else {
invalidCode();
currentPosition = 0;
}
if (key == ourCode[currentPosition])
{
++currentPosition;
if (currentPosition == 4)
{
unlockDoor();
currentPosition = 0;
}
} else {
invalidCode();
currentPosition = 0;
}
}
}
Full Code here:
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <Keypad.h>
//constants for LEDs
int greenLED = 12;
int redLED = 13;
//set our code
char* ourCode = "123A";
int currentPosition = 0;
char* ourCode4 = "145";
int currentPosition4 = 0;
//define the keypad
const byte rows = 4;
const byte cols = 4;
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};
byte colPins[cols] = {7,6,5,4};
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, rows, cols);
#define GPIO_ADDR 0x27
LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE); // set the LCD I2C address
void setup() {
lcd.begin(16,2);
}
void loop()
{
int l;
char key = keypad.getKey();
if (int(key) != 0) {
lcd.setCursor(14,3);
lcd.print(" ");
lcd.setCursor(12,3);
for (l=0; l<=currentPosition; ++l)
{
lcd.print("*");
}
if (key == ourCode4[currentPosition])
{
++currentPosition;
if (currentPosition == 3)
{
unlockDoor4();
currentPosition = 0;
}
} else {
invalidCode();
currentPosition = 0;
}
}
}
void invalidCode()
{
digitalWrite(redLED, HIGH);
clearScreen();
lcd.setCursor(0,0);
lcd.print("No Letter Found");
lcd.setCursor(0,1);
lcd.print("");
lcd.setCursor(0,2);
lcd.print("");
lcd.setCursor(0,3);
lcd.print("");
delay(2000);
digitalWrite(redLED, LOW);
displayCodeEntryScreen();
}
void unlockDoor()
{
digitalWrite(greenLED, HIGH);
clearScreen();
lcd.setCursor(0,0);
lcd.print("** WELCOME! **");
lcd.setCursor(0,1);
lcd.print("");
lcd.setCursor(0,2);
lcd.print("");
lcd.setCursor(0,3);
lcd.print("********************");
"1235" ;//add any code to unlock the door here
delay(2000);
digitalWrite(greenLED, LOW);
displayCodeEntryScreen();
}
void unlockDoor4()
{
digitalWrite(greenLED, HIGH);
clearScreen();
lcd.setCursor(0,0);
lcd.print("* Letter: D *");
lcd.setCursor(0,1);
lcd.print("");
lcd.setCursor(0,2);
lcd.print("");
lcd.setCursor(0,3);
lcd.print("");
"145" ; //add any code to unlock the door here
delay(5000);
digitalWrite(greenLED, LOW);
displayCodeEntryScreen();
}
void displayCodeEntryScreen()
{
clearScreen();
lcd.setCursor(0,0);
lcd.print("Tactus");
lcd.setCursor(0,1);
lcd.print("");
lcd.setCursor(0,2);
lcd.print("Enter:");
}
void clearScreen()
{
lcd.setCursor(0,0);
lcd.print(" ");
lcd.setCursor(0,1);
lcd.print(" ");
lcd.setCursor(0,2);
lcd.print(" ");
lcd.setCursor(0,3);
lcd.print(" ");