alx (owner)

Revisions

gist: 140034 Download_button fork
public
Public Clone URL: git://gist.github.com/140034.git
powermeter.pde
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
// =======
// =======
//
// Arduino Powermeter
//
// =======
// =======
//
// Use Arduino and a photoresistor LED to read impulsion from
// an electricity box while it blinks its consumption
//
// =======
//
// Authors:
// * Tetalab: http://tetalab.tuxfamily.org/
// * Gilles Reyna
// * Alex Girard: http://alexgirard.com
//
 
#define LDR_PIN 0 // set the LDR_PIN where the LDR will be connected
#define SAMPLE_DELAY 25 // set the sample delay (ms) for retrieving the LDR value
#define LED_PIN 13 // set the LED PIN where the LED is blinking
// during calibration step
#define CALIBRATION_DELAY 15000 // set calibration time
 
int val=0; // store the current value of the analog pin LDR_PIN
int sensorMin=1023; // minimum LDR sensor value
int sensorMax=0; // max LDR sensor value
int sensorMaxMax=0; // max LDR sensor value
 
const int numReadings = 5;
 
int readings[numReadings]; // the readings from the analog input
int index = 0; // the index of the current reading
int total = 0; // the running total
int average = 0; // the average
 
int inputPin = 0;
boolean filterDown=false;
unsigned long delayImpulsion;
 
void setup()
{
// Init mode for LDR
pinMode(LED_PIN, OUTPUT);
 
// Calibrate LDR
digitalWrite(LED_PIN, HIGH);
while (millis() < CALIBRATION_DELAY)
{
val=analogRead(LDR_PIN);
if (val>sensorMax) {
sensorMax = val;
}
if (val<sensorMin) {
sensorMin = val;
}
}
sensorMaxMax = sensorMax;
digitalWrite(LED_PIN, LOW);
 
// Send calibration on Serial
Serial.begin(9600);
Serial.print("Sensor Min:");
Serial.println(sensorMin);
Serial.print("Sensor Max :");
Serial.println(sensorMax);
 
// initialize all the readings to 0:
for (int thisReading = 0; thisReading < numReadings; thisReading++)
readings[thisReading] = 0;
 
delayImpulsion = millis();
}
 
void displayInfo(){
Serial.print("Current Val:");
Serial.print(val);
Serial.print(" / Average:");
Serial.print(average, DEC);
Serial.print(" / timestamp:");
Serial.println(millis());
}
 
void updateReadingArray() {
readings[index] = val;
// add the reading to the total:
total= total + readings[index];
// advance to the next position in the array:
index = index + 1;
// if we're at the end of the array...
if (index >= numReadings)
// ...wrap around to the beginning:
index = 0;
 
// calculate the average:
average = total / numReadings;
}
 
void loop()
{
total= total - readings[index];
val=analogRead(LDR_PIN);
 
if(val > average + ((sensorMax - sensorMin)/3)){
// Impulsion found, send it to client and wait for LDR down-slope
displayInfo();
delay(800);
delayImpulsion = millis();
} else if(millis() - delayImpulsion > 10000){
// In case of long period without impulsion
// reinitialize sensorMax with initial value
sensorMax = sensorMaxMax;
}
 
updateReadingArray();
 
// Update maximum value for sensor
if (val > sensorMax) {
sensorMax = val;
}
 
// Wait before to get a new sample from LDR
delay(SAMPLE_DELAY);
}