I am trying to use both the servo motor and the heart rate sensor but without success
Here is the code I tried:
#define USE_ARDUINO_INTERRUPTS true // Set-up low-level interrupts for most acurate BPM math.
#include <PulseSensorPlayground.h> // Includes the PulseSensorPlayground Library.
#include "ServoTimer2.h"
ServoTimer2 servo1;
// Variables
const int PulseWire = 0; // PulseSensor PURPLE WIRE connected to ANALOG PIN 0
const int LED13 = 13; // The on-board Arduino LED, close to PIN 13.
int Threshold = 550; // Determine which Signal to "count as a beat" and which to ignore.
// Use the "Gettting Started Project" to fine-tune Threshold Value beyond default setting.
// Otherwise leave the default "550" value.
PulseSensorPlayground pulseSensor; // Creates an instance of the PulseSensorPlayground object called "pulseSensor"
void setup() {
Serial.begin(9600); // For Serial Monitor
servo1.attach(12);
// Configure the PulseSensor object, by assigning our variables to it.
pulseSensor.analogInput(PulseWire);
pulseSensor.blinkOnPulse(LED13); //auto-magically blink Arduino's LED with heartbeat.
pulseSensor.setThreshold(Threshold);
// Double-check the "pulseSensor" object was created and "began" seeing a signal.
if (pulseSensor.begin()) {
Serial.println("We created a pulseSensor Object !"); //This prints one time at Arduino power-up, or on Arduino reset.
}
}
void loop() {
int myBPM = pulseSensor.getBeatsPerMinute(); // Calls function on our pulseSensor object that returns BPM as an "int".
// "myBPM" hold this BPM value now.
if (pulseSensor.sawStartOfBeat()) { // Constantly test to see if "a beat happened".
Serial.println("♥ A HeartBeat Happened ! "); // If test is "true", print a message "a heartbeat happened".
Serial.print("BPM: "); // Print phrase "BPM: "
Serial.println(myBPM); // Print the value inside of myBPM.
}
servo1.write(0); //min pulse width for 0 degree
delay(1000);
servo1.write(90); //pulse width for 90 degree
delay(1000);
delay(20); // considered best practice in a simple sketch.
}
Although I do get pulse readings but the servo motor does not rotate. It jitters after each 2/3 sec. How can I achieve the execution of both sensors simultaneously, please?
I guess there was certainly an issue with the circuit.. However, although I do get both of them working the servo motor jitters a lot. This is most probably because of the heart rate sensor because it works fine otherwise. Any idea how can I fix it please?
If you remove the pulse sensor parts, does the servo sweep back and forth as expected?
#include "ServoTimer2.h"
ServoTimer2 servo1;
void setup()
{
Serial.begin(9600); // For Serial Monitor
servo1.attach(12);
}
void loop()
{
servo1.write(0); //min pulse width for 0 degree
delay(1000);
servo1.write(90); //pulse width for 90 degree
delay(1000);
delay(20); // considered best practice in a simple sketch.
}
It seems like the PulseSensorPlayground library is somehow conflicting with the ServoTimer2 library. It has code to avoid a conflict with the Servo library so I would try that.
Change:
#define USE_ARDUINO_INTERRUPTS true // Set-up low-level interrupts for most acurate BPM math.
#include <PulseSensorPlayground.h> // Includes the PulseSensorPlayground Library.
#include "ServoTimer2.h"
ServoTimer2 servo1;
to:
#include <Servo.h> // Must be before PulseSensorPlayground
#define USE_ARDUINO_INTERRUPTS true // Set-up low-level interrupts for most acurate BPM math.
#include <PulseSensorPlayground.h> // Includes the PulseSensorPlayground Library.
Servo servo1;
Thank you Sir.
Actually, I also want to add software serial along with the servo motor and the pulse sensor. The SoftwareSerial use the same interrupt as the servo.h library. I also tried <AltSoftSerial.h> but I get the same issue. I actually want the baud rate of 115200 with another serial to be able to use the lidar sensor. (NeoSWSerial.h does not support baud rate of 115200)
The problem is that the interrupt handler of the servo, pulse sensor and serial does not working concurrently in ways I have tested so far.
Hmm..
The pulse sensor works only if the PulseSensorPlayground interrupt is assigned true.
I can successfully compile the code and can get the readings of both the pulse sensor and lidar sensor but the servo motor jitters a lot. It does not rotate as expected
Here is the code I tried:
#define USE_ARDUINO_INTERRUPTS true // Set-up low-level interrupts for most acurate BPM math.
#include <PulseSensorPlayground.h> // Includes the PulseSensorPlayground Library.
#include <ServoTimer2.h>
ServoTimer2 servo1;
#include <SoftwareSerial.h> //header file of software serial port
SoftwareSerial Serial1(2,3);
// Variables
int dist2;
const int PulseWire = 0; // PulseSensor PURPLE WIRE connected to ANALOG PIN 0
const int LED13 = 13; // The on-board Arduino LED, close to PIN 13.
int Threshold = 550; // Determine which Signal to "count as a beat" and which to ignore.
int i;
int uart[9]; //save data measured by LiDAR
const int HEADER=0x59; //frame header of data package
int check; //save check value
PulseSensorPlayground pulseSensor; // Creates an instance of the PulseSensorPlayground object called "pulseSensor"
void setup() {
Serial.begin(9600); // For Serial Monitor
servo1.attach(12);
Serial1.begin(115200);
// Configure the PulseSensor object, by assigning our variables to it.
pulseSensor.analogInput(PulseWire);
pulseSensor.blinkOnPulse(LED13); //auto-magically blink Arduino's LED with heartbeat.
pulseSensor.setThreshold(Threshold);
// Double-check the "pulseSensor" object was created and "began" seeing a signal.
if (pulseSensor.begin()) {
Serial.println("We created a pulseSensor Object !"); //This prints one time at Arduino power-up, or on Arduino reset.
}
}
void loop() {
int myBPM = pulseSensor.getBeatsPerMinute(); // Calls function on our pulseSensor object that returns BPM as an "int".
// "myBPM" hold this BPM value now.
if (pulseSensor.sawStartOfBeat()) { // Constantly test to see if "a beat happened".
Serial.println("♥ A HeartBeat Happened ! "); // If test is "true", print a message "a heartbeat happened".
Serial.print("BPM: "); // Print phrase "BPM: "
Serial.println(myBPM); // Print the value inside of myBPM.
}
for (int i = 0; i < 90; i++)
{
lidar();
servo1.write(i);
delay(1);
}
for (int i = 90; i > 0; i--)
{
lidar();
servo1.write(i);
delay(1);
}
delay(20); // considered best practice in a simple sketch.
}
int lidar(){
if (Serial1.available()) { //check if serial port has data input
if(Serial1.read() == HEADER) { //assess data package frame header 0x59
uart[0]=HEADER;
if (Serial1.read() == HEADER) { //assess data package frame header 0x59
uart[1] = HEADER;
for (i = 2; i < 9; i++) { //save data in array
uart[i] = Serial1.read();
}
check = uart[0] + uart[1] + uart[2] + uart[3] + uart[4] + uart[5] + uart[6] + uart[7];
if (uart[8] == (check & 0xff)){ //verify the received data as per protocol
dist2 = uart[2] + uart[3] * 256; //calculate distance value
// Serial.listen();
Serial.print("lidar dist = ");
Serial.println(dist2); //output measure distance value of LiDAR
//Serial.print('\t');
delay(100);
}
}
}
}
return dist2;
}
Sir, grateful if you could please help me on this:
When I use the servo motor with pulse sensor, the latter gives reading although I am not touching it at all. Why is it so?
The code:
#include <Servo.h>
#define USE_ARDUINO_INTERRUPTS true // Set-up low-level interrupts for most acurate BPM math
#include <PulseSensorPlayground.h> // Includes the PulseSensorPlayground Library
const int PulseWire = 0; // 'S' Signal pin connected to A0
const int LED13 = 13; // The on-board Arduino LED
int Threshold = 550; // Determine which Signal to "count as a beat" and which to ignore
int distance;
PulseSensorPlayground pulseSensor; // Creates an object
Servo servo;
long duration;
void setup() {
Serial.begin(9600);
servo.attach(12);
servo.write(0);
// Configure the PulseSensor object, by assigning our variables to it
pulseSensor.analogInput(PulseWire);
pulseSensor.blinkOnPulse(LED13); // Blink on-board LED with heartbeat
pulseSensor.setThreshold(Threshold);
// Double-check the "pulseSensor" object was created and began seeing a signal
if (pulseSensor.begin()) {
Serial.println("PulseSensor object created!");
}
}
void loop() {
int myBPM = pulseSensor.getBeatsPerMinute(); // Calculates BPM
if (pulseSensor.sawStartOfBeat()) { // Constantly test to see if a beat happened
Serial.println("♥ A HeartBeat Happened ! "); // If true, print a message
Serial.print("BPM: ");
Serial.println(myBPM); // Print the BPM value
}
delay(20);
for (int i=0; i<180; i++){
servo.write(i);
//usdist();
delay(5);
}
for (int i=180; i>0; i--){
servo.write(i);
//usdist();
delay(5);
}
}