WeatherSnap

Virtual CodeDay Winter 2021 ∙ 
Permalink
Unmute
It is an app that will tell you the temperature and humidity based on pressing a button and clapping.

How much experience does your group have? Does the project use anything (art, music, starter kits) you didn't create?

I have zero experience in Arduino, and actually got help from my dad.
Code:
#include <U8g2lib.h>
#include "DHT.h"

#define DHTPIN 3 // what pin we're connected to

// #define INCLUDE_LCD 1

// Uncomment whatever type you're using!
#define DHTTYPE DHT11 // DHT 11
char led = 4;
char button = 6;
const int pinAdc = A2;
int x = 0;
float temp_hum_val[2] = {0};
String to_display;

//#define DHTTYPE DHT10

// Connect pin 1 (on the left) of the sensor to +5V
// Connect pin 2 of the sensor to whatever your DHTPIN is
// Connect pin 4 (on the right) of the sensor to GROUND
// Connect a 10K resistor from pin 2 (data) to pin 1 (power) of the sensor

DHT dht(DHTPIN, DHTTYPE);

#ifdef INCLUDE_LCD
U8G2_SSD1306_128X64_NONAME_F_HW_I2C u8g2(U8G2_R2, SCL, SDA, U8X8_PIN_NONE);
#endif

#define debug Serial

void setup() {
#ifdef INCLUDE_LCD
u8g2.begin();
#endif
pinMode(led, OUTPUT);
pinMode(button, INPUT);
debug.begin(9600);
debug.println("Press the button(D6) to get temperature and clap/snap to get humidity!");
Wire.begin();
dht.begin();
}

#ifdef INCLUDE_LCD
void displayLCD() {
u8g2.clearBuffer(); // clear the internal memory
u8g2.setFont(u8g2_font_ncenB08_tr); // choose a suitable font
u8g2.drawStr(0, 10, to_display.c_str()); // write something to the internal memory
u8g2.sendBuffer(); // transfer internal memory to the display
delay(1000);
}
#endif

void loop() {
// Reading temperature or humidity takes about 250 milliseconds!
// Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
// if (x == 0){
// displayLCD("Welcome");
// }
int err;
err = dht.readTempAndHumidity(temp_hum_val);

if (digitalRead(button) == HIGH) {
digitalWrite(led, HIGH);
if (err == 0) {
// debug.print("Humidity: ");
// debug.print(temp_hum_val[0]);
// debug.print(" %\t");
debug.print("Temperature: ");
debug.print(temp_hum_val[1]);
debug.println(" *C");
to_display = String(temp_hum_val[1]);
} else {
debug.print("Failed to get temprature and humidity value. Error =");
debug.println(err);
}

}
else {
digitalWrite(led, LOW);
}
long sum = 0;
for (int i = 0; i < 128; i++) {
sum += analogRead(pinAdc);
}

sum >>= 5;

if (sum > 1625) {
// debug.print("sum = ");
// debug.print(sum);
// debug.println(" SNAP or CLAP");
// err = dht.readTempAndHumidity(temp_hum_val[0]);
debug.print("Humidity: ");
debug.println(temp_hum_val[0]);
to_display = String(temp_hum_val[0]);
}
#ifdef INCLUDE_LCD
displayLCD();
#endif
// Serial.println(sum);
delay(10);
}
0
0
0
0
 
Participation Certificate

Members

Saketh