-
Notifications
You must be signed in to change notification settings - Fork 1
/
luxmeter.ino
162 lines (144 loc) · 4.86 KB
/
luxmeter.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
// BH1750 + 5510 LCD Luxmeter
#include <Wire.h>
#include <BH1750.h>
#include <SPI.h>
#include <Adafruit_GFX.h>
#include <Adafruit_PCD8544.h>
// Hardware SPI (faster, but must use certain hardware pins):
// pin 13 on Arduino (SCK) - LCD serial clock (SCLK)
// pin 11 on Arduino (MOSI) - LCD data in (DIN)
// pin 5 - LCD Data/Command select (D/C)
// pin 4 - LCD chip select (CS)
// pin 3 - LCD reset (RST)
Adafruit_PCD8544 display = Adafruit_PCD8544(5, 4, 3);
// Note with hardware SPI MISO and SS pins aren't used but will still be read
// and written to during SPI transfer. Be careful sharing these pins!
/*Connection of BH1750:
VCC-5v
GND-GND
SCL-SCL(analog pin 5)
SDA-SDA(analog pin 4)
ADD-NC or GND
*/
BH1750 lightMeter;
//84*48
const uint8_t BUFF_LENGTH = 83;
uint8_t buffIndex = 0;
uint16_t graphBuff[BUFF_LENGTH]={0};
uint16_t graphMin=65535, graphMax=0;
int numScreens = 2;
volatile int screenMode = 0;
void changeScreen(){
// Uncomment these Serial.print lines to enable debug information
//Serial.print("Change screen from ");
//Serial.print(screenMode);
screenMode = ++screenMode%numScreens;
//Serial.print(" to ");
//Serial.println(screenMode);
}
void testContrast(){
int c = 0;
display.setTextSize(2);
display.setTextColor(BLACK);
while(true){
if(digitalRead(2)) continue; // Stop if button is not pressed
display.clearDisplay();
display.setContrast(c);
display.setCursor(0,16);
display.print(c);
display.display();
Serial.print("contrast: ");
Serial.println(c);
c++;
if(c==128) c=0;
delay(100);
}
}
void setup() {
screenMode = 0;
pinMode(9,OUTPUT);
pinMode(13, OUTPUT);
pinMode(2,INPUT_PULLUP);
Serial.begin(57600);
lightMeter.begin(BH1750_CONTINUOUS_HIGH_RES_MODE_2);
display.begin();
display.setContrast(55);
// Contrast value of 50 works fine for me. If your LCD is badly readable,
// or doesn't show anything at all, uncomment the following line to go into
// Contrast Test mode and see, which value works best for you
// Hold down the button to change contrast
// Then change display.setContrast(55) to that value
//testContrast();
attachInterrupt(0,changeScreen,FALLING);
}
void loop() {
digitalWrite(13,HIGH);
// Get lux reading from HB1750
uint16_t lux = lightMeter.readLightLevel();
Serial.print("Light: ");
Serial.print(lux);
Serial.println(" lx");
// Turn on/off LCD backlight based on current light level
if(lux>4000){
digitalWrite(9, HIGH);
}
else{
if(lux>3000){
analogWrite(9, (lux-1000)/4);
}
else
digitalWrite(9, LOW);
}
// Update graph buffer
buffIndex = (buffIndex+1)%BUFF_LENGTH;
graphBuff[buffIndex] = lux;
// Update graph minimum and maximum values
graphMax=graphBuff[0]; //0;
graphMin=graphBuff[0]; //65535;
for(int x=0; x<BUFF_LENGTH;x++){
if(graphBuff[x]>graphMax) graphMax=graphBuff[x];
if(graphBuff[x]<graphMin) graphMin=graphBuff[x];
}
// Update LCD
display.clearDisplay();
switch(screenMode){
case 0:
display.setTextSize(2);
display.setTextColor(BLACK);
display.setCursor(0,16);
display.print(lux);
display.setCursor(45,33);
display.println("lux");
display.display();
break;
case 1:
for(int x=1;x<83;x++){
/*if(buffIndex-x-1<2)
display.drawPixel(x, 38-int(map(graphBuff[x-1], graphMin,graphMax, 0,38)), WHITE);
else*/
//display.drawPixel(x, 38-int(map(graphBuff[x-1], graphMin,graphMax, 0,38)), BLACK);
//display.drawPixel(x, 38-int(map(graphBuff[(buffIndex+x)%BUFF_LENGTH ], graphMin,graphMax, 0,38)), BLACK);
/*display.drawLine(x, 38-int(map(graphBuff[x-1], graphMin,graphMax, 0,38)),
x+1, 38-int(map(graphBuff[x], graphMin,graphMax, 0,38)), BLACK);*/
if(x==1){
// Avoid drawing a line from the end to the beginning of graph
display.drawLine(x, 38-int(map(graphBuff[(buffIndex+x)%BUFF_LENGTH], graphMin,graphMax, 0,38)),
x+1, 38-int(map(graphBuff[(buffIndex+x)%BUFF_LENGTH], graphMin,graphMax, 0,38)), BLACK);
}
else {
display.drawLine(x, 38-int(map(graphBuff[(buffIndex+x-1)%BUFF_LENGTH], graphMin,graphMax, 0,38)),
x+1, 38-int(map(graphBuff[(buffIndex+x)%BUFF_LENGTH], graphMin,graphMax, 0,38)), BLACK);
}
}
// Refresh screen
display.setTextSize(1);
display.setTextColor(BLACK);
display.setCursor(0,40);
display.print(lux);
display.println(" lux");
display.display();
break;
}
digitalWrite(13,LOW);
delay(250);
}