Take a reading from GSR sensor for 30 seconds

:o

arduino_gsr.ino (696 Bytes)

The delay function is in milliseconds. So you need 3000 miliseconds per loop to get a total of 30 seconds.

Your average is a int type variable. That means it is a whole number. Using float will allow you to have more precise average results.

If you have enought time and interest to make the solution more elegant and flexible, let me know and I will give a few pointers.

It seems you are running this measurement only once.
If so, then you can put everything in setup().

Try this test sketch.
It measures/stores continuously, and divides at the end by the number of measurements it has taken.
The reset button on the Uno starts a new measurement.
Leo..

const byte sensorPin = A0;
unsigned long startMillis;
unsigned long runTime = 30000; // in milliseconds
unsigned long sensorValue; // holds up to ~400 seconds of continuous A/D readings
unsigned int count;

void setup() {
  Serial.begin(9600);
  Serial.print(F("Measurement running for "));
  Serial.print(runTime / 1000);
  Serial.println(F(" seconds"));
  Serial.println(F("please wait..."));

  startMillis = millis(); // mark time
  
  while (millis() - startMillis < runTime)
  {
    sensorValue += analogRead(sensorPin);
    count ++;
  }
  Serial.print(F("GSR average: "));
  Serial.println(sensorValue / count);
  Serial.println();
}

void loop() {
}

It seems to me that a nice tidy solution would be a state machine:

  • After setup(), start in stateIdle, waiting for a button press to start the readings (much nicer than having to restart the Arduino each time.) On button press switch to...
  • stateReading, doing the readings every "x" apart and keeping running total, for "y" total time, and when "y" is up switch to...
  • stateDisplay (see other thread where OP mentions this), calculate the average and display for "z" time. When "z" is up, go back to...
  • stateIdle to await a new button press for a new cycle.

I wrote such a sketch, just tidying it up a bit, will post soon.

Code below implements the state machine outlined above.

  • Set the variables marked XXXX to suit real life
  • My 7-seg is a bare bones 2-digit bd-f305rd and I used the sevseg library from the Playground
  • I just used random(1,100) to generate readings as a proxy for the sensor
  • Check that your real life readings, average and running total fit the data types I used
  • It's all non-blocking, and L13 bwods to prove that
  • There's a bool debug near the top to turn serial prints on or off
  • The sev seg displays 0 while in idle, 99 while taking readings, then the average for a short while
// gsr read, avearge and display https://forum.arduino.cc/index.php?topic=650026 and https://forum.arduino.cc/index.php?topic=649955
// has bwod proof of life on L13
// this example uses random(1,100) as a proxy for the actual sensor

//seven segment display stuff
// my display is a loose bd-f305rd (2-digit cc)
#include <SevSeg.h>
SevSeg sevseg; //Instantiate a seven segment controller obj

bool debug = true ; //true or false to print / not print to Serial, need to set this XXXXXXX

//states
enum {state_idle, state_reading, state_display} currentState = state_idle;
unsigned long startingNewStateAt;
int displayStateLength = 1000; // need to set this XXXXXXX
int readStateLength = 5000; // need to set this XXXXXXX

//readings
unsigned long lastReadingAt;
int readInterval = 500; // need to set this XXXXXXX
byte howManyReadsDone = 0;
int average;
int runningTotal;
int newVal;

//the pulse led
int pulseLedInterval = 500;
unsigned long previousMillisPulse;
bool pulseState = false;

//buttons
const byte startPin = 14;

void setup()
{
  // initialize serial communication:
  Serial.begin(9600);
  Serial.println("setup() ... ");
  Serial.println("gsr read, average, display");
  Serial.print("Compiler: ");
  Serial.print(__VERSION__);
  Serial.print(", Arduino IDE: ");
  Serial.println(ARDUINO);
  Serial.print("Created: ");
  Serial.print(__TIME__);
  Serial.print(", ");
  Serial.println(__DATE__);
  Serial.println(__FILE__);

  //sev seg
  byte numDigits = 2;
  byte digitPins[] = {2, 3};
  byte segmentPins[] = {4, 5, 6, 7, 8, 9, 10, 11}; //Segments: A,B,C,D,E,F,G,Period
  sevseg.begin(COMMON_CATHODE, numDigits, digitPins, segmentPins);
  sevseg.setBrightness(10); //doesn't seem to make much diffs
  // with resistors on the digits, check in sevseg.h that
  //#define RESISTORS_ON_SEGMENTS 0

  //initialise pulse led
  pinMode(LED_BUILTIN, OUTPUT);
  digitalWrite(LED_BUILTIN, pulseState);

  randomSeed(analogRead(0)); //going to use random as a proxy for the sensor

  //buttons
  pinMode(startPin, INPUT_PULLUP);
  if (debug)
  {
    Serial.println("setup() done");
    Serial.println(" ");
    Serial.println("Press button to start");
  }
}//setup

void loop()
{
  sevseg.refreshDisplay(); //must not block this
  proofOfLife();
  manageStates();
} //loop


void manageStates()
{
  switch (currentState)
  {
    case state_idle:
      sevseg.setNumber(0, 0); // 0 just to show we're in idle

      if (!digitalRead(startPin))
      {
        if (debug) Serial.println("Starting readings");
        startingNewStateAt = millis();
        currentState = state_reading;
        howManyReadsDone = 0;
        runningTotal = 0;
        average = 0;
      }
      break;

    case state_reading:
      sevseg.setNumber(99, 0); // 99 just some value to show we're reading

      if (millis() - lastReadingAt >= readInterval)
      {
        lastReadingAt = millis();
        newVal = random(0, 100); //random is a proxy for the gsr sensor
        howManyReadsDone++;
        runningTotal = runningTotal + newVal;
        if (debug)
        {
          Serial.print("Readings: "); Serial.print(howManyReadsDone);
          Serial.print(", Last reading: "); Serial.print(newVal);
          Serial.print(", Running total: "); Serial.println(runningTotal);
        }
      }

      if (millis() - startingNewStateAt >= readStateLength )
      {
        average = runningTotal / howManyReadsDone;
        startingNewStateAt = millis(); //for next state
        currentState = state_display;
        if (debug)
        {
          Serial.print("Average: "); Serial.println(average);
        }
      }

      break;

    case state_display:
      sevseg.setNumber(average, 0);

      if (millis() - startingNewStateAt >= displayStateLength)
      {
        currentState = state_idle;
        if (debug) Serial.println("Press button to start");
      }

      break;

  }//switch
}//manageStates

void proofOfLife()
{
  if (millis() - previousMillisPulse >= pulseLedInterval)
  {
    previousMillisPulse = millis();
    pulseState = !pulseState;
    digitalWrite(LED_BUILTIN, pulseState);
  }
} //proofOfLife

OP's query is reduced to a :o , it seems.

See new question here by one TiffanyL.


Here's the state diagram for that code, btw.