WorldFamousElectronics / PulseSensorPlayground

A PulseSensor library (for Arduino) that collects our most popular projects in one place.
https://PulseSensor.com
MIT License
200 stars 97 forks source link

Two pulse sensor with servo morter doesn't work #78

Closed daidaimar closed 5 years ago

daidaimar commented 5 years ago

Hi,

I'm following your Playground source code of "TwoPulseSensors_On_Onearduino" and "PulseSensor_Servo" and trying to create a project by combine the two example into "two pulse Sensor with a Servo" project.

However if I combine the both sample codes, the Arduino seems doesn't work, From my investigation, it appears if I remove the servo.h from header, the code would start working. below code is simply added Servo.h and created a variable in the sample code of "TwoPulseSensors_On_Onearduino", however the code won't work and Arduino would stop after a while. Do you have any idea ?

Env Arduino IDE version 1.8.5. Arduino Uno R3

// if I remove this Servo.h and remove the relevant sarvo variables, the code start working.
#include <Servo.h>

#define USE_ARDUINO_INTERRUPTS true
#include <PulseSensorPlayground.h>

const int OUTPUT_TYPE = SERIAL_PLOTTER;

const int PULSE_SENSOR_COUNT = 2;

const int PULSE_INPUT0 = A0;
const int PULSE_BLINK0 = 13;    // Pin 13 is the on-board LED
const int PULSE_FADE0 = 5;

const int PULSE_INPUT1 = A1;
const int PULSE_BLINK1 = 8;
const int PULSE_FADE1 = 11;

const int THRESHOLD = 550;   // Adjust this number to avoid noise when idle

PulseSensorPlayground pulseSensor(PULSE_SENSOR_COUNT);

Servo heart;
const int SERVO_PIN = 6;
int pos = 0;

void setup() {
  Serial.begin(250000);
  heart.attach(SERVO_PIN);
  heart.write(pos);

  // Configure the PulseSensor manager.  
  pulseSensor.analogInput(PULSE_INPUT0, 0);
  pulseSensor.blinkOnPulse(PULSE_BLINK0, 0);
  pulseSensor.fadeOnPulse(PULSE_FADE0, 0);

  pulseSensor.analogInput(PULSE_INPUT1, 1);
  pulseSensor.blinkOnPulse(PULSE_BLINK1, 1);
  pulseSensor.fadeOnPulse(PULSE_FADE1, 1);

  pulseSensor.setSerial(Serial);
  pulseSensor.setOutputType(OUTPUT_TYPE);
  pulseSensor.setThreshold(THRESHOLD);

  // Now that everything is ready, start reading the PulseSensor signal.
  if (!pulseSensor.begin()) {
    for(;;) {
     // Flash the led to show things didn't work.
      digitalWrite(PULSE_FADE0, LOW);
      digitalWrite(PULSE_FADE1, LOW);
      delay(50);
      digitalWrite(PULSE_FADE0, HIGH);
      digitalWrite(PULSE_FADE1, HIGH);
      delay(50);
    }
  }
}

void loop() {
  delay(20);

  // write the latest sample to Serial.
  pulseSensor.outputSample();

  for (int i = 0; i < PULSE_SENSOR_COUNT; ++i) {
    if (pulseSensor.sawStartOfBeat(i)) {
      pulseSensor.outputBeat(i);
    }
  }
}

thanks

biomurph commented 5 years ago

I'm currently away from my office this week, but the dual Pulse Sensor code should work fine with the servo code to control two servos with two Pulse Sensors. When I'm in my office, I can run some tests to find the bug, but often times when you use multiple servos on an Arduino Uno the current draw can make things tweak weirdly. To fix this problem you should use an external power supply for your servos. Here's a couple of examples.

https://www.instructables.com/id/Arduino-How-to-Use-a-Servo-Motor-With-an-External-/ Not sure about the unregulated power supply on that example.

https://captain-slow.dk/2010/10/19/servo-with-external-power/ This one uses an external power supply regulated to 5V. You can also use regulated 5V for the servos, and keep the Uno connected to your computer, while sharing a GND connection between the Uno and the servos.