-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSensor_PCB_Attiny85_ver_1_testing.ino
167 lines (103 loc) · 4.16 KB
/
Sensor_PCB_Attiny85_ver_1_testing.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
/*
Day night IR sensor shield 5/04/13 Trobb
Please enjoy my dodgy code!
I am not a programmer by any means!
Feel free to improve on it!
Check out http://www.tobyrobb.com
Written with Arduino IDE V.1.0.1
$VER1.0b
Version 1.0b for the Attiny 85 mini 3 LED Sensor Widget Board
DEBUG VERSION!!!!
this version does a double flash when in the check LDR routine.
TO DO
*/
#include <avr/wdt.h> // include the watchdog timer library
#define ldrPin A1 // LDR light sensor
#define irPin 1 // IR movement sensor pin
#define timerPin A2 // Pin the timer potentiometer is connected to
#define brightnessPin A3 // Pin the timer potentiometer is connected to
#define ledPin 0 // Output for LED
#define timerGain 1 // the amount of timer delay gain to add to the timerPot. 10 is a normal value
boolean running = false; // BOOLEAN value to decide wether we are dark and running
int DUSK = 250; // Value to trigger the low light condition
int DAWN = 270; // Value to trigger the bright light condition
float brightnessPot = 0; // a variable to hold the brightness pot value
float brightness = 0; // A variable to hold the brightness value itself for the LED
float delayPot = 0; // A variable to hold the value of the delay on time
float delayTime = 0; // This is the exact value of the timer delay
long previousMillis = 0; // will store last time we checked the LDR
long interval = 3000; // interval at which to check the LDR (milliseconds)
void setup() {
wdt_enable(WDTO_8S); // begin watchdog timer
// initialize the IO.
pinMode(ledPin, OUTPUT);
pinMode(irPin, INPUT);
pinMode(ldrPin, INPUT);
pinMode(timerPin, INPUT);
pinMode(brightnessPin, INPUT);
digitalWrite(ledPin, LOW); // start with LED off
digitalWrite(irPin, LOW); // no PULLUPS
wdt_reset(); //make sure we reset watchdog timer to prevent endless resetting
//lets flash the led
for(int i = 0; i <=255;i++){
analogWrite(ledPin,i);
delay(1);
}
for(int i = 255; i >=0;i--){
analogWrite(ledPin,i);
delay(1);
}
// Setup is complete!!
wdt_reset(); //make sure we reset watchdog timer to prevent endless resetting
}
void loop(){
wdt_reset(); //make sure we reset watchdog timer to prevent endless resetting
checkLdr();
checkBrightness();
checkDelay();
while(running){
checkDelay();
analogWrite(ledPin,brightness);
for(int i = 0; i <= delayTime; i++){
delay(1);
wdt_reset(); //make sure we reset watchdog timer to prevent endless resetting
checkLdr();
checkBrightness();
}
}
wdt_reset(); //make sure we reset watchdog timer to prevent endless resetting
analogWrite(ledPin,30);
}
void checkLdr(){
// check to see if it's time to read the LDR;
unsigned long currentMillis = millis(); // difference between the current time and last time
if(currentMillis - previousMillis > interval) { // if THIS time is an INTERVAL longer than LAST time
previousMillis = currentMillis; // save the last time you checked the LDR
// go ahead and check the LDR
if(analogRead(ldrPin)>=DAWN){
running = false; // set the RUNNING flag to OFF
}
if(analogRead(ldrPin)<=DUSK){
running = true; // set the RUNNING flag to ON
}
} // end of if statement
wdt_reset(); //make sure we reset watchdog timer to prevent endless resetting
}
void checkBrightness(){
// lets average the brightness pot
for(int i =1; i <=10; i++){
brightnessPot = brightnessPot + analogRead(brightnessPin);
delay(5); // adc settle time
}
brightness = map((brightnessPot/10), 0, 1023, 0, 255);
wdt_reset(); //make sure we reset watchdog timer to prevent endless resetting
}
void checkDelay(){
// lets average the brightness pot
for(int i =1; i <=10; i++){
delayPot = delayPot + analogRead(timerPin);
delay(5); // adc settle time
}
delayTime = (delayPot/10); // A variable to hold the brightness value itself for the LED
wdt_reset(); //make sure we reset watchdog timer to prevent endless resetting
}