:o
arduino_gsr.ino (696 Bytes)
: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:
I wrote such a sketch, just tidying it up a bit, will post soon.
Code below implements the state machine outlined above.
// 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.