major changes

- switched to the correct DHT library
- cleaned out cruft
- got more light sensors connected
- display on the LCD

Signed-off-by: Vincent Batts <vbatts@hashbangbash.com>
This commit is contained in:
Vincent Batts 2024-08-03 19:36:25 -04:00
parent 66ab550699
commit bb0c226c31
Signed by: vbatts
GPG key ID: E30EFAA812C6E5ED

View file

@ -1,22 +1,30 @@
#include <Wire.h> #include <Wire.h>
#include <LiquidCrystal_I2C.h> #include <LiquidCrystal_I2C.h>
#include <DHT.h> #include <dht11.h>
#include <BH1750.h> #include <BH1750.h>
#include <Servo.h> #include <Servo.h>
#define DHT11_PIN 7 #define DHT11_PIN 7
#define photoL1 A0 #define photoL1 A0
#define photoL2 A1
#define photoL3 A2
#define photoL4 A3
#define LED 3 #define LED 3
#define button 2 #define button 2
#define buzzer 6 #define buzzer 6
#define circleServoPin 9 #define circleServoPin 9
#define tiltServoPin 10 #define tiltServoPin 10
#define LED 3//define the pin of LED as D10
BH1750 lightMeter; BH1750 lightMeter;
DHT dht(DHT11_PIN, DHT11); dht11 DHT;
volatile int buttonState; volatile int buttonState;
volatile int lightReadL1 = 0; volatile int lightReadL1 = 0;
int count; volatile int lightReadL2 = 0;
volatile int lightReadL3 = 0;
volatile int lightReadL4 = 0;
int value;
int servoPin = 9;//set the pin of the servo
LiquidCrystal_I2C lcd(0x27, 16, 2); LiquidCrystal_I2C lcd(0x27, 16, 2);
Servo circleServo; Servo circleServo;
Servo tiltServo; Servo tiltServo;
@ -24,38 +32,39 @@ Servo tiltServo;
void setup() void setup()
{ {
Serial.begin(9600); Serial.begin(9600);
Serial.println("");
Serial.print("setting up!\n"); Serial.print("setting up!\n");
pinMode(LED, OUTPUT); //initialize digital pin LED as an output.
pinMode(button, INPUT);
pinMode(buzzer, OUTPUT);
circleServo.attach(circleServoPin);
tiltServo.attach(tiltServoPin);
Serial.print("init lcd\n"); Serial.print("init lcd\n");
lcd.init(); lcd.init();
lcd.backlight(); lcd.backlight();
lcd.setCursor(0,0); lcd.setCursor(0,0);
lcd.printstr("Hello, World!"); lcd.printstr("setting up!");
lcd.setCursor(0,1); //lcd.setCursor(0,1);
lcd.printstr("hello, Roald!"); //lcd.printstr("hey,human!");
dht.begin(20); pinMode(LED, OUTPUT); //initialize digital pin LED as an output.
pinMode(button, INPUT);
pinMode(buzzer, OUTPUT);
//circleServo.attach(circleServoPin);
//tiltServo.attach(tiltServoPin);
Serial.print("done setting up!\n"); Serial.print("done setting up!\n");
lcd.clear();
lcd.setCursor(0,0);
lcd.printstr("done");
//Intialize the 12C bus //Intialize the 12C bus
Wire.begin(); Wire.begin();
//On esp8266 you can select SCL and SDA pins using Wire.begin //On esp8266 you can select SCL and SDA pins using Wire.begin
//For Wemos /Lolin D1 Mini Pro and the Ambient Light shield use //For Wemos /Lolin D1 Mini Pro and the Ambient Light shield use
Wire.begin();
lightMeter.begin(); lightMeter.begin();
Serial.println(F("BH1750 Test begin")); Serial.println(F("BH1750 Test begin"));
} }
void loop(){ void loop()
//Serial.print("["); {
//Serial.print(count); /*
//Serial.print("] hey\n");
delay(300); delay(300);
for (int i = 180; i > 0; i--) { for (int i = 180; i > 0; i--) {
circleServo.write(i); circleServo.write(i);
@ -67,92 +76,106 @@ void loop(){
tiltServo.write(i); tiltServo.write(i);
delay(10); delay(10);
} }
*/
lightReadL1 = analogRead(photoL1);
if (lightReadL1 > 300){ /*
servopulse(servoPin, 0);//rotate to 0 degree
delay(200);//delay in 1s
servopulse(servoPin, 90);//rotate to 90 degrees
delay(200);
servopulse(servoPin, 180);//rotate to 1800 degrees
delay(200);
if (lightReadL1 > 300){
digitalWrite(LED, HIGH); digitalWrite(LED, HIGH);
} }
else{ else{
digitalWrite(LED, LOW); digitalWrite(LED, LOW);
} }
*/
float lux = lightMeter.readLightLevel(); int chk;
float hum = dht.readTemperature(); chk = DHT.read(DHT11_PIN);
float temp = dht.readHumidity(); switch (chk)
{ {
Serial.print("hum: "); case DHTLIB_OK:
Serial.print(hum); break;
Serial.print(" temp: "); case DHTLIB_ERROR_CHECKSUM:
Serial.print(dht.convertCtoF(temp)); Serial.println("[ERR] dht11 checksum");
Serial.print(" L1: "); break;
Serial.print(lightReadL1); case DHTLIB_ERROR_TIMEOUT:
Serial.print(" PanL: "); Serial.println("[ERR] dht11 timeout");
Serial.print(lux); break;
Serial.println("lx"); default:
delay(50); break;
} }
//delay(100); float lux = lightMeter.readLightLevel();
lightReadL1 = analogRead(photoL1);
//blink(); lightReadL2 = analogRead(photoL2);
lightReadL3 = analogRead(photoL3);
lightReadL4 = analogRead(photoL4);
{
char buf[40];
char lStr[6];
dtostrf(lux, 4, 2, lStr); // https://forum.arduino.cc/t/sprintf-with-float-values/937562
sprintf(buf, "h: %d%%, t: %dF, l: %s, ", DHT.humidity, CtoF(DHT.temperature), lStr);
Serial.print(buf);
sprintf(buf, "L1: %d, L2: %d, L3: %d, L4: %d", lightReadL1, lightReadL2, lightReadL3, lightReadL4);
Serial.println(buf);
lcd.clear();
// the LCD has two rows and 17 columns ...
sprintf(buf, "h:%d,t:%d,l:%s", DHT.humidity, CtoF(DHT.temperature), lStr);
lcd.setCursor(0,0);
lcd.printstr(buf);
sprintf(buf, "%d,%d,%d,%d", lightReadL1, lightReadL2, lightReadL3, lightReadL4);
lcd.setCursor(0,1);
lcd.printstr(buf);
}
delay(2000);
/*
{
Serial.print("hum: ");
Serial.print(DHT.humidity);
Serial.print(", temp: ");
Serial.print(CtoF(DHT.temperature));
Serial.print(", L1: ");
Serial.print(lightReadL1);
Serial.print(", L2: ");
Serial.print(lightReadL2);
Serial.print(", L3: ");
Serial.print(lightReadL3);
Serial.print(", L4: ");
Serial.print(lightReadL4);
Serial.print(", lx: ");
Serial.println(lux);
delay(500);
}
*/
buttonState = digitalRead(button); buttonState = digitalRead(button);
if(buttonState == 0) { if(buttonState == 0) {
digitalWrite(LED, HIGH); for(value = 0; value < 255; value +1){
birthdaySong(); analogWrite (LED, value);//LED lights gradually light up
} else { }
} else {
digitalWrite(LED, LOW); digitalWrite(LED, LOW);
value = 0;
} }
//delay(100);
//count++;
} }
void birthdaySong() { int CtoF(int temp)
tone(buzzer, 294); {
delay(250); return temp * 1.8 + 32;
tone(buzzer, 440); }
delay(250);
tone(buzzer, 392); int FtoC(int temp)
delay(250); {
tone(buzzer, 532); return (temp - 32) * 0.55555;
delay(250);
tone(buzzer, 494);
delay(500);
tone(buzzer, 392);
delay(250);
tone(buzzer, 440);
delay(250);
tone(buzzer, 392);
delay(250);
noTone(buzzer);
tone(buzzer, 587);
delay(250);
tone(buzzer, 532);
delay(500);
tone(buzzer, 392);
delay(250);
tone(buzzer, 784);
delay(250);
tone(buzzer, 659);
delay(250);
tone(buzzer, 532);
delay(250);
tone(buzzer, 494);
delay(250);
tone(buzzer, 440);
delay(250);
tone(buzzer, 698);
delay(375);
tone(buzzer, 659);
delay(250);
tone(buzzer, 532);
delay(250);
tone(buzzer, 587);
delay(250);
tone(buzzer, 532);
delay(500);
noTone(buzzer);
} }
void blink() void blink()