for (int thisPin = pin0; thisPin< pin1; thisPin++){
pinMode(thisPin,INPUT);
}
}
void loop(){
corriente();
}
void corriente(){
for (int thisPin = pin0; thisPin<pin1; thisPin++){ //read analog input
for(int i=10000; i>0; i--){ // SE HACE UN CONTEO PARA OBTENER UN PROMEDIO
sensorvalor = (analogRead(thisPin) -510);
valorSensor = valorSensor + pow(sensorvalor,2); //la suma del valor mas el valor elevado al cuadrado
}
i'm trying to save values read (kwh) in an array so that I can display each one on the srceen when I request it.
for example:
float data[100];
in this array I want to save variable "kwh" and then:
data[0]= ? // I want to one value from A0 (kwh)
data[1]=?? //I want to second value from A1 (kwh)
how to save data from analog input writing them in an array?
How you have started is not bad (if I have understood correctly):
float data[100];
in this array I want to save variable "kwh" and then:
data[0]= analogRead( A0 ); // I want to one value from A0 (kwh)
data[1]= analogRead( A1 ); //I want to second value from A1 (kwh)
// and then:
Serial.print( "data[0]=" ) ; Serial.println ( data[0] ) ;
Serial.print( "data[1]=" ) ; Serial.println ( data[1] ) ;
// etc.
bare in mind that an array of floats uses four bytes of memory for each entry. Your array of 100 floats takes 400 bytes.
for(int i=10000; i>0; i--){ // SE HACE UN CONTEO PARA OBTENER UN PROMEDIO
  sensorvalor = (analogRead(thisPin) -510);
  valorSensor = valorSensor + pow(sensorvalor,2); //la suma del valor mas el valor elevado al cuadrado
 }
Why do you need 10000 readings? Rather than using pow, I would use use sensorvalor*sensorvalor. How are your variables defined? Please post complete code.
#define pin0 A0
#define pin1 A1
#define pin2 A2
----------------------------
float inAn[100], ink;
//float inAn[]={A0,A1,A2,A3,A4};
int inkw;
float k0,k1,k2,k3,k4,k5;
float dutyCycle;
String cadena ="";
int sensorvalor = 0;Â Â
float valorSensor = 0,valorCorriente = 0;
float voltsporUnidad = 0.0032258064516129032258064516129; //para teensy 3.3/1023;Â //para arduino 0.004887585532746823069403714565;// 5%1023
float watts = 0, kwh=0;
float sensibilidad = 0.066; // ACS712_30 A
int tension = 127;Â //
------------------
void loop() {
for (int thisPin = pin0; thisPin<pin2; thisPin++){
Â
  for(int i=10000; i>0; i--){ //
   sensorvalor = (analogRead(thisPin) -509); //
   valorSensor = valorSensor + pow(sensorvalor,2);
  }
 valorSensor = (sqrt(valorSensor/ 10000)) * voltsporUnidad;
 valorCorriente = (valorSensor/sensibilidad);Â
 Â
  if(valorCorriente <= 0.095){Â
   valorCorriente = 0;
  }
 watts = valorCorriente*tension;
 kwh = (watts * 1) /1000;
 if (kwh<0.02) kwh=0.00;
 inkw=kwh;
Â
valorSensor =0;
Â
inAn[inkw]=kwh; //array to save kwh
k0=inAn[0]; //analog A0
Serial.println(k0);
delay(500);
k1=inAn[1];Â //analog A1
// Serial.println(k1);
//delay(500);
}
}
  inkw = kwh;
  valorSensor = 0;
  inAn[inkw] = kwh; //array to save kwh
This is not right. The inAn array is declared to hold 100 values in locations numbered 0 to 99. To do what you want to save 2 values you need to read the first value, do any calculations and save the result ro anAn[0]. Then read the second value, do any calculations and save the result to anAn[1] and so on.