Sampling/Average Data with Processing or Arduino IDE

Hi!

Right now I´m stucked in this proyect: I want to do a "Current-Only" Opensource Monitor as seen on this Site www.openenergymonitor.com (Basically CT sending a Voltage U to Arduino). I don´t use the same circuit but it works well--> I wrote a Arduino-Sketch that works very well:

/** Berechnung der Scheinleistung S

Dieser Arduino Sketch ist eine größe Vereinfachung für
die Berechnung von Scheinleistung. An der analogesPin 0 
des Arduinos ist einer Schaltung angeschlossen (s.Einleitung)
mit einen Stromwandler der einen Wechselspannung proportional
zur in der Leitung fliessende Strom ausgibt.
Wir berechnen Scheinleistung durch Anwendung der quadratische
Mittelwert aus Strom(s.unter Berechnung)

Contribution: Eric Merckel+Beispiel Sketches aus pachubeLm335zTemperaturSensor_V6,
AnalogSerialRead

*/

// Definition der Variablen.................................
float w; //analoge Wert (zw. 0 und 1023) aus die Sensorschaltung
int numberOfSamples=3000;// n=3000, Anzahl der Wiederholungen
float wquad;
float wsum;
float wqm;
double wkorr;// Bzw. gemittelte analoger Wert mit Korrekturfaktor
double U;// Analoge Wert entspricht eine Spannung U
double Uf=0.00488758; //Umwandlungsfaktor 5V/1023
double Uum=3.1; //Umw.faktor für Strom->Aus Datenblatt der Stromwandler CR 3110-3000
double I;// Effektive Strom
double Uleit=240;//Annahme für eff.Spannung
double S; //Scheinleistung

void setup()
{
   Serial.begin(300); 
}
void loop()
{ 
for (int n=0; n<numberOfSamples; n++)

{
w=analogRead(A0);
wquad=w*w;
wsum += wquad;  //summiere alle wquad (über 3000 Messungen)
}

wqm=(sqrt(wsum/numberOfSamples));//Bildung des Mittelwertes
wkorr=wqm;// Hier konnte man um X-Faktor der Wert korrigieren 
U= wkorr*Uf;
I=U*Uum;
S=I*Uleit;
//Druck die Werte ins Serial-Monitor Fernster
Serial.println("analogeWertwkorr");
Serial.println(wkorr, DEC);
Serial.println("SpannungU");
Serial.println(U,DEC);
Serial.println("StromI");
Serial.println(I,DEC);
Serial.println("ScheinleistungS");
Serial.println(S,DEC);

wsum = 0;//Summe sollte wieder Null sein, neu Start
}

I understand the maths on it, but lack of programming knowlegde. Here is the problem:
I want to do the same sampling to calculate the root mean squares on Processing so I can push the Data
into Pachube.
I m trying to translate the Arduino Sketch into Processing to work for the interfacing with Pachube and Firmata.
I loaded the Firmata on my Arduino and loaded the libraries on the Processing Sketch, so my Arduino sends raw "w" (analog Value), and Processing gets it. So it does, but it fail on doing the sampling part:

import processing.serial.*;
import cc.arduino.*;
import eeml.*;

Arduino arduino;
float w;
int numberOfSamples=3000;
float wquad;
float wsum;
float wqm;
double wkorr;
double U;
double Uf=0.00488758;
double Uum=3.1;
double I;
double Uleit=240;
float S;
float lastUpdate;

DataOut dOut; 

void setup()
{
  println(Arduino.list());
  arduino = new Arduino(this, Arduino.list()[2], 57600);
  
  dOut = new DataOut(this, "XML SITE HERE" , "API-KEY"); 
  dOut.addData(0,"Temperatur");
}
void draw()
{
  for (int i=0; i<3000; i++)
{
	w = arduino.analogRead(0);
        wquad=w*w;
        wsum += wquad;
}
   wqm=(sqrt(wsum/3000));
   U=wqm*Uf;
   I=U*Uum;
   S=I*Uleit;
    println(S);

    if ((millis() - lastUpdate) > 180000){
        println("ready to PUT: ");
        dOut.update(0, S); 
        int response = dOut.updatePachube(); 
        println(response); 
        lastUpdate = millis();
    }   

}

void onReceiveRequest(DataOut d){ 
    d.update(0, S); 
}

My problem is the loop-Block--> How can I do this sampling/avering calculation on Processing?
The first step is of course changing void loop for void draw, but should I change something else, maybe the variiable types or build an array? thanks!!!!

So it does, but it fail on doing the sampling part:

It fails how? Does it overflow the wquad variable? Does it fail to read the sensor enough times?

It calculates the apparent Current S but not with the RMS w value. Processing gets the Data (Arduino IDE+FIRMATA O.k.), but it
doesn´t sums the 3000 Samples. My simple idea was to programm this equation:

But with the w (analog Values), and then doing the rest transformations...

Thanks for the reply, it´s kind of a long post!