// ======= // ======= // // 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= 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); }