Skip to content

Instantly share code, notes, and snippets.

@john212
Last active December 22, 2015 08:01
Show Gist options
  • Save john212/dd1d7f74fec0bda75ecd to your computer and use it in GitHub Desktop.
Save john212/dd1d7f74fec0bda75ecd to your computer and use it in GitHub Desktop.
/**************************************************
Udemy Arduino Step-by-Step Course
Section 3 Interactive Lecture 27 Keypad Part 5 Quiz
by J. Cantlin
December 21, 2015
***************************************************/
/**************************************************
Summary of Arduino Analog Pins Used:
A0 = 4x4 Keypad
A1 = LDR Light Dependent Resistor
A2 =
A3 =
A4 =
A5 =
***************************************************/
/**************************************************
Summary of Arduino Digital Pins Used:
00 =
01 =
02 = DHT11 Temperature-Humidity Sensor
03 =
04 =
05 =
06 =
07 = 1602 LCD Pin 4 RS (Register Select)
08 = 1602 LCD Pin 6 EN (Enable Signal)
09 = 1602 LCD Pin 11 DB4 (Data Bus Line 4)
10 = 1602 LCD Pin 12 DB4 (Data Bus Line 5)
11 = 1602 LCD Pin 13 DB4 (Data Bus Line 6)
13 = 1602 LCD Pin 14 DB4 (Data Bus Line 7)
***************************************************/
/**************************************************
DHT11 Temperature-Humidy Sensor
Uses library DHT.h
Must define DHT sensor type, DHT11, DHT21, DHT22 also.
Call the library using DHT dht(pin_used,dht_sensor_type).
External Pull Up Resistor:
The DHT11 needs a 5k ohm pullup resistor, connect it
between the 5v power and the signal line. The
Arduino has internal pull-ups which could be used,
but their exact value in unknown. (10k to 40k?) so
use the known 5k external pull up resistor instead.
***************************************************/
#include "DHT.h"
#define DHTPIN 2 //analog pin DHT sensor uses
#define DHTTYPE DHT11 //type of sensor
DHT dht(DHTPIN, DHTTYPE); //call DHT library
/**************************************************/
/**************************************************
1602 LCD Display - 16 Characters-Two Lines
This sketch is used with an Adafruit P-tec P-1602-17 16x2 LCD.
The LiquidCrystal library works with all LCD displays that are
compatible with the Hitachi HD44780 driver. This is a
common 16x2 display and uses a 16-pin interface. The
LiquidCrystal Library is modified "Hello World"
from the Arduino Reference Library.
Summary of 1602 Pins Used:
01 = Ground (-)
02 = 5vdc (+)
03 = Conrast - wiper arm of 10k ohm pot (5vdc)
04 = RS (Register Select)
05 = GROUND (no display unless pin 5 grounded)
06 = EN or E (Enable Signal)
07 =
08 =
09 =
10 =
11 = DB4 (Data Bus Line 4)
12 = DB5 (Data Bus Line 5)
13 = DB6 (Data Bus Line 6)
14 = DB7 (Data Bus Line 7)
15 = Backlight Power (+ 5vdc)
16 = Backlight Ground (-)
***************************************************/
#include <LiquidCrystal.h> //LCD library for 16x2 text LCD
LiquidCrystal lcd(7, 8, 9, 10, 11, 12); //define Arduino pins used with LCD
/**************************************************/
//define temperature and humidy ralated variables
float h = 0.0;
float t = 0.0;
float t_degF = 0.0;
//define LDR - light dependent resistor ralated variables
int ldr = 0;
//define keypad related variables
char* keypressed = 0; //the * is a pointer.
int keyboardPin = 0; // Analog input pin that the keypad is attached to
int keyboardValue = 0; // value read from the keyboard
void setup()
{//***start setup function****
dht.begin(); //start dht function.
lcd.begin(16, 2); //start LCD with 16 colums 2 rows
Serial.begin(9600); //hardware serial to PC
lcd.print("SBS Quiz"); //print initial message to LCD
delay(100); //initial delay in millisec to read display, let circuit settle.
}//***endof setup function****
void loop()
{//***start infinite loop****
keyboardValue = analogRead(A0); // read the keyboard value (0 - 1023)
//do nothing until a key is pressed
while (keyboardValue < 25)
{//****start while no key pressed loop****
// Reading temperature or humidity takes about 250 milliseconds!
// Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
h = dht.readHumidity();
t = dht.readTemperature();
t_degF = 1.8*t+32.0;
ldr = analogRead(A1);
keyboardValue = analogRead(A0);
delay(50);
}//****endof while no key pressed loop****
//when a key is pressed get its value
readkeyboard(); //get the value of key being pressed "keypressed" i.e. 0-9
}//***endof infinite loop****
void readkeyboard()
{//***start read keyboard function****
keyboardValue = analogRead((A0)); // read the value (0-1023)
Serial.print("Pin value:");
Serial.println( keyboardValue);
if (keyboardValue <25){keypressed = "n";} //measured 0.0v
if ((keyboardValue >870) && (keyboardValue < 878))
{
keypressed = "1"; //Keypad 1 pressed
lcd.clear(); //clear LCD
lcd.setCursor(0, 0); //move cursor to start of first row
lcd.print("Temp. deg F"); //print title on first row
lcd.setCursor(0, 1); //move cursor to start of second row.
lcd.print(t_degF,DEC); //DEC means base ten decimal
delay(200);
}
if ((keyboardValue >763) && (keyboardValue < 771))
{
keypressed = "2"; //Keypad 2 pressed
lcd.clear(); //clear LCD
lcd.setCursor(0, 0); //move cursor to start of first row
lcd.print("Humidity %"); //print title on first row
lcd.setCursor(0, 1); //move cursor to start of second row.
lcd.print(h,DEC); //DEC means base ten decimal
delay(200);
}
if ((keyboardValue >659) && (keyboardValue < 667))
{keypressed = "3"; //Keypad 3 pressed
lcd.clear(); //clear LCD
lcd.setCursor(0, 0); //move cursor to start of first row
lcd.print("Light Intensity");//print title on first row
lcd.setCursor(0, 1); //move cursor to start of second row.
lcd.print(ldr,DEC); //DEC means base ten decimal
delay(200);
}
//if ((keyboardValue >612) && (keyboardValue < 616)){keypressed = "A";}
//if ((keyboardValue >534) && (keyboardValue < 538)){keypressed = "4";}
//if ((keyboardValue >492) && (keyboardValue < 496)){keypressed = "5";}
//if ((keyboardValue >447) && (keyboardValue < 451)){keypressed = "6";}
//if ((keyboardValue >421) && (keyboardValue < 425)){keypressed = "B";}
//if ((keyboardValue >361) && (keyboardValue < 365)){keypressed = "7";}
if ((keyboardValue >337) && (keyboardValue < 345))
{
keypressed = "8"; //Keypad 8 pressed
lcd.clear(); //clear LCD
lcd.setCursor(0, 0); //move cursor to start of first row
lcd.print("Hello from"); //print message on first row
lcd.setCursor(0, 1); //move cursor to start of second row.
lcd.print("John Cantlin"); //print message on first row
delay(200);
}
//if ((keyboardValue >319) && (keyboardValue < 323)){keypressed = "9";}
//if ((keyboardValue >306) && (keyboardValue < 310)){keypressed = "C";}
//if ((keyboardValue >279) && (keyboardValue < 283)){keypressed = "*";}
//if ((keyboardValue >266) && (keyboardValue < 270)){keypressed = "0";}
//if ((keyboardValue >252) && (keyboardValue < 256)){keypressed = "#";}
//if ((keyboardValue >245) && (keyboardValue < 249)){keypressed = "D";}
//if (keyboardValue >950){keypressed = "e";}
while (keyboardValue > 25)
{
delay (100);
keyboardValue = analogRead((A0)); // read the value (0-1023)
}//wait until key no longer being pressed before continuing
Serial.println(keypressed); // print the value back to the Serial view window on your PC
delay(500); // delay milliseconds before the next loop
}//***endof read keyboard function****
@john212
Copy link
Author

john212 commented Dec 22, 2015

This quiz coding project is not for credit and is not graded, it is to help other students and the instructor suggested I post the code on Gist. :-)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment