Good day everyone, I have some problem regarding to my practice of fan speed control using pulse sensor in Arduino Uno. I can gather the pulse rate and rpm of the fan. However, the fan won't change its speed depending on the pulse rate. Here is my circuit on using this project
And here is my code right now in arduino IDE:
#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
int Threshold = 550; // Determine which Signal to "count as a beat" and which to ignore
int pulse;
const int fan_control_pin = 3;
int count = 0;
unsigned long start_time;
int rpm;
PulseSensorPlayground pulseSensor; // Creates an object
void setup() {
Serial.begin(9600);
pulseSensor.analogInput(PulseWire);
pulseSensor.setThreshold(Threshold);
// Double-check the "pulseSensor" object was created and began seeing a signal
if (pulseSensor.begin()) {
Serial.println("PulseSensor object created!");
}
pinMode(fan_control_pin, OUTPUT);
analogWrite(fan_control_pin, 0);
Serial.begin(9600);
attachInterrupt(digitalPinToInterrupt(2), counter, RISING);
}
void loop() {
int pulse = (pulseSensor.getBeatsPerMinute()/2.5);
if (pulse < 40) analogWrite(fan_control_pin, 0);
if ((pulse >= 40) && (pulse < 70)) analogWrite(fan_control_pin, 102);
if ((pulse >= 70) && (pulse < 85)) analogWrite(fan_control_pin, 153);
if ((pulse >= 85) && (pulse < 100)) analogWrite(fan_control_pin, 204);
if (pulse >= 100) analogWrite(fan_control_pin, 255);
start_time = millis();
count = 0;
while((millis() - start_time) < 1000);
rpm = count * 60/2;
if (pulseSensor.sawStartOfBeat()) {
Serial.print("The pulse is ");
Serial.print(pulse);
Serial.println(" bpm");
Serial.print("The fan speed is ");
Serial.print(rpm);
Serial.println(" rpm");
}
delay(1000);
}
void counter() {
count++;
}
The code tells me the rpm and my bpm but the fan speed is not allign with my pulse rate. I want to have a fan speed depending on the pulse rate. high pulse rate, fast fan speed, and low pulse rate, low fan speed. below minimum pulse rate, off fan.