Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <TFT_eSPI.h> // Graphics and font library for ILI9341 driver chip
- #include <SPI.h>
- //#define TFT_GREY 0x5AEB // New colour
- // Pin Assignments
- uint8_t Fan1_Pin 2;
- uint8_t Fan2_Pin 15;
- #define FAN1_PIN 2
- #define FAN2_PIN 15
- // unipole hall effect sensor, Div is 2
- // bipole hall effect sensor, Div is 8
- // Declare Fan struct
- typedef struct{
- uint32_t Ticks;
- uint32_t RPM;
- uint8_t Div;
- } Fan;
- // Variables
- Fan Fans[2];
- uint8_t str_Fan1[16], str_Fan2[16];
- TFT_eSPI tft = TFT_eSPI(); // Invoke library
- // Interrupt Service Routines
- void Fan1_Add_Ticks () { Fans[0].Ticks++; }
- void Fan2_Add_Ticks () { Fans[1].Ticks++; }
- // function to clear fan calculations
- void clear_fans (){
- Fans[0].Ticks = 0;
- Fans[0].RPM = 0;
- Fans[1].Ticks = 0;
- Fans[1].RPM = 0;
- }
- void setup(){
- // Initalize GPIO pins
- pinMode(Fan1_Pin, INPUT);
- pinMode(Fan2_Pin, INPUT);
- // Initalize Serial port
- Serial.begin(9600);
- // Initalize TFT
- tft.init();
- tft.setRotation(2);
- // Initalize interrupt service
- attachInterrupt(digitalPinToInterrupt(Fan1_Pin), Fan1_Add_Ticks, RISING);
- attachInterrupt(digitalPinToInterrupt(Fan2_Pin), Fan2_Add_Ticks, RISING);
- // Set Fans Div
- Fans[0].Div = 2;
- Fans[1].Div = 2;
- }
- void loop (){
- //Set Fans Ticks to 0, ready for new calculations
- clear_fans;
- //Enables interrupts
- sei();
- //Wait 1 second
- delay (1000);
- //Disable interrupts
- cli();
- // Calculate the RPM of the Fans, RPM = (Ticks * 60) / Div
- Fans[0].RPM = ((Fans[0].Ticks * 60)/Fans[0].Div);
- Fans[1].RPM = ((Fans[1].Ticks * 60)/Fans[1].Div);
- // Output the RPM of the fans to the serial port.
- //str_Fan1 = "Fan1 RPM: " + Fans[0].RPM
- //str_Fan2 = "Fan2 RPM: " + Fans[1].RPM
- // https://cpp4arduino.com/2020/02/07/how-to-format-strings-without-the-string-class.html
- snprintf_P(str_Fan1, sizeof(str_Fan1), PSTR("Fan1 RPM: %u"), Fan[0].RPM);
- snprintf_P(str_Fan2, sizeof(str_Fan2), PSTR("Fan2 RPM: %u"), Fan[1].RPM);
- Serial.println (str_Fan1);
- Serial.println (str_Fan2);
- //Serial.print ("Fan1 RPM: ");
- //Serial.println (Fans[0].RPM, DEC);
- //Serial.print ("Fan2 RPM: ");
- //Serial.println (Fans[1].RPM, DEC);
- // Set "cursor" at top left corner of display (0,0) and select font 2
- // (cursor will move to next line automatically during printing with 'tft.println'
- // or stay on the line is there is room for the text with tft.print)
- tft.setCursor(0, 0, 2);
- // Set the font colour to be green with black background, set to font 4
- tft.setTextColor(TFT_GREEN,TFT_BLACK);
- //tft.setTextFont(2);
- tft.println("Fan Meter");
- tft.println(str_Fan1);
- tft.println(str_Fan2);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement