Comparing real-time sensor data in an array

Hi guys,

I am using an arduino to monitor the voltage output from a gas sensor. I want the arduino to trigger a warning when the gas sensor outputs a prespecified voltage. I am relatively new to software and wondered what the best way to compare this data was.

My initial plan was to use a for loop to update the array with 20 values, and compare each value against this voltage limit. Expelling one element from the array, shifting the other 19 down one and then updating the final value seems quite computationally heavy but I dont really know of a better way to do it as my experience is very limited.

Like i say, i am just getting started with software so if there are any easier/more efficient ways to do this I would love to hear them.

Thanks very much

based on your description

I want the arduino to trigger a warning when the gas sensor outputs a prespecified voltage

I don't see why you need an array of 20 values.

the (pseudo-)code could be

vGas = readGasSensorVoltage();
if (vGas >= prespecifiedVoltage) {
  // alert !!
}

So what's the story for your 20 values ?
also reorganizing buffer is pretty fast probably compared to how slow reading a gas sensor is... (and you could use circular buffer to not have to move things around in the array)

Sorry i should have been more clear, just due to the environment and noise in the system I want to ensure that it is indeed above the value. One value being above the threshold may just be a signal spike, granted 20 may be overkill. I'll look into a circular buffer, thanks

Ahh, you want to time it's over the limit for some time. Still don't need an array of all values though. Just count how many readings (which are spaced in time) are above the threshold. And if you see one that's below the threshold just reset the count. Next, trigger the alarm when count is above the desired number.

septillion:
Just count how many readings (which are spaced in time) are above the threshold. And if you see one that’s below the threshold just reset the count. Next, trigger the alarm when count is above the desired number.

Thanks very much, that works brilliantly.

I am also trying to build a check into the setup to ensure the device is working before going into the loop.

void setup() {
  Serial.begin(9600);
  pinMode(A0, INPUT);
  pinMode(A1, INPUT);
  InternalVoltage = readInternalVoltage();
  OpAmpAnalogValue = analogRead(OpAmpPin);
  TestPinAnalogValue = analogRead(TestPin);
  OpAmpVoltage = (OpAmpAnalogValue / 1024.0) * InternalVoltage - OpAmpVoltageOffset;
  Serial.println(OpAmpVoltage);
  TestPinVoltage = (TestPinAnalogValue / 1024.0) * InternalVoltage - TestPinVoltageOffset;

  if (OpAmpVoltage >= 4900 || OpAmpVoltage <= -50){
    ErrorState = true;
  }
  else {
    ErrorState = false;
  }
}

void loop() {

  if (ErrorState = true) {
    Serial.println("ERROR");
  }

  else if (ErrorState = false) {
    InternalVoltage = readInternalVoltage();
    
    etc etc etc

The serial monitor prints an OpAmpValue of 4742.04, yet it still sets the ErrorState to true and constantly prints error. Again, it’s likely just due to my lack of programming knowledge and a small error I haven’t noticed.

Cheers

= is for assignment and == is for comparison…

  if (ErrorState [b][color=red]=[/color][/b] true) {
    Serial.println("ERROR");
  } else if (ErrorState [b][color=red]=[/color][/b] false) {

and if ErrorState is a boolean, you don’t really need to compare with true or false, just do

  if (ErrorState) {
    Serial.println("ERROR");
  } else { // if it's not true, then it's false :)
    InternalVoltage = readInternalVoltage();
...

also this part

if (OpAmpVoltage >= 4900 || OpAmpVoltage <= -50){
    ErrorState = true;
  }
  else {
    ErrorState = false;
  }

could be rewritten this way ErrorState = (OpAmpVoltage >= 4900 || OpAmpVoltage <= -50);

J-M-L:
= is for assignment and == is for comparison...

  if (ErrorState [b][color=red]=[/color][/b] true) {

Serial.println("ERROR");
  } else if (ErrorState = false) {




and if `ErrorState` is a boolean, you don't really need to compare with true or false, just do

if (ErrorState) {
    Serial.println("ERROR");
  } else { // if it's not true, then it's false :slight_smile:
    InternalVoltage = readInternalVoltage();
...

Thanks very much

If all you want to do is print error and halt I would simply do:

void setup() {
  Serial.begin(9600);
  pinMode(A0, INPUT);
  pinMode(A1, INPUT);
  InternalVoltage = readInternalVoltage();
  OpAmpAnalogValue = analogRead(OpAmpPin);
  TestPinAnalogValue = analogRead(TestPin);
  OpAmpVoltage = (OpAmpAnalogValue / 1024.0) * InternalVoltage - OpAmpVoltageOffset;
  Serial.println(OpAmpVoltage);
  TestPinVoltage = (TestPinAnalogValue / 1024.0) * InternalVoltage - TestPinVoltageOffset;

  if (OpAmpVoltage >= 4900 || OpAmpVoltage <= -50){
    Serial.println(F("ERROR OpAmp"));
    while(true);
  }
}

void loop() {
  InternalVoltage = readInternalVoltage();
 
  etc etc etc