solar_charger/soler_chager.ino
Vincent Batts bb0c226c31
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>
2024-08-03 19:36:25 -04:00

187 lines
4.2 KiB
C++

#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <dht11.h>
#include <BH1750.h>
#include <Servo.h>
#define DHT11_PIN 7
#define photoL1 A0
#define photoL2 A1
#define photoL3 A2
#define photoL4 A3
#define LED 3
#define button 2
#define buzzer 6
#define circleServoPin 9
#define tiltServoPin 10
#define LED 3//define the pin of LED as D10
BH1750 lightMeter;
dht11 DHT;
volatile int buttonState;
volatile int lightReadL1 = 0;
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);
Servo circleServo;
Servo tiltServo;
void setup()
{
Serial.begin(9600);
Serial.println("");
Serial.print("setting up!\n");
Serial.print("init lcd\n");
lcd.init();
lcd.backlight();
lcd.setCursor(0,0);
lcd.printstr("setting up!");
//lcd.setCursor(0,1);
//lcd.printstr("hey,human!");
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");
lcd.clear();
lcd.setCursor(0,0);
lcd.printstr("done");
//Intialize the 12C bus
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
lightMeter.begin();
Serial.println(F("BH1750 Test begin"));
}
void loop()
{
/*
delay(300);
for (int i = 180; i > 0; i--) {
circleServo.write(i);
tiltServo.write(i);
delay(10);
}
for (int i = 0; i < 180; i++) {
circleServo.write(i);
tiltServo.write(i);
delay(10);
}
*/
/*
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);
}
else{
digitalWrite(LED, LOW);
}
*/
int chk;
chk = DHT.read(DHT11_PIN);
switch (chk)
{
case DHTLIB_OK:
break;
case DHTLIB_ERROR_CHECKSUM:
Serial.println("[ERR] dht11 checksum");
break;
case DHTLIB_ERROR_TIMEOUT:
Serial.println("[ERR] dht11 timeout");
break;
default:
break;
}
float lux = lightMeter.readLightLevel();
lightReadL1 = analogRead(photoL1);
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);
if(buttonState == 0) {
for(value = 0; value < 255; value +1){
analogWrite (LED, value);//LED lights gradually light up
}
} else {
digitalWrite(LED, LOW);
value = 0;
}
}
int CtoF(int temp)
{
return temp * 1.8 + 32;
}
int FtoC(int temp)
{
return (temp - 32) * 0.55555;
}
void blink()
{
digitalWrite(LED,HIGH); //turn the LED on (HIGH is the voltage level
delay(1000); //wait for a second
digitalWrite(LED,LOW); //turn the LED off by making the voltage LOW
delay(1000); //wait for a second
}