Help writing alarm program in C code to Arduino

Thread Starter

adrian_f81

Joined Dec 12, 2015
1
Hello, I am a beginner writing code and I am having problems trying to figure out how to do a loop correctly. I am writing in C to the Arduino. I have a
4x4 keypad and an LCD. I am trying to get the LCD to display when it is running that an alarm is on and when I enter the correct pin it will display the alarm off. I can get it to do that but what I also want it to do is when I enter the password again it displays the alarm is on again, and that is where I am having my problem. I cannot get it to move on to my next part of the code...I've tried using if else statement, switch case, and go to. I'm sure I am just doing it incorrectly and any input or suggestions would be greatly appreciated. I have attached the code on a notepad doc.

Thanks,
Adrian

C:
#include <Wire.h>
#include <Keypad.h> // for I2C LCD
#include "LiquidCrystal_I2C.h" // for I2C bus LCD module

//constants for LEDs
const int switchPin = 2;
int greenLED = 12;
int redLED = 13;

//set our code
char* ourCode = "1234";
int currentPosition = 0;
int newPosition = 0;
const byte numRows= 4; //number of rows on the keypad
const byte numCols= 4; //number of columns on the keypad

//keymap defines the key pressed according to the row and columns just as appears on the keypad
char keymap[numRows][numCols]=
{
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'}
};

//Code that shows the the keypad connections to the arduino terminals
byte rowPins[numRows] = {9, 8, 7, 6}; //Rows 0 to 3
byte colPins[numCols]= {5, 4, 3, 2}; //Columns 0 to 3

//initializes an instance of the Keypad class
Keypad myKeypad= Keypad(makeKeymap(keymap), rowPins, colPins, numRows, numCols);

LiquidCrystal_I2C lcd(0x27,16,4);

void setup()
{
  lcd.init();
  lcd.backlight();
   
  //Serial.begin(9600);
  displayAlarmOffScreen();
 
  pinMode(switchPin, INPUT);

  //setup and turn off both LEDs
  pinMode(redLED, OUTPUT);
  pinMode(greenLED, OUTPUT);
  digitalWrite(redLED, LOW);
  digitalWrite(greenLED, LOW);
  digitalWrite(switchPin, HIGH);
}

//If key is pressed, this key is stored in 'keypressed' variable
//If key is not equal to 'NO_KEY', then this key is printed out
//if count=17, then count is reset back to 0 (this means no key is pressed during the whole keypad scan process
void loop()
{
int i;
int l;
char keypressed = myKeypad.getKey();
int num;
int oddcount = 0;
int evencount = 0;


if (keypressed != NO_KEY)
{
    lcd.setCursor(11,1);
    lcd.print("");
    //lcd.setCursor(0,1);

for (i = 0; i <= currentPosition; i++)
  {
    lcd.print("*");
  }
   if (keypressed == ourCode[currentPosition])
    {
      ++currentPosition;
      if (currentPosition == 4)
      {
        unlockDoor();
        //doorSensor();
        currentPosition = 0;
      
      }
      } else      {
        invalidCode();
        currentPosition = 0;
      }

  /* for (i = 0; i <= newPosition; i++)
  {
    lcd.print(" ");
  }

       if (keypressed == ourCode[newPosition])
    {
      ++newPosition;
      if (newPosition == 4)
      {
        lockDoor();
        newPosition = 0;
        doorSensor();
       
      }
      } else {
        invalidCode();
        newPosition = 0;
      }*/
}    
}

void doorSensor()
{
  if(digitalRead(switchPin) == LOW)
  {

  digitalWrite(redLED, LOW);
  digitalWrite(greenLED, HIGH);
  delay(3000);  // waits for 5 seconds
  }

  else{

  digitalWrite(redLED, HIGH);
  digitalWrite(greenLED, LOW);
  delay(3000); // waits for 5 seconds
}
}

void invalidCode()
{
  digitalWrite(redLED, HIGH);
  digitalWrite(greenLED, LOW);
  clearScreen();
  //lcd.setCursor(0,0);
  //lcd.print("******************");
  lcd.setCursor(1,1);
  lcd.print("**ACCESS DENIED!**");
  lcd.setCursor(1,2);
  lcd.print("**INVALID CODE**");
  lcd.setCursor(1,3);
  lcd.print("******************");

  delay(5000);
  digitalWrite(redLED, LOW);
  clearScreen();
  displayAlarmOnScreen();
  //unlockDoor();
}

void unlockDoor()
{
  digitalWrite(greenLED, HIGH);
  clearScreen();
  lcd.setCursor(1,0);
  lcd.print("******************");
  lcd.setCursor(1,1);
  lcd.print("**ACTIVATED**");
  lcd.setCursor(1,2);
  lcd.print("**WELCOME!!**");
  lcd.setCursor(1,3);
  lcd.print("******************");

  //add any code to unlock the door here
  delay(3000);
  digitalWrite(greenLED, LOW);
  displayAlarmOnScreen();
}

void lockDoor()
{
  digitalWrite(redLED, HIGH);
  clearScreen();
  lcd.setCursor(1,0);
  //lcd.print("*****************");
  lcd.setCursor(1,1);
  lcd.print("**SYSTEM ACTIVE**");
  lcd.setCursor(1,2);
  //lcd.print("*****************");
  lcd.setCursor(1,3);
  //lcd.print("*****************");

  //add any code to unlock the door here
  delay(3000);
  digitalWrite(greenLED, LOW);
  displayAlarmOffScreen();
}

void displayAlarmOffScreen()
{
 
  clearScreen();
  lcd.setCursor(1,0);
  lcd.print("Sensors On! ");
  lcd.setCursor(1,1);
  lcd.print("Enter Pin: ");
  //lcd.setCursor(1,2);
  //lcd.print("Enter Pin:");
}

void displayAlarmOnScreen()
{
 
  clearScreen();
  lcd.setCursor(0,0);
  lcd.print("Sensors Off! ");
  lcd.setCursor(0,1);
  lcd.print("Enter Pin: ");
  //lcd.setCursor(0,2);
  //lcd.print("Enter Pin:");
}

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("                    ");
}
Mod edit: added code tags
 

Attachments

Last edited by a moderator:
Top