Skip to content

Instantly share code, notes, and snippets.

@Arty2
Created August 8, 2018 15:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Arty2/6d8cbd4b629cdfd3abfd7c0b58dbf004 to your computer and use it in GitHub Desktop.
Save Arty2/6d8cbd4b629cdfd3abfd7c0b58dbf004 to your computer and use it in GitHub Desktop.
Arduino sketch: Pulse sensor by means of a photoresistor • Breathing sensor by means of resistance sensor fabric
//pins
const int led = 13; //calibration LED
const int vibrator = 12;
const int buzzer = 11;
const int pulseSensor = A0; //LDR
const int breathSensor = A1; //resistive fabric
//pulse sensor variables
int pulseMin = 1023;
int pulseMax = 0;
int pulse = 0;
//breath sensor variables
int breathBase = 0;
int breathMax = 0;
int breath = 0;
//timers
int calibration = 5000; //calibration time
int interval = 100; //sampling time
long previousMillis = 0;
int count = 1; //callibration loop
int once = 0;
void setup() {
Serial.begin(9600);
pinMode(vibrator, OUTPUT);
pinMode(buzzer, OUTPUT);
pinMode(led, OUTPUT);
}
void loop() {
unsigned long currentMillis = millis();
//START calibration
while (millis() < calibration) {
//breath sensor min & max
pulse = analogRead(pulseSensor);
if (pulse > pulseMax) {
pulseMax = pulse;
}
if (pulse < pulseMin) {
pulseMin = pulse;
}
// Serial.print(pulseMin);
// Serial.print(" - ");
// Serial.print(pulseMax);
// Serial.print(" - ");
Serial.println(pulse);
//breath sensor min & max
breath = analogRead(breathSensor);
if (breath > breathMax) {
breathMax = breath;
}
breathBase += breath;
// Serial.println(breath);
//notify the user that we are callibrating
analogWrite(led, 100);
delay(50);
count++;
}
if (once == 0) {
breathBase = breathBase / count;
analogWrite(led, 0);
once = 1;
}
//END calibration
//normalize execution time
if (currentMillis - previousMillis > interval) {
pulse = analogRead(pulseSensor);
//normalize values
pulse = map(pulse, pulseMin, pulseMax, 0, 255);
pulse = constrain(pulse, 0, 255);
// Serial.print(pulseMin);
// Serial.print(" - ");
// Serial.print(pulseMax);
// Serial.print(" -- ");
Serial.println(pulse);
breath = analogRead(breathSensor);
//normalize values
breath = map(breath, breathBase, breathMax, 0, 255);
breath = constrain(breath, 0, 255);
if (pulse > 300) {
// analogWrite(vibrator, pulse);
}
analogWrite(vibrator, breath);
// Serial.print(breathBase);
// Serial.print(" - ");
// Serial.print(breathMax);
// Serial.print(" -- ");
// Serial.print(analogRead(breathSensor));
// Serial.print(" -- ");
// Serial.println(breath);
previousMillis = currentMillis;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment