Help writing alarm program in C code to Arduino

Discussion in 'Embedded Systems and Microcontrollers' started by adrian_f81, Dec 12, 2015.

  1. adrian_f81

    Thread Starter New Member

    Dec 12, 2015
    1
    0
    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

    Code (C):
    1. #include <Wire.h>
    2. #include <Keypad.h> // for I2C LCD
    3. #include "LiquidCrystal_I2C.h" // for I2C bus LCD module
    4.  
    5. //constants for LEDs
    6. const int switchPin = 2;
    7. int greenLED = 12;
    8. int redLED = 13;
    9.  
    10. //set our code
    11. char* ourCode = "1234";
    12. int currentPosition = 0;
    13. int newPosition = 0;
    14. const byte numRows= 4; //number of rows on the keypad
    15. const byte numCols= 4; //number of columns on the keypad
    16.  
    17. //keymap defines the key pressed according to the row and columns just as appears on the keypad
    18. char keymap[numRows][numCols]=
    19. {
    20. {'1', '2', '3', 'A'},
    21. {'4', '5', '6', 'B'},
    22. {'7', '8', '9', 'C'},
    23. {'*', '0', '#', 'D'}
    24. };
    25.  
    26. //Code that shows the the keypad connections to the arduino terminals
    27. byte rowPins[numRows] = {9, 8, 7, 6}; //Rows 0 to 3
    28. byte colPins[numCols]= {5, 4, 3, 2}; //Columns 0 to 3
    29.  
    30. //initializes an instance of the Keypad class
    31. Keypad myKeypad= Keypad(makeKeymap(keymap), rowPins, colPins, numRows, numCols);
    32.  
    33. LiquidCrystal_I2C lcd(0x27,16,4);
    34.  
    35. void setup()
    36. {
    37.   lcd.init();
    38.   lcd.backlight();
    39.    
    40.   //Serial.begin(9600);
    41.   displayAlarmOffScreen();
    42.  
    43.   pinMode(switchPin, INPUT);
    44.  
    45.   //setup and turn off both LEDs
    46.   pinMode(redLED, OUTPUT);
    47.   pinMode(greenLED, OUTPUT);
    48.   digitalWrite(redLED, LOW);
    49.   digitalWrite(greenLED, LOW);
    50.   digitalWrite(switchPin, HIGH);
    51. }
    52.  
    53. //If key is pressed, this key is stored in 'keypressed' variable
    54. //If key is not equal to 'NO_KEY', then this key is printed out
    55. //if count=17, then count is reset back to 0 (this means no key is pressed during the whole keypad scan process
    56. void loop()
    57. {
    58. int i;
    59. int l;
    60. char keypressed = myKeypad.getKey();
    61. int num;
    62. int oddcount = 0;
    63. int evencount = 0;
    64.  
    65.  
    66. if (keypressed != NO_KEY)
    67. {
    68.     lcd.setCursor(11,1);
    69.     lcd.print("");
    70.     //lcd.setCursor(0,1);
    71.  
    72. for (i = 0; i <= currentPosition; i++)
    73.   {
    74.     lcd.print("*");
    75.   }
    76.    if (keypressed == ourCode[currentPosition])
    77.     {
    78.       ++currentPosition;
    79.       if (currentPosition == 4)
    80.       {
    81.         unlockDoor();
    82.         //doorSensor();
    83.         currentPosition = 0;
    84.      
    85.       }
    86.       } else      {
    87.         invalidCode();
    88.         currentPosition = 0;
    89.       }
    90.  
    91.   /* for (i = 0; i <= newPosition; i++)
    92.   {
    93.     lcd.print(" ");
    94.   }
    95.  
    96.        if (keypressed == ourCode[newPosition])
    97.     {
    98.       ++newPosition;
    99.       if (newPosition == 4)
    100.       {
    101.         lockDoor();
    102.         newPosition = 0;
    103.         doorSensor();
    104.        
    105.       }
    106.       } else {
    107.         invalidCode();
    108.         newPosition = 0;
    109.       }*/
    110. }    
    111. }
    112.  
    113. void doorSensor()
    114. {
    115.   if(digitalRead(switchPin) == LOW)
    116.   {
    117.  
    118.   digitalWrite(redLED, LOW);
    119.   digitalWrite(greenLED, HIGH);
    120.   delay(3000);  // waits for 5 seconds
    121.   }
    122.  
    123.   else{
    124.  
    125.   digitalWrite(redLED, HIGH);
    126.   digitalWrite(greenLED, LOW);
    127.   delay(3000); // waits for 5 seconds
    128. }
    129. }
    130.  
    131. void invalidCode()
    132. {
    133.   digitalWrite(redLED, HIGH);
    134.   digitalWrite(greenLED, LOW);
    135.   clearScreen();
    136.   //lcd.setCursor(0,0);
    137.   //lcd.print("******************");
    138.   lcd.setCursor(1,1);
    139.   lcd.print("**ACCESS DENIED!**");
    140.   lcd.setCursor(1,2);
    141.   lcd.print("**INVALID CODE**");
    142.   lcd.setCursor(1,3);
    143.   lcd.print("******************");
    144.  
    145.   delay(5000);
    146.   digitalWrite(redLED, LOW);
    147.   clearScreen();
    148.   displayAlarmOnScreen();
    149.   //unlockDoor();
    150. }
    151.  
    152. void unlockDoor()
    153. {
    154.   digitalWrite(greenLED, HIGH);
    155.   clearScreen();
    156.   lcd.setCursor(1,0);
    157.   lcd.print("******************");
    158.   lcd.setCursor(1,1);
    159.   lcd.print("**ACTIVATED**");
    160.   lcd.setCursor(1,2);
    161.   lcd.print("**WELCOME!!**");
    162.   lcd.setCursor(1,3);
    163.   lcd.print("******************");
    164.  
    165.   //add any code to unlock the door here
    166.   delay(3000);
    167.   digitalWrite(greenLED, LOW);
    168.   displayAlarmOnScreen();
    169. }
    170.  
    171. void lockDoor()
    172. {
    173.   digitalWrite(redLED, HIGH);
    174.   clearScreen();
    175.   lcd.setCursor(1,0);
    176.   //lcd.print("*****************");
    177.   lcd.setCursor(1,1);
    178.   lcd.print("**SYSTEM ACTIVE**");
    179.   lcd.setCursor(1,2);
    180.   //lcd.print("*****************");
    181.   lcd.setCursor(1,3);
    182.   //lcd.print("*****************");
    183.  
    184.   //add any code to unlock the door here
    185.   delay(3000);
    186.   digitalWrite(greenLED, LOW);
    187.   displayAlarmOffScreen();
    188. }
    189.  
    190. void displayAlarmOffScreen()
    191. {
    192.  
    193.   clearScreen();
    194.   lcd.setCursor(1,0);
    195.   lcd.print("Sensors On! ");
    196.   lcd.setCursor(1,1);
    197.   lcd.print("Enter Pin: ");
    198.   //lcd.setCursor(1,2);
    199.   //lcd.print("Enter Pin:");
    200. }
    201.  
    202. void displayAlarmOnScreen()
    203. {
    204.  
    205.   clearScreen();
    206.   lcd.setCursor(0,0);
    207.   lcd.print("Sensors Off! ");
    208.   lcd.setCursor(0,1);
    209.   lcd.print("Enter Pin: ");
    210.   //lcd.setCursor(0,2);
    211.   //lcd.print("Enter Pin:");
    212. }
    213.  
    214. void clearScreen()
    215. {
    216.   lcd.setCursor(0,0);
    217.   lcd.print("                    ");
    218.   lcd.setCursor(0,1);
    219.   lcd.print("                    ");
    220.   lcd.setCursor(0,2);
    221.   lcd.print("                    ");
    222.   lcd.setCursor(0,3);
    223.   lcd.print("                    ");
    224. }
    Mod edit: added code tags
     
    Last edited by a moderator: Dec 13, 2015
Loading...