As you may soon discover, this is my first programming project. I am trying to modify an existing sketch which reads fidget spinner rpms and read 2 fidget spinners at once.
Here is a link to the sketch I started from:
https://create.arduino.cc/projecthub/andriy-baranov/fidget-spinner-rpm-counter-253ac0?ref=userrespected&ref_id=401439&offset=0
I have managed to get the display to show both lines but both show the rpm from pin 2 and not pin 4. I am wondering if I need to identify the rpm input from pin 4 differently than pin 2.
Any other critique/suggestions of my sketch would be welcomed.
#include <LiquidCrystal.h>
LiquidCrystal lcd(13,12,11,10,9,8);
const int hallSensorPin1 = 2; // connect the hall effect sensor on pin 2
const int hallSensorPin2 = 4;
const unsigned long sampleTime = 1000;
const int maxRPM = 960; // maximum RPM for LCD Bar
int rpmMaximum = 0;
void setup()
{
pinMode(hallSensorPin1,INPUT);
pinMode(hallSensorPin2,INPUT);
Serial.begin(9600);
lcd.begin(16, 2);
lcd.print("startup");
delay(2000);
lcd.clear();
}
void loop()
{
delay(100);
int rpm = getRPM();
if (rpm > rpmMaximum) rpmMaximum = rpm;
lcd.clear();
displayRPM(rpm);
//displayBar(rpm);
}
int getRPM()
{
int count = 0;
boolean countFlag = LOW;
unsigned long currentTime = 0;
unsigned long startTime = millis();
while (currentTime <= sampleTime)
{
if (digitalRead(hallSensorPin1) == HIGH)
{
countFlag = HIGH;
}
if (digitalRead(hallSensorPin1) == LOW && countFlag == HIGH)
{
count++;
countFlag=LOW;
}
currentTime = millis() - startTime;
}
// int countRpm = int(60000/float(sampleTime))*count;
// return countRpm;
// int count = 0;
// boolean countFlag = LOW;
// unsigned long currentTime = 0;
// unsigned long startTime = millis();
while (currentTime <= sampleTime)
{
if (digitalRead(hallSensorPin2) == HIGH)
{
countFlag = HIGH;
}
if (digitalRead(hallSensorPin2) == LOW && countFlag == HIGH)
{
count++;
countFlag=LOW;
}
currentTime = millis() - startTime;
}
int countRpm = int(60000/float(sampleTime))*count;
return countRpm;
}
void displayRPM(int rpm)
{
lcd.clear();
lcd.setCursor(0,0);
lcd.print(rpm,DEC);
lcd.setCursor(0,1);
lcd.print(rpm,DEC);
lcd.setCursor(9,0);
lcd.print(rpmMaximum, DEC);
lcd.setCursor(9,1);
lcd.print(rpmMaximum, DEC);
lcd.setCursor(13,0);
lcd.print("MAX");
lcd.setCursor(13,1);
lcd.print("MAX");
Serial.print("RPM = ");
Serial.print(rpm);
Serial.print(" MAX RPM = ");
Serial.println(rpmMaximum);
}