Storing sensor values into an array (lie detection test)

Hello! I'm working on a lie detection test for an intro class.

As of right now I'm able to gather data from my pulse sensor and dht11 humidity sensor but need to store them into an array.

This is all to calibrate the lie detector

I will ask the recipient their name, while I'm doing so:
I will press a button-- as the button is being held down it will take the BPM of the recipient and put it into a size 10 array, I would also like for my humidity point to be placed into an array.
I also currently have a work in progress function to print an array of the 10 BPM values

#define USE_ARDUINO_INTERRUPTS true    // Set-up low-level interrupts for most acurate BPM math.
#include <PulseSensorPlayground.h>     // Includes the PulseSensorPlayground Library.   
#include <dht.h>
#define dht_apin A0 // Analog Pin sensor is connected to
 
dht DHT;

const int PulseWire = 0;       // PulseSensor PURPLE WIRE connected to ANALOG PIN 0
const int LED13 = 13;          // The on-board Arduino LED, close to PIN 13.
int Threshold = 550;           

int bpmArr[10];  
int humidityArr[10];
int arrayIndex = 0;

int buttonPin = 49;
int lastButtonState = 0;


PulseSensorPlayground pulseSensor;  // Creates an instance of the PulseSensorPlayground object called "pulseSensor"

void printlist(int *arr1){ //function to print BPM 

    for(int i = 0; i<10; i++){
        Serial.print(arr[i]);
        }
}
  
void setup() {   

  Serial.begin(9600);          // For Serial Monitor

  delay(500);                   //Delay to let system boot

  
  // Configure the PulseSensor object, by assigning our variables to it. 
  pulseSensor.analogInput(PulseWire);   
  pulseSensor.setThreshold(Threshold);   

  // Double-check the "pulseSensor" object was created and "began" seeing a signal. 
   if (pulseSensor.begin()) {
    Serial.println("We created a pulseSensor Object !");  //This prints one time at Arduino power-up,  or on Arduino reset.  
  }
}

void loop() {
  
DHT.read11(dht_apin);
int myBPM = pulseSensor.getBeatsPerMinute(); //calculated bpm

  if(buttonValue != lastButtonState){
    if(buttonValue == HIGH){
      
        if (pulseSensor.sawStartOfBeat()) {  // Constantly test to see if "a beat happened". 
    
              for(int i=0; i<10; i++){ //gathering 10 BPM values
                  bpmArr[i] = myBPM;
                  printlist(bpmArr);
                  delay(20); 
                }

            }
            
         for(int i=0; i<10; i++){  //gathering 10 humidity point values 
             humidityArr[i]= DHT.humidity;
             delay(5000); //it can only access the sensor every 2-5 seconds
                }
         }
  }
  lastButtonState = buttonValue;
}

Looks plausible, although I see it may disappoint you.

Do you have question yet? Is it doing something it shouldn't, or not doing something it should?

This peephole observation led me to make this changes to promtlist:

void printlist(int *arr1){ //function to print BPM 
{
   for (int i = 0; i< 10; i++) {
     Serial.print(arr[i]);
     Serial.print("   ");
   }
  Serial.println("");
}

I suggest you temporarily remove or comment out or otherwise forget the sensors and get the pushbutton to just print what it would do. I don't think the switch logic (which looks like you've thought about it and read some code) is going to be what you end up with.

This

              for(int i=0; i<10; i++){ //gathering 10 BPM values
                  bpmArr[i] = myBPM;
                  printlist(bpmArr);
                  delay(20); 
                }

does not gather ten values. It stores 10 identical values, the current value of myBPM. That will not change without executing something like

   myBPM = pulseSensor.getBeatsPerMinute();

but are ten values solicited in 200 milliseconds meaningful anyway in the context of measuring heart rate?

And this

         for(int i=0; i<10; i++){  //gathering 10 humidity point values 
             humidityArr[i]= DHT.humidity;
             delay(5000); //it can only access the sensor every 2-5 seconds
                }

will go away for nearly a minute, nothing else will happen in your sketch, and again, you've only stored ten copies of the same value.

Sensors must be read again if they are to report on changes to whatever is being. Sensed.

