State machine min an max example

Hello,

i want to make an arduino program with Local Minimum and Local Maximum. I have waves and i want to define a parameter for the maximal and the minimal value in each wave. The rest i can prog, but how i get the parameters with a state machine? Did anyone have an example code to adjust?

why is a state machine involved?

initialize the max/min to the initial point and update each with each new point

Thanks for your reply. I get an IR value in waves form so i don't set the waves, i read it via input on the arduino. I will post the example code in the next minuts. :slight_smile:

/*
  MAX30105 Breakout: Output all the raw Red/IR/Green readings
  By: Nathan Seidle @ SparkFun Electronics
  Date: October 2nd, 2016
  https://github.com/sparkfun/MAX30105_Breakout

  Outputs all Red/IR/Green values.

  Hardware Connections (Breakoutboard to Arduino):
  -5V = 5V (3.3V is allowed)
  -GND = GND
  -SDA = A4 (or SDA)
  -SCL = A5 (or SCL)
  -INT = Not connected

  The MAX30105 Breakout can handle 5V or 3.3V I2C logic. We recommend powering the board with 5V
  but it will also run at 3.3V.

  This code is released under the [MIT License](http://opensource.org/licenses/MIT).
*/

#include <Wire.h>
#include "MAX30105.h"

MAX30105 particleSensor;

#define debug Serial //Uncomment this line if you're using an Uno or ESP
//#define debug SerialUSB //Uncomment this line if you're using a SAMD21

void setup()
{
  debug.begin(9600);
  debug.println("MAX30105 Basic Readings Example");

  // Initialize sensor
  if (particleSensor.begin() == false)
  {
    debug.println("MAX30105 was not found. Please check wiring/power. ");
    while (1);
  }

  particleSensor.setup(); //Configure sensor. Use 6.4mA for LED drive
}

void loop()
{
  debug.print(" R[");
  debug.print(particleSensor.getRed());
  debug.print("] IR[");
  debug.print(particleSensor.getIR());
  debug.print("] G[");
  debug.print(particleSensor.getGreen());
  debug.print("]");

  debug.println();
}

consider

#define MyHW
#ifdef MyHW
# include "sim.hh"

struct MAX30105 {
    int red;
    int redDir;

    MAX30105   (void) { redDir = 1; }
    bool begin (void) { return true; }
    int setup  (void) { return 0; }

    int getRed (void) {
        red += redDir;
        if (10 <= red || -5 >= red)
            redDir = -redDir;
        
        return red;
    }

    int getIR  (void) { return 0; }
    int getGreen (void) { return 0; }
};

#else
# include <Wire.h>
# include "MAX30105.h"
#endif


MAX30105 particleSensor;

#define debug Serial //Uncomment this line if you're using an Uno or ESP
//#define debug SerialUSB //Uncomment this line if you're using a SAMD21

char s [80];

int redMax;
int redMin;

// -----------------------------------------------------------------------------
void loop()
{
    int red = particleSensor.getRed();
    int ir  = particleSensor.getIR();
    int grn = particleSensor.getGreen();

    if (redMax < red)
        redMax = red;
    if (redMin > red)
        redMin = red;

    sprintf (s, " %6d %6d %6d", red, redMax, redMin);
    debug.print   (s);
    sprintf (s, ", %6d, %6d", ir, grn);
    debug.println (s);
}

// -----------------------------------------------------------------------------
void setup()
{
    debug.begin(9600);
    debug.println("MAX30105 Basic Readings Example");

    // Initialize sensor
    if (particleSensor.begin() == false)
    {
        debug.println("MAX30105 was not found. Please check wiring/power. ");
        while (1);
    }

    particleSensor.setup(); //Configure sensor. Use 6.4mA for LED drive
    redMax = redMin = particleSensor.getRed();
}
1 Like

Wow! I will try the code in the evening and will response you if the code works. Many thanks for your very helpful reply!

Can you may explain where i can find the "sim.hh"? :slight_smile: Regards

sorry. it's not needed in this case
it include dummy structs like your MAX30105 so i can simulate the code

1 Like

I have done some changes and try it with an other sensor, but im prety sure that the values are not correct, maybe you can have a look on it?

#define MyHW
#ifdef MyHW


# include <Wire.h>
#include <DFRobot_MAX30102.h>
#endif


DFRobot_MAX30102 particleSensor;

#define debug Serial //Uncomment this line if you're using an Uno or ESP
//#define debug SerialUSB //Uncomment this line if you're using a SAMD21

char s [80];


I get in the serial monitor values like this:

R=120140 redMax=121923 redMin=4444 AC=117479 DC=4444 PerfusionsIndex=2600
R=120168 redMax=121923 redMin=4444 AC=117479 DC=4444 PerfusionsIndex=2600
R=120106 redMax=121923 redMin=4444 AC=117479 DC=4444 PerfusionsIndex=2600
R=119254 redMax=121923 redMin=4444 AC=117479 DC=4444 PerfusionsIndex=2600
R=119322 redMax=121923 redMin=4444 AC=117479 DC=4444 PerfusionsIndex=2600
R=119615 redMax=121923 redMin=4444 AC=117479 DC=4444 PerfusionsIndex=2600
R=119591 redMax=121923 redMin=4444 AC=117479 DC=4444 PerfusionsIndex=2600
R=119601 redMax=121923 redMin=4444 AC=117479 DC=4444 PerfusionsIndex=2600
R=119628 redMax=121923 redMin=4444 AC=117479 DC=4444 PerfusionsIndex=2600


But i had to get a lower value in the perfusions index.  Normal the DC must be also the same value and the AC need to change. 

regards

why?
can you post the complete output? where the max and min are captured?

I will post the full serial monitor in the next post. The perfusions index must be in the range from ranging from 0.02% to 20% so he is to high. I want to calculate this: (a) Definition of perfusion index (b) PI distribution in test subjects. | Download Scientific Diagram (researchgate.net)

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