solar_charger/soler_chager.ino

188 lines
4.2 KiB
Arduino
Raw Normal View History

#include <Wire.h>
2022-12-29 02:57:16 +00:00
#include <LiquidCrystal_I2C.h>
#include <dht11.h>
#include <BH1750.h>
2023-01-27 01:55:37 +00:00
#include <Servo.h>
2022-12-29 03:25:00 +00:00
2022-12-29 02:57:16 +00:00
#define DHT11_PIN 7
#define photoL1 A0
#define photoL2 A1
#define photoL3 A2
#define photoL4 A3
2022-12-29 02:57:16 +00:00
#define LED 3
#define button 2
#define buzzer 6
2023-01-27 01:55:37 +00:00
#define circleServoPin 9
#define tiltServoPin 10
#define LED 3//define the pin of LED as D10
2022-12-29 02:57:16 +00:00
BH1750 lightMeter;
dht11 DHT;
2022-12-29 02:57:16 +00:00
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
2022-12-29 02:57:16 +00:00
LiquidCrystal_I2C lcd(0x27, 16, 2);
2023-01-27 01:55:37 +00:00
Servo circleServo;
Servo tiltServo;
2022-12-29 02:57:16 +00:00
void setup()
{
Serial.begin(9600);
Serial.println("");
2022-12-29 02:57:16 +00:00
Serial.print("setting up!\n");
2023-01-27 01:55:37 +00:00
2022-12-29 02:57:16 +00:00
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);
2022-12-29 02:57:16 +00:00
Serial.print("done setting up!\n");
lcd.clear();
lcd.setCursor(0,0);
lcd.printstr("done");
2023-01-26 23:30:22 +00:00
//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"));
2022-12-29 02:57:16 +00:00
}
void loop()
{
/*
2023-01-27 01:55:37 +00:00
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;
}
2022-12-29 02:57:16 +00:00
2023-01-26 23:30:22 +00:00
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);
/*
2022-12-29 02:57:16 +00:00
{
2023-01-26 23:30:22 +00:00
Serial.print("hum: ");
Serial.print(DHT.humidity);
Serial.print(", temp: ");
Serial.print(CtoF(DHT.temperature));
Serial.print(", L1: ");
2023-01-26 23:30:22 +00:00
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);
2022-12-29 02:57:16 +00:00
}
*/
2022-12-29 02:57:16 +00:00
buttonState = digitalRead(button);
if(buttonState == 0) {
for(value = 0; value < 255; value +1){
analogWrite (LED, value);//LED lights gradually light up
}
} else {
2022-12-29 02:57:16 +00:00
digitalWrite(LED, LOW);
value = 0;
2022-12-29 02:57:16 +00:00
}
2022-12-29 03:25:00 +00:00
}
2022-12-29 02:57:16 +00:00
int CtoF(int temp)
{
return temp * 1.8 + 32;
}
int FtoC(int temp)
{
return (temp - 32) * 0.55555;
2022-12-29 02:57:16 +00:00
}
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
}