Hi friends,
I’m still a novice when it comes to interacting with physical sensors, in this case I am trying to get the small plastic flow sensor to work using the stock code found on their GitHub, with small modifications. I under stand that the flow meter works on the the principal of the flow spinning a wheel with magnets that induces a voltage every time it passes by the sensor. The code they provide i think is for an Arduino uno, now from my research i know that the interrupts are processor specific so i need to know how to modify the following code for it to work on my photon.
The stock code comes with LCD screen code that will display the sensor measurement, I’ve commented all of that out and am trying to get a reading on the serial monitor.
The two parts I still do not understand are the Signal, and the UseInterupt sections, so any explanation is greatly appreciated.
Here is the code i am using when i try to compile as is i get a few error readings.
// which pin to use for reading the sensor? can use any pin!
#define FLOWSENSORPIN D2
// count how many pulses!
volatile int pulses = 0;
// track the state of the pulse pin
volatile int lastflowpinstate;
// you can try to keep time of how long it is between pulses
volatile int 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(FLOWSENSORPIN, ISR, CHANGE)
{
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);
attachInterrupt(FLOWSENSORPIN, useInterrupt, CHANGE);
}
void loop() // run over and over again
{
//lcd.setCursor(0, 0);
//lcd.print("Pulses:"); lcd.print(pulses, DEC);
//lcd.print(" Hz:");
//lcd.print(flowrate);
//lcd.print(flowrate);
Serial.print("Freq: "); Serial.println(flowrate);
Serial.print("Pulses: "); Serial.println(pulses, DEC);
// if a plastic sensor use the following calculation
// Sensor Frequency (Hz) = 7.5 * Q (Liters/min)
// Liters = Q * time elapsed (seconds) / 60 (seconds/minute)
// Liters = (Frequency (Pulses/second) / 7.5) * time elapsed (seconds) / 60
// Liters = Pulses / (7.5 * 60)
float liters = pulses;
liters /= 7.5;
liters /= 60.0;
/*
// if a brass sensor use the following calculation
float liters = pulses;
liters /= 8.1;
liters -= 6;
liters /= 60.0;
*/
Serial.print(liters); Serial.println(" Liters");
//lcd.setCursor(0, 1);
//lcd.print(liters); lcd.print(" Liters ");
delay(100);
}