At a glance.

a7

Thanks for your insights. My problem was the sensor values weren't begin stored properly in the array, but It's fixed now! Also went with different button logic too.

#define USE_ARDUINO_INTERRUPTS true    
#include <PulseSensorPlayground.h>     

//  Variables
const int PulseWire = 0;       
const int LED13 = 13;          
int Threshold = 550;          
                              
int bpmArr[100];
int count = 0;

int button = 2; //calibration button
int state = 1; 

int button2 = 3; //instant detection button
int state2 = 1; 

float avg;

PulseSensorPlayground pulseSensor;  // Creates an instance of the PulseSensorPlayground object called "pulseSensor"


//Will print the array of BPM values 
void printBPM(int *arr){

  for(int i = 0; i<10; i++){
    
  if(arr[i]==0){
    break;
  }

    Serial.print(arr[i]);
    Serial.print(", ");
  }
  Serial.println();    
}


//averages the sum of the calibration
int average(int *arr){

    long sum = 0; 
    int i = 0;
    for(i; i<100; i++){
        if(arr[i] == 0){
             break;
            }
       sum += arr[i];
   }

  return avg = (sum / i-1);
  
  Serial.println(avg);  
}
  
void setup() {   

  Serial.begin(9600);         
  pinMode(button, INPUT_PULLUP);
  pinMode(button2, INPUT_PULLUP);
  pinMode(LED13,OUTPUT);

  
 // Configure the PulseSensor object, by assigning our variables to it. 
  pulseSensor.analogInput(PulseWire);   
  pulseSensor.blinkOnPulse(LED13);       //auto-magically blink Arduino's LED with heartbeat.
  pulseSensor.setThreshold(Threshold);   

// Double-check the "pulseSensor" object was created and "began" seeing a signal. 
   if (pulseSensor.begin()) {
    Serial.println("We created a pulseSensor Object !");  
  }
}

void loop() {

int myBPM = pulseSensor.getBeatsPerMinute();
 

//while button is pressed collect BPM Values, then take the average
  if(digitalRead(button) ==0){

    if(state ==1){
      if (pulseSensor.sawStartOfBeat()) {            // Constantly test to see if "a beat happened". 

         bpmArr[count] = myBPM; 
         count+=1;
         printBPM(bpmArr);
         average(bpmArr);
         state = 0;
    } 
    
    }else {
        state = 1;
        digitalWrite(LED13, LOW);
    } 
 }

delay(10);
}

To calibrate and get a baseline BPM I will be asking the recipient their name (while holding down the button for 10ish beats). During this press their BPM values will be stored into an array and averaged with each additional addition.

For the lie detection part, when a question is answered I want a button to be pressed that will evaluate the BPM of that instance (but really the most recent 10 BPM values will be put into a different array and averaged). That will be compared to the baseline average and indicative of a lie or not! I may follow up with more questions when I get to that part. Holding off with humidity for now.

You have moved well into looking better territory, and I repaet that it is obvious you are actually thinking about this. Trust me, this is not always the case with ppl who seek help here. :expressionless:

I'll look closer L8R. Just now I don't see that count is ever reset to zero and will therefor go merrily beyond the end of the array. Bad things will ensue.

The array is 100 long, but it seems that you are only really interested in ten values, or in one case I spotted, logic looking at only as many as have been "filled in", that is to say not zero.

I think the button logic will change again, moving ever closer to standard patterns you may be riffing on.

a7

Can you do it a bit differently?

When a button is pushed, start an automatic 10 (or whatever) second process of reading and storing and whatever else you wanna do with the sensors?

Or start it with a button press and let it do its thing until you press the button again?

Just thinking in transit here.

a7

As fallible as lie detection systems are, I'm trying to make this as robust as possible given the two days I have left haha. I'm comfortable with the button system bc it's working thus far and integrating the time aspect concerns me a little lol, but there is definitely touching up needed. I was dismissive of the count's needs, so thanks for that heads up. I'm now working on the second array, which holds all the BPM values while the program is running. Then, when the detection button is pressed-- it takes the average of the five most recent BPM values to compare to the calibration BPM value.