Function for counting wheels RPM is not working

Hello to everyone. I am creating a four wheel vehicle and I want to count how many circles every wheel does. I did it for one wheel but now I am trying to create a function to do the same thing to all wheels. The problem is that it seems like doesn't count anything even in one wheel... It seems like it only keeps the last value without change anything. Can anyone understand why?

int sensor_FR = 8;
int sensor_FL = 9;
int sensor_BR = 10;
int sensor_BL = 11;

bool hallFR;

int cur_statusFR;
int prev_statusFR;
int counterFR = 0;
double distance_completeFR = 0;
double rounds_completeFR = 0;


void setup() {

	pinMode(sensor_FR, INPUT);
	pinMode(sensor_FL, INPUT);
	pinMode(sensor_BR, INPUT);
	pinMode(sensor_BL, INPUT);

	Serial.begin(9600);
}


void counter_wheels(int sensor, bool hall, int cur_status, int prev_status, int counter, double distance_complete, double rounds_complete)
{
	
	hall = digitalRead(sensor);

	if (!hall)
	{
		cur_status = 0;
		if (prev_status != cur_status)
		{
			counter++;
			prev_status = cur_status;
		}
		else
		{
			counter = counter;
			prev_status = prev_status;
		}
	}
	else
	{
		cur_status = 1;
		if (prev_status != cur_status)
		{
			counter ++;
			prev_status = cur_status;
		}
		else
		{
			counter = counter;
			prev_status = prev_status;
		}
	}

	
	distance_complete = (counter * 1.1) / 2;
	rounds_complete = counter * 0.025;
	
	
	Serial.println(counter);
	Serial.println(distance_complete);
	Serial.println(rounds_complete);

	
}
void loop() {

	Serial.println("-----Start-----");
	counter_wheels(sensor_FR, hallFR, cur_statusFR, prev_statusFR, counterFR, distance_completeFR, rounds_completeFR);
	Serial.println("------End------");

	delay(10);
}

You promised the compiler that your function would return a value.

Oh, that's from my last experiment when I tried only to return the counter with no other information.

But it also does the same as a void function.

What happens during the time that you’re deliberately not looking at the sensor?

I don’t understand your question…

SGbron:
I don’t understand your question…

Clue:delay(10);

If the problem was in the delay then it wouldn't count properly.... Not counting nothing... The problem seems to be in the memory of the variables i have create.

As I wrote at the first post, the same code works fine if it is in the void loop() and works as a script code for only one wheel. So somewhere in my variables and their memory I believe it is the problem.

Google “C variable scope”

For the moment now, due to your delay,etc... the code will never run as expected.
Use interrupts to check for movements.

You could try to fix the scope issue by using references.

Ok, I will see it. Thank you very much both of you.

Anyway, can you explain me how it works properly when it’s inside the loop and stops when its inside another function?

See reply #7

the task that you are trying to do the way that you are doing it is not going to work.
not only does it hurt our eyes to look at this big list of parameters in your function, but you also need the function to know which variables to reassign.

this is a basic problem of applying a function to different sets of variables.

this can be solved through either classes or arrays.
and will make your script much more organized.

here is an example of a custom class:

// declair the class like this:
class wheel {
  public:
  int counter;
  int pin;
  int cur;
  int pre;
  double rounds;
  double dist;
  bool hall; 
  
  void count (){
   cur = digitalRead(pin);
   if(cur!=pre){
      if(cur){
        counter++;
        dist = (counter * 1.1) / 2;
        rounds = counter * 0.025;
        }
     pre=cur; }
 
  Serial.println(counter);
  Serial.println(dist);
  Serial.println(rounds);
   }
};
  
// make 4 of the above classes 
wheel FR;
wheel FL;
wheel BR;
wheel BL;

void setup() {
  Serial.begin(9600);
 //change variables inside a class like this:
 FR.pin = 8; 
 FL.pin = 9;
 BR.pin = 10; 
 BL.pin = 11;
// get a variable from a class like this:
pinMode(FR.pin, INPUT);
pinMode(FL.pin, INPUT);
pinMode(BR.pin, INPUT);
pinMode(BL.pin, INPUT);
}

void loop() {
// call the function inside each class like this:
 FR.count();
 FL.count();
 BR.count();
 BL.count();
 delay(10);

}

the purpose in making a class is to make groups of variables or functions. then you can make as many copies of that group as you wish. in this example i put your function inside the class so that it only effects the variables inside. you could also think of a class as declaring a mini sketch inside your sketch... then making one for each wheel. Anyways google into classes... they are cool

Thank you very much, all of you for your replies.
Your answers had been very helpful.

Marry Christmas!!!