Advertisement
Guest User

Untitled

a guest
Jan 17th, 2020
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.90 KB | None | 0 0
  1. #include <LiquidCrystal_I2C.h>
  2. #include <Wire.h>
  3. #include <DS3231.h>
  4. #include "PinChangeInterrupt.h"
  5. #define BACKLIGHT_PIN 3
  6. int LCD1Address = 0x26;
  7. int LCD2Address = 0x27;
  8. int RTCAddress = 0x57;
  9. LiquidCrystal_I2C  lcd1(LCD1Address, 2, 1, 0, 4, 5, 6, 7);
  10. LiquidCrystal_I2C  lcd2(LCD2Address, 2, 1, 0, 4, 5, 6, 7);
  11. DS3231  rtc(SDA, SCL);
  12.  
  13. volatile static float vss_pulse_distance = 0.0003816033;
  14. volatile static float unleadedFlow = 1743e-11;
  15. volatile unsigned long vss_pulses;
  16. volatile float speed;
  17. int vss_pin = 2;
  18.  
  19.  
  20. void setup()
  21. {
  22.   Wire.begin();
  23.   rtc.begin();
  24.   lcd_begin();
  25.   pinMode(vss_pin, INPUT);
  26.   noInterrupts();
  27.   // set and initialize the TIMER1
  28.   TCCR1A = 0; // set entire TCCR1A register to 0
  29.   TCCR1B = 0; // set entire TCCR1B register to 0
  30.   TCCR1B |= (1 << CS12);
  31.   TIMSK1 |= (1 << TOIE1);
  32.   TCNT1 = 3036;
  33.   interrupts();
  34.   delay(10);
  35.  
  36.   attachInterrupt(digitalPinToInterrupt(vss_pin), distance, RISING);
  37.  
  38. }
  39. void loop()
  40. {
  41.   lcd1.home();
  42.   lcd1.print("RTC - TESTBENCH1");
  43.   lcd1.setCursor ( 0, 1 );
  44.   lcd1.print(rtc.getTimeStr());
  45.  
  46.   lcd2.home();
  47.   lcd2.print("SPEED - TB2");
  48.   lcd2.setCursor ( 0, 1 );
  49.   lcd2.print(speed);
  50.  
  51.   delay(1000);
  52.  
  53.   Serial.print(speed);
  54.   Serial.print('\n');
  55.  
  56. }
  57.  
  58. void lcd_begin()
  59. {
  60.   lcd1.begin(16, 2);
  61.   lcd1.setBacklightPin(BACKLIGHT_PIN, POSITIVE);
  62.   lcd1.setBacklight(HIGH);
  63.   lcd1.home ();
  64.  
  65.   lcd2.begin(16, 2);
  66.   lcd2.setBacklightPin(BACKLIGHT_PIN, POSITIVE);
  67.   lcd2.setBacklight(HIGH);
  68.   lcd2.home ();
  69. }
  70.  
  71. void instantSpeed()
  72. {
  73.   speed = (vss_pulse_distance * vss_pulses * 3600);
  74. }
  75.  
  76. ISR(TIMER1_OVF_vect) //TIMER1 overflow interrupt -- occurs every 1sec --
  77. {
  78.   instantSpeed();
  79.   vss_pulses = 0;
  80.   TCNT1 = 3036;
  81. }
  82.  
  83. void distance()
  84. {
  85.   vss_pulses++;                                           // we calculate 3 times the same thing in order to reset the distance, the average cons. and average speed independently
  86. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement