Advertisement
atheos42

ESP32_Fan_Meter

Jun 1st, 2023 (edited)
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.62 KB | Source Code | 0 0
  1. #include <TFT_eSPI.h> // Graphics and font library for ILI9341 driver chip
  2. #include <SPI.h>
  3.  
  4. //#define TFT_GREY 0x5AEB // New colour
  5.  
  6. // Pin Assignments
  7. uint8_t Fan1_Pin 2;
  8. uint8_t Fan2_Pin 15;
  9. #define FAN1_PIN 2
  10. #define FAN2_PIN 15
  11.  
  12. // unipole hall effect sensor, Div is 2
  13. // bipole hall effect sensor, Div is 8
  14. // Declare Fan struct
  15. typedef struct{  
  16.     uint32_t Ticks;
  17.     uint32_t RPM;
  18.     uint8_t Div;
  19. } Fan;
  20.  
  21. // Variables
  22. Fan Fans[2];
  23. uint8_t str_Fan1[16], str_Fan2[16];
  24. TFT_eSPI tft = TFT_eSPI();  // Invoke library
  25.  
  26. // Interrupt Service Routines
  27. void Fan1_Add_Ticks () { Fans[0].Ticks++; }
  28. void Fan2_Add_Ticks () { Fans[1].Ticks++; }
  29.  
  30. // function to clear fan calculations
  31. void clear_fans (){
  32.     Fans[0].Ticks = 0;
  33.     Fans[0].RPM = 0;
  34.     Fans[1].Ticks = 0;
  35.     Fans[1].RPM = 0;
  36. }
  37.  
  38. void setup(){
  39.     // Initalize GPIO pins
  40.     pinMode(Fan1_Pin, INPUT);
  41.     pinMode(Fan2_Pin, INPUT);
  42.  
  43.     // Initalize Serial port
  44.     Serial.begin(9600);
  45.    
  46.     // Initalize TFT
  47.     tft.init();
  48.     tft.setRotation(2);
  49.    
  50.     // Initalize interrupt service
  51.     attachInterrupt(digitalPinToInterrupt(Fan1_Pin), Fan1_Add_Ticks, RISING);
  52.     attachInterrupt(digitalPinToInterrupt(Fan2_Pin), Fan2_Add_Ticks, RISING);
  53.  
  54.     // Set Fans Div
  55.     Fans[0].Div = 2;
  56.     Fans[1].Div = 2;
  57. }
  58.  
  59. void loop (){
  60.     //Set Fans Ticks to 0, ready for new calculations
  61.     clear_fans;
  62.  
  63.     //Enables interrupts
  64.     sei();
  65.  
  66.     //Wait 1 second
  67.     delay (1000);
  68.  
  69.     //Disable interrupts
  70.     cli();
  71.  
  72.     // Calculate the RPM of the Fans, RPM = (Ticks * 60) / Div
  73.     Fans[0].RPM = ((Fans[0].Ticks * 60)/Fans[0].Div);
  74.     Fans[1].RPM = ((Fans[1].Ticks * 60)/Fans[1].Div);
  75.  
  76.     // Output the RPM of the fans to the serial port.
  77.     //str_Fan1 = "Fan1 RPM: " + Fans[0].RPM
  78.     //str_Fan2 = "Fan2 RPM: " + Fans[1].RPM
  79.     // https://cpp4arduino.com/2020/02/07/how-to-format-strings-without-the-string-class.html
  80.     snprintf_P(str_Fan1, sizeof(str_Fan1), PSTR("Fan1 RPM: %u"), Fan[0].RPM);
  81.     snprintf_P(str_Fan2, sizeof(str_Fan2), PSTR("Fan2 RPM: %u"), Fan[1].RPM);
  82.    
  83.     Serial.println (str_Fan1);
  84.     Serial.println (str_Fan2);
  85.  
  86.     //Serial.print ("Fan1 RPM: ");
  87.     //Serial.println (Fans[0].RPM, DEC);
  88.     //Serial.print ("Fan2 RPM: ");
  89.     //Serial.println (Fans[1].RPM, DEC);
  90.  
  91.     // Set "cursor" at top left corner of display (0,0) and select font 2
  92.     // (cursor will move to next line automatically during printing with 'tft.println'
  93.     //  or stay on the line is there is room for the text with tft.print)
  94.     tft.setCursor(0, 0, 2);
  95.  
  96.     // Set the font colour to be green with black background, set to font 4
  97.     tft.setTextColor(TFT_GREEN,TFT_BLACK);
  98.     //tft.setTextFont(2);
  99.     tft.println("Fan Meter");
  100.     tft.println(str_Fan1);
  101.     tft.println(str_Fan2);
  102.    
  103. }
Tags: ESP32
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement