190 lines
4.1 KiB
C++
190 lines
4.1 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 button 3
|
|
#define buzzer 6
|
|
#define circleServoPin 10
|
|
#define tiltServoPin 9
|
|
#define LED 4 //define the pin of LED as D10
|
|
|
|
#define TILT_ANGLE_FLAT 0
|
|
#define TILT_ANGLE_MAX 170
|
|
|
|
#define TURN_L3 180
|
|
#define TURN_L4 90
|
|
#define TURN_L1 0
|
|
|
|
#define SERVO_STEP 5
|
|
|
|
byte m_speed = 10;
|
|
BH1750 lightMeter;
|
|
dht11 DHT;
|
|
volatile int buttonState;
|
|
volatile int lightReadL1 = 0; // EAST FACING
|
|
volatile int lightReadL2 = 0; // NORTH FACING
|
|
volatile int lightReadL3 = 0; // WEST FACING
|
|
volatile int lightReadL4 = 0; // SOUTH FACING
|
|
int value;
|
|
LiquidCrystal_I2C lcd(0x27, 16, 2);
|
|
Servo circleServo;
|
|
Servo tiltServo;
|
|
int tiltPos;
|
|
int circlePos;
|
|
|
|
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);
|
|
circleServo.write(TURN_L4);
|
|
Serial.print("circle pos: ");
|
|
Serial.print(TURN_L4);
|
|
Serial.println("");
|
|
delay(1000);
|
|
|
|
tiltServo.attach(tiltServoPin);
|
|
tiltServo.write(0);
|
|
Serial.print("tilt pos: ");
|
|
Serial.print(TILT_ANGLE_FLAT);
|
|
Serial.println("");
|
|
delay(1000);
|
|
|
|
if (false) {
|
|
circleServo.write(TURN_L3);
|
|
delay(1000);
|
|
circleServo.write(TURN_L1);
|
|
delay(1000);
|
|
circleServo.write(TURN_L4);
|
|
delay(1000);
|
|
|
|
for (tiltPos = TILT_ANGLE_FLAT; tiltPos < TILT_ANGLE_MAX; tiltPos += SERVO_STEP) {
|
|
tiltServo.write(tiltPos);
|
|
Serial.print("tilt pos: ");
|
|
Serial.print(tiltPos);
|
|
Serial.println("");
|
|
delay(m_speed);
|
|
}
|
|
}
|
|
|
|
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"));
|
|
}
|
|
|
|
long long count = 0;
|
|
void loop()
|
|
{
|
|
char buf[40];
|
|
|
|
//lcd.clear();
|
|
//lcd.setCursor(0,0);
|
|
|
|
sprintf(buf, "count: %d\n", count);
|
|
Serial.print(buf);
|
|
//lcd.printstr(buf);
|
|
|
|
count++;
|
|
}
|
|
|
|
|
|
|
|
int CtoF(int temp)
|
|
{
|
|
return temp * 1.8 + 32;
|
|
}
|
|
|
|
int FtoC(int temp)
|
|
{
|
|
return (temp - 32) * 0.55555;
|
|
}
|
|
|
|
void old_loop()
|
|
{
|
|
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);
|
|
|
|
buttonState = digitalRead(button);
|
|
if(buttonState == 0) {
|
|
analogWrite (LED, HIGH);
|
|
} else {
|
|
digitalWrite(LED, LOW);
|
|
}
|
|
}
|
|
|
|
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
|
|
}
|