Using millis to measure intervals to trigger a digital output

Hi,

I'm currently trying to figure out how to use millis to measure the time between each blink of an LED (the blink being caused by a pulse sensor with an analog input) and take this information to trigger a digital pin if the interval is greater than 500.

Here's my code:

int PulseSensorPurplePin = 0;
int ledPin = 13;
int SHOCK = 9;

int Signal;
int Threshold = 550;
unsigned long previousMillis = 0;
unsigned long startMillis;
const unsigned long interval = 500;

// The SetUp Function:
void setup() {
pinMode(SHOCK,OUTPUT);
pinMode(ledPin, OUTPUT);
Serial.begin(9600);

}

void loop() {

Signal = analogRead(PulseSensorPurplePin);
unsigned long currentMillis = millis();
Serial.println(Signal);

if(Signal > Threshold){
digitalWrite(ledPin,HIGH);
} else {
digitalWrite(ledPin,LOW);
}

if(currentMillis - previousMillis > interval){
digitalWrite(SHOCK,HIGH);
} else {
digitalWrite(SHOCK,LOW);
}
delay(10);
}

What does your program actually do? And what do you want it to do that is different?

The demo Several Things at a Time illustrates the use of millis() to manage timing. It may help with understanding the technique.

...R

PS... To make it easier for people to help you please modify your post and use the code button </> so your code looks like this and is easy to copy to a text editor. See How to use the Forum

My code will be used to measure the pulse of the user. If their pulse is below a certain threshold, the user will be shocked by a dog collar until they get their heart rate back up. Currently the code just repeatedly turns pin 9 on and off and does not stop.

Also thanks for the tips!

int PulseSensorPurplePin = 0;
int ledPin = 13;
int SHOCK =  9;

int Signal;
int Threshold = 550; 
unsigned long previousMillis = 0;
unsigned long startMillis;
const unsigned long interval = 500;

// The SetUp Function:
void setup() {
  pinMode(SHOCK,OUTPUT);
  pinMode(ledPin, OUTPUT);
   Serial.begin(9600);
   
}

void loop() {

  Signal = analogRead(PulseSensorPurplePin); 
                                         
  unsigned long currentMillis = millis();
   Serial.println(Signal);

   
   if(Signal > Threshold){  
     digitalWrite(ledPin,HIGH);       
   } else {
     digitalWrite(ledPin,LOW);
   }

   if(currentMillis - previousMillis > interval){       
     digitalWrite(SHOCK,HIGH);       
   } else {
     digitalWrite(SHOCK,LOW);
   } 
delay(10);
  }

mcclocu:
My code will be used to measure the pulse of the user. If their pulse is below a certain threshold, the user will be shocked by a dog collar until they get their heart rate back up.

That sounds quite dangerous.

I have experience the shock myself and it's not bad, it's only powerful enough to startle you. There's also a vibration and beep function which I will most likely be using instead of the shock.

I am literally the only person this will be demonstrated on. It's not any different than those shock games that shock the user if they lose. I don't see what problem you have here.

Uh oh, someone's triggered!

Can you at least explain to me what your problem is with the device? The collar will be warn on the arm. It fastens via a clip that isn't locked and is easily removed. The collar is also designed for puppies so the shocks that it administers are minimal.

I never said you had to help, did I? I asked you what your issue with the device was. I have already developed the device and had it looked over by my professor, so I can assure you it isn't me being a "n00b" that makes it dangerous. I know what I am doing with this device, so you assuming that I don't is a completely ignorant statement. I came to the forum to ask a question about how you would go about writing a specific part of the code and help me troubleshoot what I had already written and wasn't asking how to build this device from scratch.

Once again, I never said you needed to help. I consulted a FORUM to see if SOMEONE would help. I am just shocked by your response to this device. And nope, I don't see the problem with issuing a shock to people, especially when that person is myself, and especially when the device can be easily removed. You're treating this like it's a lethal device (which I just find interesting to say the least) when the collar was designed for puppies.

You are using an analog read so you must have a value which is the ceiling of when the heart pulses and a low value for when it’s in between the pulse.

I think you should start your if statement with the value of the lower range and within that put a millis time reset. Put the next if inside of that if statement for the if millis is greater than the interval so you can change the state of the output pin which triggers the shock when the interval of 500 is hit to high.

Use and else if saying the upper range that corresponds to a beat and make that set the output state to low. If the heat beats quick enough the state will always be low and f it’s longer then your set interval then it shocks.

On the top of the loop digital write the shock pin to the output state outside and separate from the if statements.

I would reference the blink without delay to understand how to write the states. Only in your case your using an else if rather then an else.

Thanks man! It totally worked. My sensor is a bit buggy though so the project doesn't work the greatest but it does occasionally!

I would probably also add a timer so if the pin is on for a certain interval like 2 seconds it turns back off