how to save data from analog input writing them in an array?

:disappointed_relieved:
please, somebody help me
my code :

#define pin0 A0
#define pin1 A1

float kwh;

void setup() {

Serial.begin(9600);

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
}

valorSensor = (sqrt(valorSensor/ 10000)) * voltsporUnidad;

valorCorriente = (valorSensor/sensibilidad);

if(valorCorriente <= 0.095){ //SI LA CORIENTE ES MENOR A 0.095A, SE LE CONSIDERA 0A
valorCorriente = 0;
}
potencia = valorCorriente*tension;

kwh = (potencia * 1) /1000; // save values read in kwh

if (kwh<0.02){ kwh=0.00;}
//CALCULO PARA LA SUMA CONSECUTIVA
valorSensor =0;

Serial.println(kwh);
delay(500);
}
}

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?

ALISAYLI:
. . .

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.

@6v6gt , Thank You!
but i want to save "kwh" in an array... "kwh" is a result from sensor A0 and A1

Monitor serie:
//variable kwh
0.03 //A0
3.34 //A1

How I can to save kwh in an array so that I can display when I request it?

ALISAYLI:
@6v6gt , Thank You!
but i want to save "kwh" in an array... "kwh" is a result from sensor A0 and A1

data[] is an array. What do you really mean?

aarg:
data[] is an array. What do you really mean?

I want to save in an array each value:

Monitor serie:
//variable kwh
0.03 //A0
3.34 //A1

:frowning:

Please post your code in code tags

#define pin0 A0  //A0 ==14
#define pin1 A1  //A1 ==15

You have this following statement twice in you code

for (int thisPin = pin0; thisPin<pin1; thisPin++){ //read analog input

Your for loops are never working with pin1. Change it to

[code]for (int thisPin = pin0; thisPin<=pin1; thisPin++)
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.

I want to save in an array each value:

How many times do you want to save each pair of values ?

I want to do something like this;

  valorSensor =0;
  
float datos[100];
 datos[inkw]=kwh; //array to save kwh

 k0=datos[0]; //analog A0
 Serial.println(k0);
 delay(500);
 k1=datos[1];  //analog A1
Serial.println(datos[1]);
delay(500);

but I've problems because "k0" only to save values from A0 and A1.. k1 doesn't does anything

No, sorry, don't understand.

Edit: deleting the incomprehensible post helps no-one

I want to do something like this;

Code: [Select]

valorSensor =0;

float datos[100];
datos[inkw]=kwh; //array to save kwh

k0=datos[0]; //analog A0
Serial.println(k0);
delay(500);
k1=datos[1]; //analog A1
Serial.println(datos[1]);
delay(500);

but I've problems because "k0" only to save values of A0 and A1.. k1 doesn't does anything

I restored the posts you accidentally deleted.

I want to do something like this;

valorSensor =0;

float datos[100];
datos[inkw]=kwh; //array to save kwh

k0=datos[0]; //analog A0
Serial.println(k0);
delay(500);
k1=datos[1];  //analog A1
Serial.println(datos[1]);
delay(500);

but I've problems because "k0" only to save values of A0 and A1.. k1 doesn't does anything

Please see reply #8.

What is"inkw"?

Please stop deleting posts - you can edit them instead.

My 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.