Created
July 3, 2009 09:50
-
-
Save alx/140034 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ======= | |
// ======= | |
// | |
// 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); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment