Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <Arduino.h>
- #include <LiquidCrystal.h>
- #include <Adafruit_Sensor.h>
- #include <DHT.h>
- #include <string>
- #define DHTTYPE DHT11 // DHT 11
- #define DHTPIN 5
- #define ADCPIN 0
- /**To access sensor readings, it is necessary to instantiate a communication class that binds the
- hardware to the code
- */
- DHT dht(DHTPIN, DHTTYPE,50);
- /*To initialize it and instantiate controller object, following GPIO definition and initialization was used
- */
- const int rs = 12, en = 13, d4 = 4, d5 = 0, d6 = 2, d7 = 14;
- LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
- int nAdcReading = 0;
- void printNames();
- void measures();
- void buttonDecode();
- /**
- * Arduino function which runs once and sets everything up
- */
- void setup()
- {
- Serial.begin(9600); // open serial communication, 9600 is the default speed
- lcd.begin(16, 2); //init LCD (16 is the width, 2 is height, in bits)
- dht.begin(); // It is necessary to initialize the sensor class and give it some time to set up:
- delay(1000); //delay
- //printNames(); //invoke function from exercise 1
- }
- /**
- * Arduino function in which code which will be ran repeatedly is placed
- */
- void loop()
- {
- //measures(); //invoke function from exercise 2
- //buttonDecode(); //invoke function from exercise 3
- }
- //Task 1
- void printNames()
- {
- lcd.print("Marek Kawalski"); //print name in the first line
- lcd.setCursor(0, 1); //change the cursor position to the second line
- lcd.print("Paulina Mateusz"); //print another names
- }
- //Task 2
- void measures()
- {
- float hum = dht.readHumidity(); //read humidity from sensor
- float temp = dht.readTemperature(); //read temperature from sensor
- char buffer [50]; //create a buffer
- lcd.setCursor(0, 0); //set cursor in the first
- sprintf(buffer, "Temp.: %2.1f C", temp); //store temperature in buffer
- lcd.print(buffer); //print buffer content
- lcd.setCursor(0, 1); //set cursor to the next line
- sprintf(buffer, "Hum.: %2.1f%%Rh", hum); //store relative humidity in buffer
- lcd.print(buffer); //print buffer content
- delay(1000); //make a delay so as to make output visible
- }
- //Task 3
- void buttonDecode()
- {
- int nAD0 = analogRead(ADCPIN); //read from pin
- lcd.setCursor(0, 0); //set cursor to the first line
- lcd.print(nAD0); //print read value from nAD0
- lcd.setCursor(0, 1); //set cursor to the second line
- if(nAD0 >= 750 && nAD0 <= 770)
- lcd.print("left"); //if read value ranges between 750 and 770 button left was pressed
- else if(nAD0 >= 10 && nAD0 <= 20)
- lcd.print("right"); //if read value ranges between 10 and 20 button right was pressed
- else if(nAD0 >= 220 && nAD0 <= 240)
- lcd.print("up"); //if read value ranges between 220 and 240 button left up was pressed
- else if(nAD0 >= 490 && nAD0 <= 500)
- lcd.print("down"); //if read value ranges between 490 and 500 button down was pressed
- delay(5000); //set a delay
- lcd.clear(); // clear the display
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement