I’m trying to use a hall effect sensor to notice the presence of a magnet, calculate how much time has passed since the last presence was detected, and use this calculation as a variable in a larger equation.
Ideally: presence + time passed + constant c = total
The end goal is to also have an ultrasonic sensor calculate a position value, and then combine the results from the hall effect sensor, time passing, and ultrasonic sensor into an equation that determines a ‘point total’. For now, though, I’m just trying to combine the reading from the hall effect sensor with a very basic equation to have the presence of the magnet trigger the ‘point total’. The problem is, I don’t know what I’m doing. The symptom is, the code is such a mess that it won’t even upload.
I tried to modify an existing code for measuring RPM from a hall effect sensor, to get the time values from it (tested original code and it works fine, so I know that the problem is definitely my code and not my circuit). I was trying to stay away from void functions, because I want my results to be returned and able to be added together. But I think I’ve made things a huge mess with how many int functions I’ve got going on, here.
Other versions of this code have gotten error messages such as ‘countRpm/getRpm/countTotal not declared within scope’, or have given me an Rpm reading only.
Many thanks in advance, for anyone who takes a look at this.
#include <LiquidCrystal.h>
LiquidCrystal lcd(13,12,11,10,9,8);
const int hallSensorPin = 2;
const unsigned long sampleTime = 1000;
const int maxRPM = 10;
int rpmMaximum = 0;
const int maxTotal = 120;
const int minTotal = 0;
int rpm;
unsigned long currentTime = 0;
unsigned long startTime = millis();
void setup()
{
pinMode(hallSensorPin,INPUT);
Serial.begin(9600);
lcd.begin(16, 2);
lcd.print("Initializing");
delay(1000);
lcd.clear();
}
int getTotal()
{
int count = 0;
boolean countFlag = LOW;
unsigned long currentTime = 0;
unsigned long startTime = millis();
while (currentTime <= sampleTime)
{
if (digitalRead(hallSensorPin) == HIGH)
{
countFlag = HIGH;
}
if (digitalRead(hallSensorPin) == LOW && countFlag == HIGH)
{
count++;
countFlag=LOW;
}
currentTime = millis() - startTime;
}
int countRpm = int(1000/float(sampleTime))*count;
{
if (countRpm>0)
{
int total = (countRpm+currentTime+3);
}
if (countRpm<=0)
{
int total = (countRpm+0.5);
return countRpm;
return total;
}
}
}
void loop()
{
int total = getTotal();
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Item Value:");
lcd.setCursor(7,1);
lcd.print(total, DEC);
Serial.print(total);
}