Advertisement
Guest User

Untitled

a guest
Feb 15th, 2016
288
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.68 KB | None | 0 0
  1. #include <Wire.h>  
  2. #include <LiquidCrystal_I2C.h>
  3.  
  4. LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);
  5.  
  6.  
  7. // which pin to use for reading the sensor? can use any pin!
  8. #define FLOWSENSORPIN 2
  9.  
  10. // count how many pulses!
  11. volatile uint16_t pulses = 0;
  12. // track the state of the pulse pin
  13. volatile uint8_t lastflowpinstate;
  14. // you can try to keep time of how long it is between pulses
  15. volatile uint32_t lastflowratetimer = 0;
  16. // and use that to calculate a flow rate
  17. volatile float flowrate;
  18. // Interrupt is called once a millisecond, looks for any pulses from the sensor!
  19. SIGNAL(TIMER0_COMPA_vect) {
  20.  uint8_t x = digitalRead(FLOWSENSORPIN);
  21.  
  22.  if (x == lastflowpinstate) {
  23.    lastflowratetimer++;
  24.    return; // nothing changed!
  25.  }
  26.  
  27.  if (x == HIGH) {
  28.    //low to high transition!
  29.    pulses++;
  30.  }
  31.  lastflowpinstate = x;
  32.  flowrate = 1000.0;
  33.  flowrate /= lastflowratetimer; // in hertz
  34.  lastflowratetimer = 0;
  35. }
  36.  
  37. void useInterrupt(boolean v) {
  38.  if (v) {
  39.    // Timer0 is already used for millis() - we'll just interrupt somewhere
  40.    // in the middle and call the "Compare A" function above
  41.    OCR0A = 0xAF;
  42.    TIMSK0 |= _BV(OCIE0A);
  43.  } else {
  44.    // do not call the interrupt function COMPA anymore
  45.    TIMSK0 &= ~_BV(OCIE0A);
  46.  }
  47. }
  48.  
  49. void setup() {
  50.   Serial.begin(9600);
  51.   Serial.print("Flow sensor test!");
  52.   lcd.begin(20, 4);
  53.  
  54.   pinMode(FLOWSENSORPIN, INPUT);
  55.   digitalWrite(FLOWSENSORPIN, HIGH);
  56.   lastflowpinstate = digitalRead(FLOWSENSORPIN);
  57.   useInterrupt(true);
  58. }
  59.  
  60. void loop() // run over and over again
  61. {
  62.  lcd.setCursor(0, 0);
  63.  lcd.print("Pulses:"); lcd.print(pulses, DEC);
  64.  lcd.print(" Hz:");
  65.  lcd.print(flowrate);
  66.  //lcd.print(flowrate);
  67.  Serial.print("Freq: "); Serial.println(flowrate);
  68.  Serial.print("Pulses: "); Serial.println(pulses, DEC);
  69.  
  70.  
  71.  
  72.  // if a plastic sensor use the following calculation
  73.  // Sensor Frequency (Hz) = 7.5 * Q (Liters/min)
  74.  // Liters = Q * time elapsed (seconds) / 60 (seconds/minute)
  75.  // Liters = (Frequency (Pulses/second) / 7.5) * time elapsed (seconds) / 60
  76.  // Liters = Pulses / (7.5 * 60)
  77.  float liters = pulses;
  78.  liters /= 7.5;
  79.  liters /= 60.0;
  80.  
  81. // _________________________     AB HIER:    _________________________________
  82.  
  83.  float z;
  84.  
  85.  z = flowrate * 60 / 400; // hier kommt 5-6 liter/min raus, müssten aber ca. 2,67 l/min sein
  86.  
  87.  lcd.setCursor(0,2);
  88.  lcd.print(z,2);
  89.  delay(300); //damit das LCD Zeit hat, die Werte auch anzuzeigen
  90.  
  91.  
  92. /*
  93. // if a brass sensor use the following calculation
  94. float liters = pulses;
  95. liters /= 8.1;
  96. liters -= 6;
  97. liters /= 60.0;
  98. */
  99.  Serial.print(liters); Serial.println(" Liters");
  100.  lcd.setCursor(0, 1);
  101.  lcd.print(liters); lcd.print(" Liters ");
  102.  
  103.  delay(100);
  104. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement