0% found this document useful (0 votes)
107 views3 pages

Something

This Arduino code defines functions to read temperature and humidity sensor data using a DHT22 sensor, display the readings on an LCD screen, and allow navigation between screens and parameter adjustment using buttons. It initializes the LCD and DHT22 sensor, defines input and menu logic, reads the sensor values, displays the readings, and handles input flags and navigation/adjustment functions.

Uploaded by

Ella Anaida
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
107 views3 pages

Something

This Arduino code defines functions to read temperature and humidity sensor data using a DHT22 sensor, display the readings on an LCD screen, and allow navigation between screens and parameter adjustment using buttons. It initializes the LCD and DHT22 sensor, defines input and menu logic, reads the sensor values, displays the readings, and handles input flags and navigation/adjustment functions.

Uploaded by

Ella Anaida
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

#include <Wire.

h>
#include <LiquidCrystal_I2C.h>
#include "DHT.h"

#define DHTPIN 2
#define DHTTYPE DHT22

DHT dht(DHTPIN, DHTTYPE);


LiquidCrystal_I2C lcd(0x27, 20, 4); // Set the LCD address to 0x27 for a 16 chars
and 2 line display

float h;
float t;

//Input & Button Logic


const int numOfInputs = 4;
const int inputPins[numOfInputs] = {8,9,10,11};
int inputState[numOfInputs];
int lastInputState[numOfInputs] = {LOW,LOW,LOW,LOW};
bool inputFlags[numOfInputs] = {LOW,LOW,LOW,LOW};
long lastDebounceTime[numOfInputs] = {0,0,0,0};
long debounceDelay = 5;

//LCD Menu Logic


const int numOfScreens = 2;
int currentScreen = 0;
String screens[numOfScreens][2] = {{"Temperature","'C"}, {"Humidity", "%"}};
int parameters[numOfScreens];

void setup() {

for(int i = 0; i < numOfInputs; i++) {


pinMode(inputPins[i], INPUT);
digitalWrite(inputPins[i], HIGH); // pull-up 20k
}
//Serial.begin(9600);
lcd.init();
lcd.backlight();
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Setup...");
delay (1000);
lcd.clear();
}

void loop() {

static unsigned long int sensorTime=0;


if(sensorTime == 0) { // start sensor read
startDHT22();
sensorTime=millis();
}
else
if(millis()-sensorTime > 10) // 10mSec
{
readDHT22(); // read sensor
sensorTime=0;
}

setInputFlags();
resolveInputFlags();
startDHT22();
readDHT22();
displayReadings();

void startDHT22(){
dht.begin();
}

void readDHT22() {
float h = dht.readHumidity();
float t = dht.readTemperature();
}

void displayReadings() {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Humidity: ");
lcd.setCursor(0, 1);
lcd.print((h)," %\t");
lcd.setCursor(0, 2);
lcd.print("Temperature: ");
lcd.setCursor(0, 3);
lcd.print((t)," *C ");
}

void setInputFlags() {
for(int i = 0; i < numOfInputs; i++) {
int reading = digitalRead(inputPins[i]);
if (reading != lastInputState[i]) {
lastDebounceTime[i] = millis();
}
if ((millis() - lastDebounceTime[i]) > debounceDelay) {
if (reading != inputState[i]) {
inputState[i] = reading;
if (inputState[i] == HIGH) {
inputFlags[i] = HIGH;
}
}
}
lastInputState[i] = reading;
}
}

void resolveInputFlags() {
for(int i = 0; i < numOfInputs; i++) {
if(inputFlags[i] == HIGH) {
inputAction(i);
inputFlags[i] = LOW;
printScreen();
}
}
}
void inputAction(int input) {
if(input == 0) {
if (currentScreen == 0) {
currentScreen = numOfScreens-1;
}else{
currentScreen--;
}
}else if(input == 1) {
if (currentScreen == numOfScreens-1) {
currentScreen = 0;
}else{
currentScreen++;
}
}else if(input == 2) {
parameterChange(0);
}else if(input == 3) {
parameterChange(1);
}
}

void parameterChange(int key) {


if(key == 0) {
parameters[currentScreen]++;
}else if(key == 1) {
parameters[currentScreen]--;
}
}

void printScreen() {
lcd.clear();
lcd.print(screens[currentScreen][0]);
lcd.setCursor(0,1);
lcd.print(parameters[currentScreen]);
lcd.print(" ");
lcd.print(screens[currentScreen][1]);
}

You might also like