Creating Sensor Array Data in 2 alternate arrays

Dear Members,

I have 18 sensors which are to be read in 2 arrays (A and B). Each array consists of 9 sensor values (A1,A2...A9). Similarly for array B(B1,B2...B9). For the time being, I don't have sensors so I am testing it with random values.

So I want to fill in the same array in an alternative way. For e.g. First, the array creates 9 values for A like A1,A2,..A9 and after the 9 values are created, the array should create 9 random numbers in the same array for B like B1,B2,...B9 and after B's 9 values are created then it should create 9 values in the same array for A and so on.

Here is my piece of code that I have tried. Please give some guidance on how to do it. Thank you.

float sensor[9];

void setup() {
  Serial.begin(9600);
  randomSeed(analogRead(0));
}

void loop() {
  unsigned long presentMillis = millis();
  while (millis() - presentMillis < 500) //500 ms
  {
    ;   //wait here
  }
  presentMillis = millis();
  for(int k = 0;k < 9;k++){
    if (sizeof(sensor)<9){
    sensor[k] = random(0,3.5);
    Serial.print("A"+ String(k));
    Serial.println(sensor[k]);
    //Serial.println(" ");
    }
    else{
    sensor[k] = random(0,3.5);
    Serial.print("B"+ String(k));
    Serial.println(sensor[k]);
      }
  }
  delay(1000);
  //Serial.println();
}
float sensor[9];
...
...
if (sizeof(sensor)<9)

What is the intention here?
sizeof(sensor) is going to be 36.

Instead of:

  unsigned long presentMillis = millis();
  while (millis() - presentMillis < 500) //500 ms
  {
    ;   //wait here
  }
  presentMillis = millis();

why don't you just use delay(500);?

This will never be true:

    if (sizeof(sensor)<9){

Therefore the block of code associated with that will never be executed; indeed, the compiler will optimize that out.

If all you are doing is flip-flopping which sensor to read then you can use a boolean flag like this:

  if (sensorFlag)
  {
    // read from sensor A
  }
  else
  {
    // read from sensor B
  }
  sensorFlag = !sensorFlag;

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