0

I've got a program for a password protected doorlock, which I got working.

I'm trying to add to the code, to add functionality where I can toggle an LED on/off with a button.

Both codes function on their own and serve their desired purpose, but when I try to combine them into the same sketch, the LED button works but the rest of my design stops working. I can no longer input a password to activate the motor.

How can I combine the two codes and have them both work?

Here is the working keypad/lock code

#include <Wire.h> 
#include <LiquidCrystal_I2C.h>
#include <Keypad.h>

#define Password_Length 5 

int signalPin = 10;
int greenLedPin = 11;
int redLedPin = 12;
char Data[Password_Length] = "4";
char Master[Password_Length] = "1234"; 
byte data_count = 0, master_count = 0;
bool Pass_is_good;
char customKey;

const byte ROWS = 4;
const byte COLS = 3;
bool ledState = 0;

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

byte rowPins[ROWS] = {9, 8, 7, 6};
byte colPins[COLS] = {5, 4, 3};

Keypad customKeypad = Keypad(makeKeymap(hexaKeys), rowPins, colPins, ROWS,     COLS);

LiquidCrystal_I2C lcd(0x3f, 16, 2);   



void setup()
{
  lcd.init(); 
  lcd.backlight();
  Serial.begin(9600);
  lcd.clear();

  pinMode(redLedPin, OUTPUT);
  pinMode(greenLedPin, OUTPUT);
  digitalWrite(redLedPin, LOW);
  digitalWrite(greenLedPin, LOW);  
}



void loop()
{
  lcd.setCursor(0,0);
  lcd.print("Enter Password:");

  customKey = customKeypad.getKey();
  if (customKey){
    Data[data_count] = customKey; 
    lcd.setCursor(data_count,1); 
    lcd.print(Data[data_count]); 
    data_count++; 
    }

if(data_count == Password_Length-1){
    lcd.clear();

    if(!strcmp(Data, Master)){
      lcd.print("Correct");
      pinMode(signalPin, OUTPUT);
      digitalWrite(signalPin, LOW); //turns on motor 
      digitalWrite(greenLedPin, HIGH); //turns on led
      delay(2000);
      digitalWrite(signalPin, HIGH); //turns off motor
      digitalWrite(greenLedPin, LOW); //turns off led
      }
    else{
      lcd.print("Incorrect");
      digitalWrite(redLedPin, HIGH); //turns on led
      delay(2000);
      digitalWrite(redLedPin, LOW); //turns off led
      }

    lcd.clear();
    clearData();  
  }
}



void clearData(){
  while(data_count !=0){
    Data[data_count--] = 0; 
  }
  return;
}

and here is the code I'm using for the LED on/off toggle button

int button = 2;
int led = 13; 
int status = false;

void setup(){ 
pinMode(led, OUTPUT); 
pinMode(button, INPUT_PULLUP); // set the internal pull up resistor, 
unpressed button is HIGH 
}

void loop()
{ 
//a) if the button is not pressed the false status is reversed by !status 
and the LED turns on 
//b) if the button is pressed the true status is reversed by !status and 
the LED turns off 

if (digitalRead(button) == true) { 
status = !status; 
digitalWrite(led, status); 
} while(digitalRead(button) == true); 
delay(50); // keeps a small delay 
}
2
  • I just figured out that the original password code will still function, but only if i keep the LED toggle button held down.
    – nick g
    Apr 30, 2018 at 6:42
  • By the way, digitalRead does not return true or false, it returns HIGH or LOW. Coincidentally they are the same numbers internally, but it is better practice to check for what the function is documented to return. See: arduino.cc/reference/en/language/functions/digital-io/…
    – Nick Gammon
    Apr 30, 2018 at 7:41

1 Answer 1

2

Let's step back from the Arduino for a minute and ask: assuming you know how to cook some bacon, and also how to boil some eggs, how would you simultaneously cook bacon and eggs?

Basically you would have to interleave any process (such as waiting for the water to boil) with some other process (such as waiting for the bacon to cook).

Once you have this concept in your head you can just take the two pieces of code, and wherever you are planning to wait for something to happen, see if the other thing is able to be done.

I have done a post about this: http://www.gammon.com.au/blink

I just figured out that the original password code will still function, but only if i keep the LED toggle button held down.

Well, you need to remove any blocking loops from either part of the code.

Your code here blocks:

if (digitalRead(button) == true) { 
status = !status; 
digitalWrite(led, status); 
} while(digitalRead(button) == true); 

That's like (in my analogy) of thinking "oh the water is boiling, I'll have to stare at the eggs until they are cooked". Instead you could check the bacon, check the eggs, check the bacon again, and so on.

I'm giving you these analogies to help you solve the general problem, not this particular bit of coding.


i understand that the code blocks, but that code also makes the LED toggle on and off like i need, so how do i integrate that in a way that it doesnt block the rest?

Replace the LED code with something like this (not compiled or tested):

void loop ()
  {
  static int oldReading = LOW;
  int newReading = digitalRead (button);
  if (newReading != oldReading)  // it changed!
    {
    oldReading = newReading;  // remember new reading state
    if (newReading == HIGH)
      {
      status = !status; 
      digitalWrite(led, status);
      }  // end of a HIGH reading
    }  // end of changed state

   // password stuff here

   }  // end of loop

Now, whatever you do with the button, the code doesn't stop (block) so the password stuff is still executed.

2
  • thanks, that makes sense, but im too new to this stuff to know how to rectify that
    – nick g
    Apr 30, 2018 at 16:06
  • i understand that the code blocks, but that code also makes the LED toggle on and off like i need, so how do i integrate that in a way that it doesnt block the rest?
    – nick g
    Apr 30, 2018 at 16:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.