#include "LiquidCrystal.h"
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
// which pin to use for reading the sensor? can use any pin!
#define FLOWSENSORPIN 7
// miinus nupu pin
#define MIINUSPIN 6
// count how many pulses!
volatile uint16_t pulses = 0;
// track the state of the pulse pin
volatile uint8_t lastflowpinstate;
// you can try to keep time of how long it is between pulses
volatile uint32_t lastflowratetimer = 0;
// and use that to calculate a flow rate
volatile float flowrate;
// Interrupt is called once a millisecond, looks for any pulses from the sensor!
SIGNAL(TIMER0_COMPA_vect) {
uint8_t x = digitalRead(FLOWSENSORPIN);
if (x == lastflowpinstate) {
lastflowratetimer++;
return; // nothing changed!
}
if (x == HIGH) {
//low to high transition!
pulses++;
}
lastflowpinstate = x;
flowrate = 1000.0;
flowrate /= lastflowratetimer; // in hertz
lastflowratetimer = 0;
}
void useInterrupt(boolean v) {
if (v) {
// Timer0 is already used for millis() - we'll just interrupt somewhere
// in the middle and call the "Compare A" function above
OCR0A = 0xAF;
TIMSK0 |= _BV(OCIE0A);
} else {
// do not call the interrupt function COMPA anymore
TIMSK0 &= ~_BV(OCIE0A);
}
}
void setup() {
Serial.begin(9600);
Serial.print("Flow sensor test!");
lcd.begin(16, 2);
pinMode(FLOWSENSORPIN, INPUT);
digitalWrite(FLOWSENSORPIN, HIGH);
lastflowpinstate = digitalRead(FLOWSENSORPIN);
useInterrupt(true);
}
void loop() // run over and over again
{
lcd.setCursor(0, 0);
lcd.print(pulses); lcd.print(" Liitrit");
Serial.print(pulses); Serial.println(" Liitrit ");
delay(500);
float liters = pulses;
liters *= 0.00276;
Serial.print(liters,1); Serial.println(" EUR-i");
lcd.setCursor(0, 1);
lcd.print(liters);
lcd.print(" EUR-i");
delay(100);
}
Kohendasin natuke oma vajaduste järgi. Tegemist vee arvestusega. Veemõõtja andur läheb gnd ja teine ots pin 7. Pole veel mõõtjaga proovinud aga niisama juhtmega ei saa täpselt. Kuskile on vaja tekitada viivitus ehk siis välimaa keeli " debounce delay" . Lihtsalt delay(100); Kuhu see panna? Sketsis on ka ülearuseid asju vast mida võib vähemaks võtta.
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
// which pin to use for reading the sensor? can use any pin!
#define FLOWSENSORPIN 7
// miinus nupu pin
#define MIINUSPIN 6
// count how many pulses!
volatile uint16_t pulses = 0;
// track the state of the pulse pin
volatile uint8_t lastflowpinstate;
// you can try to keep time of how long it is between pulses
volatile uint32_t lastflowratetimer = 0;
// and use that to calculate a flow rate
volatile float flowrate;
// Interrupt is called once a millisecond, looks for any pulses from the sensor!
SIGNAL(TIMER0_COMPA_vect) {
uint8_t x = digitalRead(FLOWSENSORPIN);
if (x == lastflowpinstate) {
lastflowratetimer++;
return; // nothing changed!
}
if (x == HIGH) {
//low to high transition!
pulses++;
}
lastflowpinstate = x;
flowrate = 1000.0;
flowrate /= lastflowratetimer; // in hertz
lastflowratetimer = 0;
}
void useInterrupt(boolean v) {
if (v) {
// Timer0 is already used for millis() - we'll just interrupt somewhere
// in the middle and call the "Compare A" function above
OCR0A = 0xAF;
TIMSK0 |= _BV(OCIE0A);
} else {
// do not call the interrupt function COMPA anymore
TIMSK0 &= ~_BV(OCIE0A);
}
}
void setup() {
Serial.begin(9600);
Serial.print("Flow sensor test!");
lcd.begin(16, 2);
pinMode(FLOWSENSORPIN, INPUT);
digitalWrite(FLOWSENSORPIN, HIGH);
lastflowpinstate = digitalRead(FLOWSENSORPIN);
useInterrupt(true);
}
void loop() // run over and over again
{
lcd.setCursor(0, 0);
lcd.print(pulses); lcd.print(" Liitrit");
Serial.print(pulses); Serial.println(" Liitrit ");
delay(500);
float liters = pulses;
liters *= 0.00276;
Serial.print(liters,1); Serial.println(" EUR-i");
lcd.setCursor(0, 1);
lcd.print(liters);
lcd.print(" EUR-i");
delay(100);
}
Kohendasin natuke oma vajaduste järgi. Tegemist vee arvestusega. Veemõõtja andur läheb gnd ja teine ots pin 7. Pole veel mõõtjaga proovinud aga niisama juhtmega ei saa täpselt. Kuskile on vaja tekitada viivitus ehk siis välimaa keeli " debounce delay" . Lihtsalt delay(100); Kuhu see panna? Sketsis on ka ülearuseid asju vast mida võib vähemaks võtta.
Comment