Advertisement
MateuszGrabarczyk

LabArduino

Dec 11th, 2022
347
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <Arduino.h>
  2. #include <LiquidCrystal.h>
  3. #include <Adafruit_Sensor.h>
  4. #include <DHT.h>
  5. #include <string>
  6.  
  7. #define DHTTYPE DHT11   // DHT 11
  8. #define DHTPIN 5
  9. #define ADCPIN 0
  10.  
  11. /**To access sensor readings, it is necessary to instantiate a communication class that binds the
  12. hardware to the code
  13. */
  14. DHT dht(DHTPIN, DHTTYPE,50);
  15.  
  16. /*To initialize it and instantiate controller object, following GPIO definition and initialization was used
  17. */
  18. const int rs = 12, en = 13, d4 = 4, d5 = 0, d6 = 2, d7 = 14;
  19. LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
  20. int nAdcReading = 0;
  21. void printNames();
  22. void measures();
  23. void buttonDecode();
  24.  
  25. /**
  26. * Arduino function which runs once and sets everything up
  27. */
  28. void setup()
  29. {
  30. Serial.begin(9600); // open serial communication, 9600 is the default speed
  31. lcd.begin(16, 2);  //init LCD (16 is the width, 2 is height, in bits)
  32. dht.begin(); // It is necessary to initialize the sensor class and give it some time to set up:
  33. delay(1000); //delay
  34. //printNames(); //invoke function from exercise 1
  35. }
  36.  
  37.  
  38.  
  39.  
  40. /**
  41. * Arduino function in which code which will be ran repeatedly is placed
  42. */
  43. void loop()
  44. {
  45.     //measures();  //invoke function from exercise 2
  46. //buttonDecode(); //invoke function from exercise 3
  47.  
  48. }
  49.  
  50. //Task 1
  51. void printNames()
  52. {
  53. lcd.print("Marek Kawalski"); //print name in the first line
  54. lcd.setCursor(0, 1); //change the cursor position to the second line
  55. lcd.print("Paulina Mateusz"); //print another names
  56. }
  57.  
  58. //Task 2
  59. void measures()
  60. {
  61.   float hum = dht.readHumidity(); //read humidity from sensor
  62.   float temp = dht.readTemperature(); //read temperature from sensor
  63.   char buffer [50];  //create a buffer
  64.  
  65.   lcd.setCursor(0, 0); //set cursor in the first
  66.   sprintf(buffer, "Temp.: %2.1f C", temp); //store temperature in buffer
  67.   lcd.print(buffer); //print buffer content
  68.  
  69.   lcd.setCursor(0, 1); //set cursor to the next line
  70.   sprintf(buffer, "Hum.: %2.1f%%Rh", hum);  //store relative humidity in buffer
  71.   lcd.print(buffer); //print buffer content
  72.   delay(1000); //make a delay so as to make output visible
  73. }
  74.  
  75. //Task 3
  76. void buttonDecode()
  77. {
  78.   int nAD0 = analogRead(ADCPIN); //read from pin
  79.  
  80.   lcd.setCursor(0, 0); //set cursor to the first line
  81.   lcd.print(nAD0); //print read value from nAD0
  82.  
  83.   lcd.setCursor(0, 1); //set cursor to the second line
  84.   if(nAD0 >= 750 && nAD0 <= 770)
  85.         lcd.print("left"); //if read value ranges between 750 and 770 button left was pressed
  86.   else if(nAD0 >= 10 && nAD0 <= 20)
  87.         lcd.print("right"); //if read value ranges between 10 and 20 button right was pressed
  88.   else if(nAD0 >= 220 && nAD0 <= 240)
  89.         lcd.print("up"); //if read value ranges between 220 and 240 button left up was pressed
  90.   else if(nAD0 >= 490 && nAD0 <= 500)
  91.         lcd.print("down"); //if read value ranges between 490 and 500 button down was pressed
  92.  
  93.   delay(5000); //set a delay
  94.   lcd.clear(); // clear the display
  95. }
  96.  
  97.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement