Attachment 'Light_Time_LDR_Fade.ino'
Download 1 /*
2 Author : Loe Feijs and TU/e 2013.
3 This example shows how to control a LED (LED via 470 Ohm between pin 9 and GND).
4 The LED goes on when the environment's bright-ness changes
5 suddenly. The averaged brightness is calculated by a moving
6 average with time constant of 20 seconds. The LDR is connect
7 between +5V and pin A0. The LDR has a pull-down resistor of
8 22k between pin A0 and GND. The LED fades out after 10 seconds.
9 It takes 2 seconds to fade out.
10
11 Author : P.Peters
12 Date : 20131202
13
14 Modified code such that it fits my test plug
15 pin 9 is used as GND, and connects to the LED and button.
16 pin 10 has a 470 Ohm resistor to the LED
17 pin 11 has a 470 Ohm resistor to the other side of the button
18 In order to operate, pin 9 must be 0, and pin 11 must have pullup
19 Also added a fade in in the state machine when led switches on
20 */
21
22 //FSM states:
23 #define OFF 0
24 #define ON 1
25 #define FADINGOUT 2
26 #define FADINGIN 3
27
28 //connect LED, LDR:
29 #define gndPin 9
30 #define ledPin 10
31 #define buttonPin 11
32 #define ldrPin A0
33
34 #define onTime 400 //times 10 milliseconds
35 #define fadeTime 200 //times 10 milliseconds
36
37 int state = OFF;
38 int timer = 0;
39 boolean LDRevent = false;
40
41 void LEDstep(){
42 switch (state){
43 case OFF:
44 // if (digitalRead(buttonPin) == LOW) {
45 if (LDRevent){
46 // prepare timer for delay
47 timer = fadeTime;
48 // switch state
49 state = FADINGIN;
50 }
51 break;
52 case ON:
53 if (timer > 0){
54 timer--;
55 }
56 else {
57 // prepare timer for fading out
58 timer = fadeTime;
59 // switch state
60 state = FADINGOUT;
61 }
62 break;
63 case FADINGOUT:
64 if (timer > 0){
65 //fade from 250 to 0 during fadeTime steps
66 timer--;
67 analogWrite(ledPin,10*timer/8);
68 }
69 else {
70 // switch state
71 state = OFF;
72 LDRevent = false;
73 // state transition actions
74 analogWrite(ledPin,0);
75 }
76 break;
77 case FADINGIN:
78 if (timer > 0){
79 //fade from 0 to 255 during fadeInTime steps
80 timer--;
81 analogWrite(ledPin,250-10*timer/8);
82 }
83 else {
84 // switch state
85 state = ON;
86 timer = onTime;
87 // state transition actions
88 analogWrite(ledPin,255);
89 }
90 break;
91 }
92 }
93
94 float avg;
95
96 void setup() {
97 pinMode(gndPin,OUTPUT);
98 digitalWrite(gndPin,0);
99 pinMode(ledPin, OUTPUT);
100 pinMode(buttonPin, INPUT_PULLUP);
101 pinMode(ldrPin, INPUT);
102 avg = (float)analogRead(ldrPin);
103 }
104
105 void LDRstep(){
106 //LDR from +5V to pin A0, pulldown 22k to GND
107 int v = analogRead(ldrPin);
108 avg = 0.9995 * avg + 0.0005* (float)v;
109 float delta = 20.0; // 20 times 10msec
110 float dif = (float)v - avg;
111 if (abs(dif) > delta)
112 LDRevent = true;
113 }
114
115 void loop() {
116 LDRstep();
117 LEDstep();
118 delay(10); // 10 ms delay between steps
119 }
Attached Files
To refer to attachments on a page, use attachment:filename, as shown below in the list of files. Do NOT use the URL of the [get] link, since this is subject to change and can break easily.You are not allowed to attach a file to this page.