Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
lab10.cpp
00001 #include "mbed.h" 00002 #include "TextLCD.h" 00003 BusOut leds(D14,D15);//creates a global busout that identifies the pins associated with the leds. 00004 bool keycode();//Function prototype for the function that checks if the correct keycode was entered(returns boolean) 00005 float readtempurature(AnalogIn *temp);//function prototype fot the readtempurature function(this function was done by ben) 00006 char scan4by3keypad(BusIn* col, BusOut* row, const char k[4][3]); //function prototype for the scan4by3keypad function(returns a char) 00007 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 00008 char usercode[4] = {' ',' ',' ',' '};//creates a global char array that is used to store the entered code. This array is initialized to nothing 00009 /************************************************************************************ 00010 Main function: 00011 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 00012 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. 00013 00014 Pins declared: 00015 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. 00016 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. 00017 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. 00018 00019 Variables: 00020 button(const char): a 4 by 3 array that is used to store the char equivalent to the keys on the keypad. 00021 access(bool): a boolean variable that is true if the code entered is correct, and is false if the code entered is incorrect. 00022 00023 Functions: 00024 lcd.cls(): a function of the lcd class that will clear the lcd display 00025 lcd.printf(""):a function of the lcd class that will print to the lcd display 00026 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. 00027 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 00028 the button pressed. 00029 wait(x): waits x amount of seconds then continues with the function. 00030 if(condition){}:the if statement will run the code contained in the curly brackets if the condition is met 00031 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 00032 leds[x].write(y):writes y(either a 1 or 0) to the pin at the position x in the leds busout. 00033 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. 00034 for(;;){}: runs the code contained in the brackets forever and ever. 00035 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. 00036 *************************************************************************************/ 00037 int main() { 00038 AnalogIn temp (A1);//indentifies the pin A1 as an analogin pin named temp 00039 BusOut rowpins(D13,D12,D11,D10);//busout for my row pins 00040 BusIn columnpins(D9,D8,D7);//busin for my colummn pins 00041 columnpins.mode(PullDown);//enables the internal pulldown for the column pins 00042 const char button[4][3]= {{'1','2','3'}, 00043 {'4','5','6'}, 00044 {'7','8','9'}, 00045 {'4','0','#'}};//array which helps determine which button was 00046 bool access = false;//boolean variable that will change depending on whether the keycode is correct or incorrect. 00047 while(access == false)//loops through this code while the access is false (keycode entered is wrong) 00048 { 00049 lcd.cls();//clears the LCD display 00050 lcd.printf("Enter Code:\n");//prints "enter code" to the LCD display. 00051 lcd.locate(11,0);//moves the cursor to the 11th column of the 0th row on the lcd display 00052 usercode[0] = scan4by3keypad(&columnpins, &rowpins, button);//sets the first value of usercode to the char returned by the scan4by3keypad function. 00053 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 00054 wait(0.5);//waits half a second 00055 usercode[1] = scan4by3keypad(&columnpins, &rowpins, button);//sets the second value of usercode to the char returned by the scan4by3keypad function. 00056 lcd.locate(12,0);//moves the cursor to the 12th column of the 0th row on the lcd display 00057 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 00058 wait(0.5);//waits half a second 00059 usercode[2] = scan4by3keypad(&columnpins, &rowpins, button);//sets the third value of usercode to the char returned by the scan4by3keypad function. 00060 lcd.locate(13,0);//moves the cursor to the 13th column of the 0th row on the lcd display 00061 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 00062 wait(0.5);//waits half a second. 00063 usercode[3] = scan4by3keypad(&columnpins, &rowpins, button);//sets the fourth value of usercode to the char returned by the scan4by3keypad function. 00064 lcd.locate(14,0);//moves the cursor to the 14th column of the 0th row on the lcd display 00065 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 00066 wait(1);//waits one second 00067 if(keycode() == true)//if the keycode is correct this code will run (breaks the loop) 00068 { 00069 lcd.cls();//clears the lcd completely 00070 lcd.locate(0,0);//moves the cursor to the 0th column of the 0th row of the LCD 00071 lcd.printf("Access granted!\n");//prints "Access granted" to the LCD 00072 leds[0].write(1);//writes 1 to the 0th value of the leds busout. (green LED) 00073 wait(1);//waits one second 00074 leds[0].write(0);//writes 0 to the 0th value of the leds busout. (green LED) 00075 access = true;//sets access to true which breaks the while loop. 00076 } 00077 else//if the keycode is not correct this code will run (does not break the loop) 00078 { 00079 lcd.cls();//clears the lcd completely 00080 lcd.locate(0,0);//moves the cursor to the 0th column of the 0th row of the LCD 00081 lcd.printf("Access Denied!");//prints "Access Denied!" to the LCD display 00082 leds[1].write(1);//writes 1 to the 1st value of the leds busout(red led) 00083 wait(1);//waits one second 00084 leds[1].write(0);//writes 0 to the 1st value of the leds busout(red led) 00085 00086 } 00087 } 00088 for(;;)//infinite loop 00089 { 00090 lcd.locate(0,1);//moves the cursor to the 0th column of the first row on the LCD 00091 lcd.printf("Temp: %.2f",readtempurature(&temp));//prints the value returns by the readtempurature function.(.2f will cutoff the value after the second decimal place. 00092 wait(1.5);//waits 1.5 seconds as to no make the display look all glitchy. 00093 } 00094 } 00095 /******************************************************************************** 00096 keycode function: 00097 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 00098 return false if the code is not correct. 00099 00100 Passed variables: 00101 None 00102 00103 Returned variables: 00104 status(bool) 00105 00106 Pins Declared: 00107 None 00108 00109 variables: 00110 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. 00111 usercode[]:A global array containing the user entered code. 00112 00113 functions: 00114 if(condition){}:the if statement will run the code contained in the curly brackets if the condition is met 00115 return:returns the variable or value to the function that this function was called from. 00116 ********************************************************************************/ 00117 bool keycode() 00118 { 00119 bool status = false;//creates a boolean variable called status that is initialized to false 00120 if(usercode[0] == '1' && usercode[1] == '2' && usercode[2] == '3' && usercode[3] == '4'||//if usercode array is 1234 or 4321 or 0000 00121 usercode[0] == '4' && usercode[1] == '3' && usercode[2] == '2' && usercode[3] == '1'|| 00122 usercode[0] == '0' && usercode[1] == '0' && usercode[2] == '0' && usercode[3] == '0') 00123 { 00124 status = true;//sets status to true 00125 } 00126 return status;//returns the value of status. 00127 } 00128 /************************************************* 00129 scan4by3keypad function: 00130 function: This function will loop through rows turning each row high and then reads the columns and if that column is high 00131 it can determine which button was pressed. This uses blocking polling. 00132 00133 variables: 00134 row (BusOut): creates the busout for my row pins 00135 col (BusOut): creates a busout for my column pins 00136 k (const char): an array containing the values of the buttons to be used when printing what button was pressed 00137 buttonpressed (char): a char buffer which stores the char value at the point in the k array depending what button is pressed. 00138 j (int): loop condition associated with my row pins 00139 isbuttonpressed (int): This int is the while loop condiition. 00140 00141 functions: 00142 pin->write(0b0001<<j): will write the byte to the pins specified and will shift depending on what value j is 00143 pin->read(): reads from the associated pin(s) 00144 printf(): prints to the terminal 00145 switch col->read(): will look at the case associated with what returns from the column read. 00146 while(condition): will keep looping what is inside of the loop until the condition is not met. This is used for my blocking polling. 00147 ****************************************************/ 00148 char scan4by3keypad(BusIn* col, BusOut* row, const char k[4][3]) 00149 { char buttonpressed; 00150 int isbuttonpressed=0; 00151 while(isbuttonpressed==0) 00152 { 00153 for(int j=0;j<4;j++)//loops j which will be used to turn the rows on 00154 { 00155 row->write(0b0001<<j);//writes a 1 to the jth pin 00156 switch (col->read()) 00157 { 00158 case 0b001://if the first column is high 00159 buttonpressed = k[j][0];//equals the array at the jth and zero position. 00160 isbuttonpressed=1;//breaks the while loop 00161 break; 00162 case 0b010://if the second column is high 00163 buttonpressed = k[j][1];//equals the array at the jth and one position. 00164 isbuttonpressed=1;//breaks the while loop 00165 break; 00166 case 0b100://if the third column is high 00167 buttonpressed = k[j][2];//equals the array at the jth and second position 00168 isbuttonpressed=1;//breaks the while loop 00169 break; 00170 } 00171 row->write(0b0001<<j);//resets the jth pin low so there is no confusion as to what button is pressed 00172 } 00173 } 00174 return buttonpressed;//returns the char value stored in the array 00175 } 00176 /********************************************************************************************************** 00177 Description: The purpose of this function is to calculate the temperature being read by the thermistor 00178 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. 00179 Below you will find a rough drawing of what the circuit should look like 00180 00181 3.3V---------Resistor---wire from A1-------Thermistor-------GND 00182 00183 Parameters: 00184 00185 temp->Defines the analog input pin used to measure the voltage of the thermistor 00186 00187 00188 Returns: 00189 celcius: Returns the tempurature measured by the thermistor in degrees celsius as a float value 00190 **********************************************************************************************************/ 00191 float readtempurature(AnalogIn *temp) 00192 { 00193 float tempVal; //variable that stores the voltage reading of the thermistor as a value between 1 and zero 00194 tempVal=temp->read(); //reads the voltage of the thermistor on analog pin 1 of the nucleo f411RE 00195 float vrt; //variable that stores the actual value of the voltage of the thermistor 00196 vrt=(tempVal*3.3);//this formula determines the actual voltage of the thermistor 00197 float top;//variable that defines the numerator of the formula to determine the resistance of the thermistor 00198 top=(vrt*10000);//numerator of the formula used to determine the thermisistor resistance 00199 float bottom; //variable that defines the denominator of the formula to determine the resistance of the thermistor 00200 bottom=(3.3-vrt); //denominator of the formula to determine the thermisistor resistance 00201 float rt=(top/bottom);//this is the formula to determine the thermisitor resistance 00202 float A=(3.354016e-3);//Constant A1 in the formula used to calculate the temperature given the resistance of the thermisitor 00203 float B=(2.569650e-4);//Constant B1 in the formula used to calculate the temperature given the resistance of the thermisitor 00204 float C=(2.620131e-6);//Constant C1 in the formula used to calculate the temperature given the resistance of the thermisitor 00205 float D=(6.383091e-8);//Constant D1 in the formula used to calculate the temperature given the resistance of the thermisitor 00206 float ln1=(log(rt/10000)); //First ln operation in the formula used to calculate the temperature given the resistance of the thermisitor 00207 float ln2=log(pow((rt/10000),2));//Second ln operation in the formula used to calculate the temperature given the resistance of the thermisitor 00208 float ln3=log(pow((rt/10000),3));//Second ln operation in the formula used to calculate the temperature given the resistance of the thermisitor 00209 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 00210 float celcius=((1/denominator)-273.15);//completed formula to calculate the tempurature given the resistance of the thermisitor 00211 00212 return celcius; //returns the value of the tempurature in degrees celsius 00213 }
Generated on Wed Oct 23 2024 12:45:41 by
1.7.2