code for the LCD and keypad

Dependencies:   mbed TextLCD

Committer:
jEvans99
Date:
Wed Nov 13 16:42:15 2019 +0000
Revision:
0:84b27625b7d0
Final Revision kindof?

Who changed what in which revision?

UserRevisionLine numberNew contents of line
jEvans99 0:84b27625b7d0 1 #include "mbed.h"
jEvans99 0:84b27625b7d0 2 #include "TextLCD.h"
jEvans99 0:84b27625b7d0 3 BusOut leds(D14,D15);//creates a global busout that identifies the pins associated with the leds.
jEvans99 0:84b27625b7d0 4 bool keycode();//Function prototype for the function that checks if the correct keycode was entered(returns boolean)
jEvans99 0:84b27625b7d0 5 float readtempurature(AnalogIn *temp);//function prototype fot the readtempurature function(this function was done by ben)
jEvans99 0:84b27625b7d0 6 char scan4by3keypad(BusIn* col, BusOut* row, const char k[4][3]); //function prototype for the scan4by3keypad function(returns a char)
jEvans99 0:84b27625b7d0 7 TextLCD lcd(D6, D5, D4, D3, D2, A0); // creates a global object of the TextLCD class called lcd with the pins D6,D5,D4,D3,D2,A0 assigned. These pins correspond to the pins rs, e, d4-d7 on the lcd
jEvans99 0:84b27625b7d0 8 char usercode[4] = {' ',' ',' ',' '};//creates a global char array that is used to store the entered code. This array is initialized to nothing
jEvans99 0:84b27625b7d0 9 /************************************************************************************
jEvans99 0:84b27625b7d0 10 Main function:
jEvans99 0:84b27625b7d0 11 Purpose: The purpose of the main function is to determine the usercode entered from the keypad. If the code is correct the function will light the green
jEvans99 0:84b27625b7d0 12 led, print access granted, and then the temperature to the LCD display. if the code entered is incorrect, the function will prompt the user to try again and light the red LED.
jEvans99 0:84b27625b7d0 13
jEvans99 0:84b27625b7d0 14 Pins declared:
jEvans99 0:84b27625b7d0 15 temp(A1,AnalogIn): This pin is used to bring the value of the thermistor onto the nucleo board since it is an analog value the pin is declared as AnalogIn.
jEvans99 0:84b27625b7d0 16 rowpins(D13,D12,D11,D10, BusOut): These pins are declared as a bus out and are connected to the row pins of the keypad. The pins will be used to turn the bus pins high.
jEvans99 0:84b27625b7d0 17 columnpins(D9,D8,D7, BusIn): These pins are declared as a bus in and are connected to the column pins of the keypad. The pins are used to read the state of the column pins to determine if a key has been pressed.
jEvans99 0:84b27625b7d0 18
jEvans99 0:84b27625b7d0 19 Variables:
jEvans99 0:84b27625b7d0 20 button(const char): a 4 by 3 array that is used to store the char equivalent to the keys on the keypad.
jEvans99 0:84b27625b7d0 21 access(bool): a boolean variable that is true if the code entered is correct, and is false if the code entered is incorrect.
jEvans99 0:84b27625b7d0 22
jEvans99 0:84b27625b7d0 23 Functions:
jEvans99 0:84b27625b7d0 24 lcd.cls(): a function of the lcd class that will clear the lcd display
jEvans99 0:84b27625b7d0 25 lcd.printf(""):a function of the lcd class that will print to the lcd display
jEvans99 0:84b27625b7d0 26 lcd.locate(c,r): a function of the lcd class that will move the cursor to the column(c) and row(r) on the lcd that is specified.
jEvans99 0:84b27625b7d0 27 scan4by3keypad(&columnpins, &rowpins, button):a function created previously that accepts the addresses of the row and column pins as well as the value of the button array. This function returns the char representing
jEvans99 0:84b27625b7d0 28 the button pressed.
jEvans99 0:84b27625b7d0 29 wait(x): waits x amount of seconds then continues with the function.
jEvans99 0:84b27625b7d0 30 if(condition){}:the if statement will run the code contained in the curly brackets if the condition is met
jEvans99 0:84b27625b7d0 31 else{}: the else statment is attached to an if statement and runs the code contained in the curly brackets if the condition of the if statement is not met
jEvans99 0:84b27625b7d0 32 leds[x].write(y):writes y(either a 1 or 0) to the pin at the position x in the leds busout.
jEvans99 0:84b27625b7d0 33 while(condition){}:continues to loop through the code contained in the curly brackets while the condition is met. The loop will be broken if the condition is not met.
jEvans99 0:84b27625b7d0 34 for(;;){}: runs the code contained in the brackets forever and ever.
jEvans99 0:84b27625b7d0 35 readtempurature(&temp): A function created by Ben that returns a value of type float representing the temperature. This function accepts the address of the temp pins.
jEvans99 0:84b27625b7d0 36 *************************************************************************************/
jEvans99 0:84b27625b7d0 37 int main() {
jEvans99 0:84b27625b7d0 38 AnalogIn temp (A1);//indentifies the pin A1 as an analogin pin named temp
jEvans99 0:84b27625b7d0 39 BusOut rowpins(D13,D12,D11,D10);//busout for my row pins
jEvans99 0:84b27625b7d0 40 BusIn columnpins(D9,D8,D7);//busin for my colummn pins
jEvans99 0:84b27625b7d0 41 columnpins.mode(PullDown);//enables the internal pulldown for the column pins
jEvans99 0:84b27625b7d0 42 const char button[4][3]= {{'1','2','3'},
jEvans99 0:84b27625b7d0 43 {'4','5','6'},
jEvans99 0:84b27625b7d0 44 {'7','8','9'},
jEvans99 0:84b27625b7d0 45 {'4','0','#'}};//array which helps determine which button was
jEvans99 0:84b27625b7d0 46 bool access = false;//boolean variable that will change depending on whether the keycode is correct or incorrect.
jEvans99 0:84b27625b7d0 47 while(access == false)//loops through this code while the access is false (keycode entered is wrong)
jEvans99 0:84b27625b7d0 48 {
jEvans99 0:84b27625b7d0 49 lcd.cls();//clears the LCD display
jEvans99 0:84b27625b7d0 50 lcd.printf("Enter Code:\n");//prints "enter code" to the LCD display.
jEvans99 0:84b27625b7d0 51 lcd.locate(11,0);//moves the cursor to the 11th column of the 0th row on the lcd display
jEvans99 0:84b27625b7d0 52 usercode[0] = scan4by3keypad(&columnpins, &rowpins, button);//sets the first value of usercode to the char returned by the scan4by3keypad function.
jEvans99 0:84b27625b7d0 53 lcd.printf("*");//prints a * to the lcd at the 11th column of the 0th row ro show that a value has been entered into the usercode
jEvans99 0:84b27625b7d0 54 wait(0.5);//waits half a second
jEvans99 0:84b27625b7d0 55 usercode[1] = scan4by3keypad(&columnpins, &rowpins, button);//sets the second value of usercode to the char returned by the scan4by3keypad function.
jEvans99 0:84b27625b7d0 56 lcd.locate(12,0);//moves the cursor to the 12th column of the 0th row on the lcd display
jEvans99 0:84b27625b7d0 57 lcd.printf("*");//prints a * to the lcd at the 12th column of the 0th row ro show that a value has been entered into the usercode
jEvans99 0:84b27625b7d0 58 wait(0.5);//waits half a second
jEvans99 0:84b27625b7d0 59 usercode[2] = scan4by3keypad(&columnpins, &rowpins, button);//sets the third value of usercode to the char returned by the scan4by3keypad function.
jEvans99 0:84b27625b7d0 60 lcd.locate(13,0);//moves the cursor to the 13th column of the 0th row on the lcd display
jEvans99 0:84b27625b7d0 61 lcd.printf("*");//prints a * to the lcd at the 13th column of the 0th row ro show that a value has been entered into the usercode
jEvans99 0:84b27625b7d0 62 wait(0.5);//waits half a second.
jEvans99 0:84b27625b7d0 63 usercode[3] = scan4by3keypad(&columnpins, &rowpins, button);//sets the fourth value of usercode to the char returned by the scan4by3keypad function.
jEvans99 0:84b27625b7d0 64 lcd.locate(14,0);//moves the cursor to the 14th column of the 0th row on the lcd display
jEvans99 0:84b27625b7d0 65 lcd.printf("*");//prints a * to the lcd at the 14th column of the 0th row ro show that a value has been entered into the usercode
jEvans99 0:84b27625b7d0 66 wait(1);//waits one second
jEvans99 0:84b27625b7d0 67 if(keycode() == true)//if the keycode is correct this code will run (breaks the loop)
jEvans99 0:84b27625b7d0 68 {
jEvans99 0:84b27625b7d0 69 lcd.cls();//clears the lcd completely
jEvans99 0:84b27625b7d0 70 lcd.locate(0,0);//moves the cursor to the 0th column of the 0th row of the LCD
jEvans99 0:84b27625b7d0 71 lcd.printf("Access granted!\n");//prints "Access granted" to the LCD
jEvans99 0:84b27625b7d0 72 leds[0].write(1);//writes 1 to the 0th value of the leds busout. (green LED)
jEvans99 0:84b27625b7d0 73 wait(1);//waits one second
jEvans99 0:84b27625b7d0 74 leds[0].write(0);//writes 0 to the 0th value of the leds busout. (green LED)
jEvans99 0:84b27625b7d0 75 access = true;//sets access to true which breaks the while loop.
jEvans99 0:84b27625b7d0 76 }
jEvans99 0:84b27625b7d0 77 else//if the keycode is not correct this code will run (does not break the loop)
jEvans99 0:84b27625b7d0 78 {
jEvans99 0:84b27625b7d0 79 lcd.cls();//clears the lcd completely
jEvans99 0:84b27625b7d0 80 lcd.locate(0,0);//moves the cursor to the 0th column of the 0th row of the LCD
jEvans99 0:84b27625b7d0 81 lcd.printf("Access Denied!");//prints "Access Denied!" to the LCD display
jEvans99 0:84b27625b7d0 82 leds[1].write(1);//writes 1 to the 1st value of the leds busout(red led)
jEvans99 0:84b27625b7d0 83 wait(1);//waits one second
jEvans99 0:84b27625b7d0 84 leds[1].write(0);//writes 0 to the 1st value of the leds busout(red led)
jEvans99 0:84b27625b7d0 85
jEvans99 0:84b27625b7d0 86 }
jEvans99 0:84b27625b7d0 87 }
jEvans99 0:84b27625b7d0 88 for(;;)//infinite loop
jEvans99 0:84b27625b7d0 89 {
jEvans99 0:84b27625b7d0 90 lcd.locate(0,1);//moves the cursor to the 0th column of the first row on the LCD
jEvans99 0:84b27625b7d0 91 lcd.printf("Temp: %.2f",readtempurature(&temp));//prints the value returns by the readtempurature function.(.2f will cutoff the value after the second decimal place.
jEvans99 0:84b27625b7d0 92 wait(1.5);//waits 1.5 seconds as to no make the display look all glitchy.
jEvans99 0:84b27625b7d0 93 }
jEvans99 0:84b27625b7d0 94 }
jEvans99 0:84b27625b7d0 95 /********************************************************************************
jEvans99 0:84b27625b7d0 96 keycode function:
jEvans99 0:84b27625b7d0 97 purpose: The purpose of this function is compare the value of the global array usercode to the correct codes specified by the PM. this function returns true if the code is one of the correct codes and will
jEvans99 0:84b27625b7d0 98 return false if the code is not correct.
jEvans99 0:84b27625b7d0 99
jEvans99 0:84b27625b7d0 100 Passed variables:
jEvans99 0:84b27625b7d0 101 None
jEvans99 0:84b27625b7d0 102
jEvans99 0:84b27625b7d0 103 Returned variables:
jEvans99 0:84b27625b7d0 104 status(bool)
jEvans99 0:84b27625b7d0 105
jEvans99 0:84b27625b7d0 106 Pins Declared:
jEvans99 0:84b27625b7d0 107 None
jEvans99 0:84b27625b7d0 108
jEvans99 0:84b27625b7d0 109 variables:
jEvans99 0:84b27625b7d0 110 status(bool): a boolean variable that will be set true if the keycode is correct, the variable will be set to false if the keycode is incorrect.
jEvans99 0:84b27625b7d0 111 usercode[]:A global array containing the user entered code.
jEvans99 0:84b27625b7d0 112
jEvans99 0:84b27625b7d0 113 functions:
jEvans99 0:84b27625b7d0 114 if(condition){}:the if statement will run the code contained in the curly brackets if the condition is met
jEvans99 0:84b27625b7d0 115 return:returns the variable or value to the function that this function was called from.
jEvans99 0:84b27625b7d0 116 ********************************************************************************/
jEvans99 0:84b27625b7d0 117 bool keycode()
jEvans99 0:84b27625b7d0 118 {
jEvans99 0:84b27625b7d0 119 bool status = false;//creates a boolean variable called status that is initialized to false
jEvans99 0:84b27625b7d0 120 if(usercode[0] == '1' && usercode[1] == '2' && usercode[2] == '3' && usercode[3] == '4'||//if usercode array is 1234 or 4321 or 0000
jEvans99 0:84b27625b7d0 121 usercode[0] == '4' && usercode[1] == '3' && usercode[2] == '2' && usercode[3] == '1'||
jEvans99 0:84b27625b7d0 122 usercode[0] == '0' && usercode[1] == '0' && usercode[2] == '0' && usercode[3] == '0')
jEvans99 0:84b27625b7d0 123 {
jEvans99 0:84b27625b7d0 124 status = true;//sets status to true
jEvans99 0:84b27625b7d0 125 }
jEvans99 0:84b27625b7d0 126 return status;//returns the value of status.
jEvans99 0:84b27625b7d0 127 }
jEvans99 0:84b27625b7d0 128 /*************************************************
jEvans99 0:84b27625b7d0 129 scan4by3keypad function:
jEvans99 0:84b27625b7d0 130 function: This function will loop through rows turning each row high and then reads the columns and if that column is high
jEvans99 0:84b27625b7d0 131 it can determine which button was pressed. This uses blocking polling.
jEvans99 0:84b27625b7d0 132
jEvans99 0:84b27625b7d0 133 variables:
jEvans99 0:84b27625b7d0 134 row (BusOut): creates the busout for my row pins
jEvans99 0:84b27625b7d0 135 col (BusOut): creates a busout for my column pins
jEvans99 0:84b27625b7d0 136 k (const char): an array containing the values of the buttons to be used when printing what button was pressed
jEvans99 0:84b27625b7d0 137 buttonpressed (char): a char buffer which stores the char value at the point in the k array depending what button is pressed.
jEvans99 0:84b27625b7d0 138 j (int): loop condition associated with my row pins
jEvans99 0:84b27625b7d0 139 isbuttonpressed (int): This int is the while loop condiition.
jEvans99 0:84b27625b7d0 140
jEvans99 0:84b27625b7d0 141 functions:
jEvans99 0:84b27625b7d0 142 pin->write(0b0001<<j): will write the byte to the pins specified and will shift depending on what value j is
jEvans99 0:84b27625b7d0 143 pin->read(): reads from the associated pin(s)
jEvans99 0:84b27625b7d0 144 printf(): prints to the terminal
jEvans99 0:84b27625b7d0 145 switch col->read(): will look at the case associated with what returns from the column read.
jEvans99 0:84b27625b7d0 146 while(condition): will keep looping what is inside of the loop until the condition is not met. This is used for my blocking polling.
jEvans99 0:84b27625b7d0 147 ****************************************************/
jEvans99 0:84b27625b7d0 148 char scan4by3keypad(BusIn* col, BusOut* row, const char k[4][3])
jEvans99 0:84b27625b7d0 149 { char buttonpressed;
jEvans99 0:84b27625b7d0 150 int isbuttonpressed=0;
jEvans99 0:84b27625b7d0 151 while(isbuttonpressed==0)
jEvans99 0:84b27625b7d0 152 {
jEvans99 0:84b27625b7d0 153 for(int j=0;j<4;j++)//loops j which will be used to turn the rows on
jEvans99 0:84b27625b7d0 154 {
jEvans99 0:84b27625b7d0 155 row->write(0b0001<<j);//writes a 1 to the jth pin
jEvans99 0:84b27625b7d0 156 switch (col->read())
jEvans99 0:84b27625b7d0 157 {
jEvans99 0:84b27625b7d0 158 case 0b001://if the first column is high
jEvans99 0:84b27625b7d0 159 buttonpressed = k[j][0];//equals the array at the jth and zero position.
jEvans99 0:84b27625b7d0 160 isbuttonpressed=1;//breaks the while loop
jEvans99 0:84b27625b7d0 161 break;
jEvans99 0:84b27625b7d0 162 case 0b010://if the second column is high
jEvans99 0:84b27625b7d0 163 buttonpressed = k[j][1];//equals the array at the jth and one position.
jEvans99 0:84b27625b7d0 164 isbuttonpressed=1;//breaks the while loop
jEvans99 0:84b27625b7d0 165 break;
jEvans99 0:84b27625b7d0 166 case 0b100://if the third column is high
jEvans99 0:84b27625b7d0 167 buttonpressed = k[j][2];//equals the array at the jth and second position
jEvans99 0:84b27625b7d0 168 isbuttonpressed=1;//breaks the while loop
jEvans99 0:84b27625b7d0 169 break;
jEvans99 0:84b27625b7d0 170 }
jEvans99 0:84b27625b7d0 171 row->write(0b0001<<j);//resets the jth pin low so there is no confusion as to what button is pressed
jEvans99 0:84b27625b7d0 172 }
jEvans99 0:84b27625b7d0 173 }
jEvans99 0:84b27625b7d0 174 return buttonpressed;//returns the char value stored in the array
jEvans99 0:84b27625b7d0 175 }
jEvans99 0:84b27625b7d0 176 /**********************************************************************************************************
jEvans99 0:84b27625b7d0 177 Description: The purpose of this function is to calculate the temperature being read by the thermistor
jEvans99 0:84b27625b7d0 178 In order for this code to work, you must connect a 10k resistor in series with the thermistor. The wire from A1 on the nucleo f411RE must be connected after the resistor. The voltage being used in this circuit is 3.3V.
jEvans99 0:84b27625b7d0 179 Below you will find a rough drawing of what the circuit should look like
jEvans99 0:84b27625b7d0 180
jEvans99 0:84b27625b7d0 181 3.3V---------Resistor---wire from A1-------Thermistor-------GND
jEvans99 0:84b27625b7d0 182
jEvans99 0:84b27625b7d0 183 Parameters:
jEvans99 0:84b27625b7d0 184
jEvans99 0:84b27625b7d0 185 temp->Defines the analog input pin used to measure the voltage of the thermistor
jEvans99 0:84b27625b7d0 186
jEvans99 0:84b27625b7d0 187
jEvans99 0:84b27625b7d0 188 Returns:
jEvans99 0:84b27625b7d0 189 celcius: Returns the tempurature measured by the thermistor in degrees celsius as a float value
jEvans99 0:84b27625b7d0 190 **********************************************************************************************************/
jEvans99 0:84b27625b7d0 191 float readtempurature(AnalogIn *temp)
jEvans99 0:84b27625b7d0 192 {
jEvans99 0:84b27625b7d0 193 float tempVal; //variable that stores the voltage reading of the thermistor as a value between 1 and zero
jEvans99 0:84b27625b7d0 194 tempVal=temp->read(); //reads the voltage of the thermistor on analog pin 1 of the nucleo f411RE
jEvans99 0:84b27625b7d0 195 float vrt; //variable that stores the actual value of the voltage of the thermistor
jEvans99 0:84b27625b7d0 196 vrt=(tempVal*3.3);//this formula determines the actual voltage of the thermistor
jEvans99 0:84b27625b7d0 197 float top;//variable that defines the numerator of the formula to determine the resistance of the thermistor
jEvans99 0:84b27625b7d0 198 top=(vrt*10000);//numerator of the formula used to determine the thermisistor resistance
jEvans99 0:84b27625b7d0 199 float bottom; //variable that defines the denominator of the formula to determine the resistance of the thermistor
jEvans99 0:84b27625b7d0 200 bottom=(3.3-vrt); //denominator of the formula to determine the thermisistor resistance
jEvans99 0:84b27625b7d0 201 float rt=(top/bottom);//this is the formula to determine the thermisitor resistance
jEvans99 0:84b27625b7d0 202 float A=(3.354016e-3);//Constant A1 in the formula used to calculate the temperature given the resistance of the thermisitor
jEvans99 0:84b27625b7d0 203 float B=(2.569650e-4);//Constant B1 in the formula used to calculate the temperature given the resistance of the thermisitor
jEvans99 0:84b27625b7d0 204 float C=(2.620131e-6);//Constant C1 in the formula used to calculate the temperature given the resistance of the thermisitor
jEvans99 0:84b27625b7d0 205 float D=(6.383091e-8);//Constant D1 in the formula used to calculate the temperature given the resistance of the thermisitor
jEvans99 0:84b27625b7d0 206 float ln1=(log(rt/10000)); //First ln operation in the formula used to calculate the temperature given the resistance of the thermisitor
jEvans99 0:84b27625b7d0 207 float ln2=log(pow((rt/10000),2));//Second ln operation in the formula used to calculate the temperature given the resistance of the thermisitor
jEvans99 0:84b27625b7d0 208 float ln3=log(pow((rt/10000),3));//Second ln operation in the formula used to calculate the temperature given the resistance of the thermisitor
jEvans99 0:84b27625b7d0 209 float denominator=(A+(B*ln1)+(C*ln2)+(D*ln3)); //complete denominator of the formula used to calculate the temperature given the resistance of the thermisitor
jEvans99 0:84b27625b7d0 210 float celcius=((1/denominator)-273.15);//completed formula to calculate the tempurature given the resistance of the thermisitor
jEvans99 0:84b27625b7d0 211
jEvans99 0:84b27625b7d0 212 return celcius; //returns the value of the tempurature in degrees celsius
jEvans99 0:84b27625b7d0 213 }