Array of sensor values

Noobish question: Trying to print out sensor values from array.

It works for predefined value(someValue).
but soilValue only shows if I uncomment "sensorSet[0] = soilValue;" after reading analog PIN.

long previousMillis = 0;
long sensorInterval = 1000;
int someValue = 47;
int soilValue;
int sensorSet[] = {soilValue, someValue};

void setup() {
  Serial.begin(9600);
}

void loop() { 
  unsigned long currentMillis = millis();
  if(currentMillis - previousMillis > sensorInterval) {                
    previousMillis = currentMillis;
    sensorInterval = 60000;
       
    soilValue = analogRead(A14);
    
    //sensorSet[0] = soilValue;
    
    for (int i = 0; i < 2; ++i){
      Serial.println("+++++++START+++++++");
      Serial.println(sensorSet[i]);
      Serial.println("___________________");
      Serial.println(soilValue);
      Serial.println("___________________");
      Serial.println(i);
      Serial.println("++++++++END++++++++");
    }
  }
}

Here is what I get on serial:

+++++++START+++++++
0
___________________
455
___________________
0
++++++++END++++++++
+++++++START+++++++
47
___________________
455
___________________
1
++++++++END++++++++

Do I need to store value to memory to show up for next loop?

It looks like you're suffering from a common misconception
This:
int sensorSet[] = {soilValue, someValue};

Does not tie soilValue to the array, it just takes the value of soilValue and stores it in the array when it is initialized. At that point, soilValue is zero, so the first element of the array is zero.

Later on you give soilValue a new value and it sounds like you're expecting that value to show up in your array. It won't, you have to explicitly assign it, as you observe.

why are you using an array?

are you using it to hold analog port numbers for multiple sensors or are you using it to store multiple sensor values collected over a number of intervals, or something else ??

Yes, trying to store multiple sensor values in to array, but I guess I have to assign them each time sensors are read as wildbill mentioned.

so how many samples do you want to capture and what do you want to do after you've captured that number of samples?

here's one possibility

long previousMillis = 0;
long sensorInterval = 1000;

#define N_SAMP 100
int sensorSet[N_SAMP];
int n = 0;

void setup() {
    Serial.begin(9600);
}

void dump() {
    Serial.println ("dump:");
    for (unsigned i = 0; i < N_SAMP; i++)
        Serial.println (sensorSet[i]);
    n = 0;
}

void loop() {
    unsigned long currentMillis = millis();

    if(currentMillis - previousMillis > sensorInterval) {
        previousMillis = currentMillis;

        sensorSet [n++] = analogRead(A14);

        if (N_SAMP == n)
            dump ();
    }
}

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.