Advertisement
Guest User

sensor fluxo

a guest
Sep 27th, 2016
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.68 KB | None | 0 0
  1. //code by Crenn from http://thebestcasescenario.com
  2. //project by Charles Gantt from http://themakersworkbench.com
  3. //To disable interrupts: cli(); disable global interrupts and
  4. //to enable them: sei(); enable interrupts
  5.  
  6. //Varibles used for calculations
  7. int NbTopsFan; int Calc;
  8.  
  9. //The pin location of the sensor
  10. int hallsensor = 2; typedef struct{
  11.  
  12. //Defines the structure for multiple fans and
  13. //their dividers
  14. char fantype;
  15. unsigned int fandiv; }fanspec;
  16.  
  17. //Definitions of the fans
  18. //This is the varible used to select the fan and it's divider,
  19. //set 1 for unipole hall effect sensor
  20. //and 2 for bipole hall effect sensor
  21. fanspec fanspace[3]={{0,1},{1,2},{2,8}}; char fan = 1;
  22.  
  23. void rpm ()
  24. //This is the function that the interupt calls
  25. { NbTopsFan++; }
  26.  
  27. //This is the setup function where the serial port is initialised,
  28. //and the interrupt is attached
  29. void setup()
  30. { pinMode(hallsensor, INPUT);
  31. Serial.begin(9600);
  32. attachInterrupt(0, rpm, RISING); }
  33.  
  34. void loop ()
  35. //Set NbTops to 0 ready for calculations
  36. { NbTopsFan = 0;
  37.  
  38. //Enables interrupts
  39. sei();
  40.  
  41. //Wait 1 second
  42. delay (1000);
  43.  
  44. //Disable interrupts
  45. cli();
  46.  
  47. //Times NbTopsFan (which is apprioxiamately the fequency the fan
  48. //is spinning at) by 60 seconds before dividing by the fan's divider
  49. Calc = ((NbTopsFan * 60)/fanspace[fan].fandiv);
  50.  
  51. //Prints the number calculated above
  52. Serial.print (Calc, DEC);
  53.  
  54. //Prints " rpm" and a new line
  55. Serial.print (" rpm\r\n");
  56. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement