Reading pc fan RPM

Hy i`m using this code to read fan RPM ,and i need sone help, i need to read multiple fans and dont now how to modify this code ,can someone help me pls, just a short kick will be nice. THX.

the code:

/*To disable interrupts:
 cli();                // disable global interrupts

and to enable them:  
 sei();                // enable interrupts
*/


                                   //Varibles used for calculations
int NbTopsFan; 
int Calc;

                                  //The pin location of the sensor
int hallsensor = 2;

                        
typedef struct{                  //Defines the structure for multiple fans and their dividers
  char fantype;
  unsigned int fandiv;
}fanspec;

//Definitions of the fans
fanspec fanspace[3]={{0,1},{1,2},{2,8}};

char fan = 1;   //This is the varible used to select the fan and it's divider, set 1 for unipole hall effect sensor 
               //and 2 for bipole hall effect sensor 


void rpm ()      //This is the function that the interupt calls 
{ 
 NbTopsFan++; 
} 

              //This is the setup function where the serial port is initialised,
             //and the interrupt is attached
void setup() 
{ 
 pinMode(hallsensor, INPUT); 
 Serial.begin(115200); 
 attachInterrupt(0, rpm, RISING); 
} 
void loop () 
{
   NbTopsFan = 0;  //Set NbTops to 0 ready for calculations
   sei();   //Enables interrupts
   delay (1000);  //Wait 1 second
   cli();   //Disable interrupts
   Calc = ((NbTopsFan * 60)/fanspace[fan].fandiv); //Times NbTopsFan (which is apprioxiamately the fequency the fan is spinning at) by 60 seconds before dividing by the fan's divider
   Serial.print (Calc, DEC); //Prints the number calculated above
   Serial.print (" rpm\r\n"); //Prints " rpm" and a new line
}

Which Arduino board do you have ? Most only have 2 hardware interrupt pins but there are other ways to do what you are asking.

Are you familiar with using arrays ?

I have now an arduino UNO.A MEGA and a DUE on the way.
I need to tell you theat i`m new to arduino world,just begining to learn.
Thx.

Since you seem to understand how to create datatypes and know about arrays, you may want to think of creating a fan Class...

... this is not my code,so i just tell you theat i`m new to arduino ,i just need to adapt this code to my needs.
I need to read 10 fan RPMs.
THX.

BulldogLowell:
Since you seem to understand how to create datatypes and know about arrays, you may want to think of creating a fan Class...

Ionut81:
... this is not my code,so i just tell you theat i`m new to arduino ,i just need to adapt this code to my needs.
I need to read 10 fan RPMs.
THX.

Oh... never mind!

I need to read 10 fan RPMs.

Your code appears oriented towards reading 10 different types of fans with different dividers, but is reading only one fan at a time through an interrupt on pin 2.

Is more than one fan going to be running at the same time and you want to read the rpm of each one? Do you have some sort of selector switch to connect each fan to pin 2?

If you don't have a selector switch, you can not bring multiple (10) input signals through one interrupt pin and know which pulse is produced by which fan.

As UK HeliBob suggests, you will need different input channels. You can do this with a Uno using the two external interrupts and Pin Change interrupts for the rest. The Mega only has 6 external interrupts and you will still have to use some pin change interrupts. Pin Change interrupts require more coding as you have to set them up with register commands, and then determine which pin, of the multiple pins on a register port, has changed. There are pin change interrupt libraries, but I have not used them.

The Due can have an external interrupts on all digital pins so it may be your better choice if you are not prepared to work pin change interrupts

Your architecture of enabling interrupts, waiting with delay(1000) to collect counts, and then disabling interrupts is not good. It is blocking for other things in your program, and interrupts are required for millis() and Serial.print() to work properly.

You should be looking at the "Blink without Delay" example as the template for how to do something (like read interrupt counts) every second.

yes the first code is for 1 fan ,i need to read 10 fan RPMs at onece,

10 fan RPM = 10 digital I/O,so 1 fan on 1 digital I/O

can someone explain to me what this function is doing pls

//Definitions of the fans
fanspec fanspace[3]={{0,1},{1,2},{2,8}};

Ionut81:
can someone explain to me what this function is doing pls

//Definitions of the fans

fanspec fanspace[3]={{0,1},{1,2},{2,8}};

you are creating an array of fanspec objects of size three called "fanspace"

see here:

typedef struct{                  //Defines the structure for multiple fans and their dividers
  char fantype;
  unsigned int fandiv;
}fanspec;

each of the array's first member is a fantype and its second member is a fandiv.

TY.