/
LilyTiny_light_sensor.ino
173 lines (134 loc) · 4.11 KB
/
LilyTiny_light_sensor.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
163
164
165
166
167
168
169
170
171
172
173
//Saved as LilyTiny_light_sensor
// It works for Lily Tiny. Use a 2K resistor ...see image on Instructables
//https://www.instructables.com/id/5-Simple-ATtiny85-LDR-Scripts/
int led1 = 0;
int led2 = 1;
int scriptNumber = 1; //here you can select one of 4 scripts
//if scriptNumber = 1: in the dark, the led on pin 1 is off & the led on pin 0 is on (& vice versa)
//if scriptNumber = 2: LED1 depending on LDR, LED2 is always blinking
//if scriptNumber = 3: fading alternately, speed of fading by LDR
//if scriptNumber = 4: depending on light value, blink alternating or both
int LDR = 1; //Light Dependent Resistor on Digital Pin 2 (which is the same as A1)
int call = 0;
long timer;
// the setup function runs once when you press reset or power the board
void setup() {
// initialize digital pins 1 & 2 as an output.
pinMode(led1, OUTPUT);
pinMode(led2, OUTPUT);
blinkOne(led1, 100);
delay(500);
blinkOne(led2, 100);
call = callibrate();
timer = millis() + 2000;
}
// the loop function runs over and over again forever
void loop() {
//script 1: depending on light value, either LED1 or LED2 in turned on
if ( scriptNumber == 1 ) {
digitalWrite(led1, LOW);
digitalWrite(led2, LOW);
if ( (analogRead ( LDR )) > 500 ) //if it's darkish >750 might be darker
{
digitalWrite(led1, HIGH); //the led on pin 0 is on
delay(200);
} else {
digitalWrite(led2, HIGH); //otherwise,if it's light, the led on pin 1 is on
delay(200);
}
call = callibrate();
} else if ( scriptNumber == 2 ) { // LED1 depending on LDR, LED2 is glowing
digitalWrite(led1, LOW);
digitalWrite(led2, LOW);
if ( (analogRead ( LDR )) > 500 ) //if it's sort of dark to very dark
{
digitalWrite(led1, HIGH); //LED1 is on
delay(200);
} else { //if it's bright
delay(200);
}
call = callibrate();
if ( timer < millis()) {
blinkOne(led2, 200);
timer = millis() + 2000;
}
} else if ( scriptNumber == 3 ) {
for (int fadeValue = 0 ; fadeValue <= 255; fadeValue += 5) {
analogWrite(led1, fadeValue);
delaySpecial();
}
for (int fadeValue = 255 ; fadeValue >= 0; fadeValue -= 5) {
analogWrite(led1, fadeValue);
delaySpecial();
}
for (int fadeValue = 0 ; fadeValue <= 255; fadeValue += 5) {
analogWrite(led2, fadeValue);
delaySpecial();
}
for (int fadeValue = 255 ; fadeValue >= 0; fadeValue -= 5) {
analogWrite(led2, fadeValue);
delaySpecial();
}
// blinkOne(2, 200);
} else if ( scriptNumber == 4 ) { //depending on light value, blink alternating or both
digitalWrite(led1, LOW);
digitalWrite(led2, LOW);
if ( (analogRead ( LDR )) > 500 )
{
blinkBoth(200);
blinkBoth(200);
blinkBoth(200);
blinkBoth(200);
blinkBoth(200);
} else {
blinkOne(led1, 1000);
blinkOne(led2, 1000);
blinkOne(led1, 1000);
blinkOne(led2, 1000);
}
} else if ( scriptNumber == 5 ) {
//see separate script with morse code
}
}
void delaySpecial() {
delay(1 + abs(analogRead(LDR) - 300) / 50);
}
void blinkOne ( int whichLed, int howLong )
{
digitalWrite(whichLed, HIGH);
delay( howLong);
digitalWrite(whichLed, LOW);
delay(howLong);
}
void blinkBoth ( int howLong )
{
digitalWrite(led1, HIGH);
digitalWrite(led2, HIGH);
delay( howLong);
digitalWrite(led1, LOW);
digitalWrite(led2, LOW);
delay(howLong);
}
int callibrate() { // type electret
int sensor1 = analogRead(LDR) / 10;
delay(10);
sensor1 += analogRead(LDR) / 10;
delay(10);
sensor1 += analogRead(LDR) / 10;
delay(10);
sensor1 += analogRead(LDR) / 10;
delay(10);
sensor1 += analogRead(LDR) / 10;
delay(10);
sensor1 += analogRead(LDR) / 10;
delay(10);
sensor1 += analogRead(LDR) / 10;
delay(10);
sensor1 += analogRead(LDR) / 10;
delay(10);
sensor1 += analogRead(LDR) / 10;
delay(10);
sensor1 += analogRead(LDR) / 10;
delay(10);
return sensor1;
}