// These constants won't change. They're used to give names // to the pins used: const int analogInPin = 2; // Analog input pin that the pulse sensor is attached to (physical pin 2, Arduino pin 4, analogRead(2) according to blog) const int analogOutPin = 1; // Analog output pin that the LED is attached to int sensorValue = 0; // value read from the pulse sensor int outputValue = 0; // value output to the PWM (analog out) int maxval = 0; // want a maximum set according to the incoming values (which will change due to many outside factors). Start at 0 to allow incoming data to define maximum int minval = 1023; //start at 1023 (max possible analog value) to allow incoming data to set the minimum int counter = 0; //counter to allow forced reset every few seconds (to prevent outliers/times when you aren't wearing the sensor from creating permanent outlier max and min vals. void setup() { pinMode(analogInPin, INPUT); pinMode(analogOutPin, OUTPUT); } void loop() { if(counter==5000){ //Reset max and min values approximately every 5 seconds counter = 0; maxval = 0; minval = 1023; } // read the analog in value: sensorValue = analogRead(analogInPin); //update mins and maxes if necessary: if(sensorValue > maxval){ maxval = sensorValue; } if(sensorValue < minval){ minval = sensorValue; } // map it to the range of the analog out: outputValue = map(sensorValue, minval, maxval, 0, 255); // change the analog out value: analogWrite(analogOutPin, outputValue); // wait 2 milliseconds before the next loop // for the analog-to-digital converter to settle // after the last reading: delay(2); counter++; //increment counter }