Coding for Hall Effect Sensor, Added Variables, and LCD Display

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);
  
}

when you have code like this

    if (countRpm > 0)
    {
      int total = (countRpm + currentTime + 3);
    }
    if (countRpm <= 0)
    {
      int total = (countRpm + 0.5);
      return countRpm;
      return total;
    }

you are actually declaring two different variables, each called ‘total’ since you are declaring them within the if() statement. After the if() statement has executed, your variables go out of scope( e.g. disappear) and have no value. The compiler warns your about that if you check the output.

You need to declare the variable at the top of the function and then simply do the assignment inside the if() statement so the value is around at the end of the function to be returned.

The built in function PulseIn() can do this for you. Easy.

John.

Thanks so much for the help!! Changing the if-statements and placement of definitions worked (I didn't realize I was declaring different variables- thanks blh64). I will also try the PulseIn() function to see how I can clean some things up. Thanks again!