/** * Energy meter */ int scrWidth = 12*24*2; int scrHeight = 300; int imgWidth = 320; int imgHeight = 240; int scopeX1 = 0; // define the frame in the image to scan for blink int scopeX2 = imgWidth; int scopeY1 = 0; int scopeY2 = imgHeight; int blink; // flag int blinkWas; // flag used to determine transition int dengs=0; int tasto; int binIs = 0; // current bin to increment (1 of 12*24) int[] dataCollection = new int[12*24]; // 24 hours x 12 bins per hour (1 every 5 mins) int MPosX = 0; // position of mouse within data window String HoursS = ""; String MinsS = ""; import processing.video.*; Capture video; void setup() { size(scrWidth, scrHeight); video = new Capture(this, imgWidth, imgHeight, 50); noStroke(); smooth(); rectMode(CORNER); noFill(); background(0); // clear (black) background PFont font; font = loadFont("ArialMT-14.vlw"); textFont(font); } void draw() { int last24h = 0; if (video.available()) { binIs = hour()*12+minute()/5; if((second() == 0) && (5*(minute()/5) == minute())){ dataCollection[binIs] = 0; // initialize bin to zero (write over every 24 hours) println("Time is "+hour()+":"+minute()); // print time every 5 minutes } video.read(); if (tasto == 86){ // check for "v" key pressed background(0); // clear (black) background stroke(255, 204, 0, 128); image(video, 0, 0, imgWidth, imgHeight); // Draw the webcam video onto the screen rect(scopeX1,scopeY1,scopeX2-scopeX1,scopeY2-scopeY1); } else { // Draw the energy diagram background(0); // clear (black) background stroke(255, 255, 255, 255); for (int x = 0; x < 24*12; x++){ line(2*x,scrHeight,2*x,scrHeight-dataCollection[x]); } for (int x = 0; x < 24*12; x++){ // evaluate last 24h cumulative last24h += dataCollection[x]; } MPosX = mouseX/2; if (MPosX >= 12*24) MPosX = 12*24-1; text(nf(MPosX/12,2)+":"+nf(5*(MPosX%12),2)+" kWh "+float(dataCollection[MPosX])/1000+" last 24 hours : kWh "+float(last24h)/1000,5,14); } float brightestValue = 0; // Brightness of the video pixel video.loadPixels(); int index = 0; blink = 0; for (int y = 0; y < imgHeight; y++) { for (int x = 0; x < imgWidth; x++) { // Get the color stored in the pixel int pixelValue = video.pixels[index]; // Determine the brightness of the pixel float pixelBrightness = brightness(pixelValue); if ((x > scopeX1) && (x < scopeX2) && (y > scopeY1) && (y < scopeY2)){ if(pixelBrightness > 240){ // print(pixelBrightness); blink = 1; } } index++; } } if ((blinkWas == 0) && (blink == 1)) { dengs++; dataCollection[binIs]++; // incrementa kWh println(dataCollection[binIs]); blinkWas = 1; // increment just one per frame } if (blink == 0) { blinkWas = 0; } } } void mousePressed() { scopeX1 = mouseX; scopeY1 = mouseY; } void mouseReleased() { scopeX2 = mouseX; scopeY2 = mouseY; } void keyPressed() { tasto = keyCode; }