Created
January 20, 2013 23:23
-
-
Save tagroup/4582539 to your computer and use it in GitHub Desktop.
Arduino Sketch for data logging water flow to an SD card. Project at http://thomasloughlin.com/rain-water-collection/.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <SD.h> | |
/* | |
This script is a simple combination of two existing example files. | |
The goal is to measure water flow coming from a rain collection barrel | |
The project progress can be found at http://thomasloughlin.com/rain-water-collection/. | |
*/ | |
/********************************************************** | |
This is example code for using the Adafruit liquid flow meters. | |
Tested and works great with the Adafruit plastic and brass meters | |
------> http://www.adafruit.com/products/828 | |
------> http://www.adafruit.com/products/833 | |
Connect the red wire to +5V, | |
the black wire to common ground | |
and the yellow sensor wire to pin #2 | |
Adafruit invests time and resources providing this open source code, | |
please support Adafruit and open-source hardware by purchasing | |
products from Adafruit! | |
Written by Limor Fried/Ladyada for Adafruit Industries. | |
BSD license, check license.txt for more information | |
All text above must be included in any redistribution | |
**********************************************************/ | |
/* | |
SD card datalogger | |
This example shows how to log data from three analog sensors | |
to an SD card using the SD library. | |
The circuit: | |
* analog sensors on analog ins 0, 1, and 2 | |
* SD card attached to SPI bus as follows: | |
** MOSI - pin 11 | |
** MISO - pin 12 | |
** CLK - pin 13 | |
** CS - pin 4 | |
created 24 Nov 2010 | |
modified 9 Apr 2012 | |
by Tom Igoe | |
This example code is in the public domain. | |
*/ | |
// which pin to use for reading the sensor? can use any pin! | |
#define FLOWSENSORPIN 2 | |
const int chipSelect = 4; | |
// 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.println("Flow sensor test!"); | |
//lcd.begin(16, 2); | |
Serial.println("Initializing SD card..."); | |
// make sure that the default chip select pin is set to | |
// output, even if you don't use it: | |
pinMode(10, OUTPUT); | |
if (!SD.begin(chipSelect)) { | |
Serial.println("Card failed, or not present"); | |
// don't do anything more: | |
return; | |
} | |
Serial.println("card initialized."); | |
pinMode(FLOWSENSORPIN, INPUT); | |
digitalWrite(FLOWSENSORPIN, HIGH); | |
lastflowpinstate = digitalRead(FLOWSENSORPIN); | |
useInterrupt(true); | |
} | |
void loop() // run over and over again | |
{ | |
//lcd.setCursor(0, 0); | |
//Serial.print("Pulses:"); Serial.print(pulses, DEC); | |
//Serial.print(" Hz:"); | |
//Serial.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; | |
Serial.print(liters); Serial.println(" Liters"); | |
float gallons=liters * 0.2642; | |
Serial.print(gallons); Serial.println(" Gallons"); | |
File dataFile = SD.open("datalog.txt", FILE_WRITE); | |
// if the file is available, write to it: | |
if (dataFile) { | |
dataFile.print("Liters: "); | |
dataFile.print(liters); | |
dataFile.print(" Gallons: "); | |
dataFile.println(gallons); | |
dataFile.close(); | |
// print to the serial port too: | |
//Serial.println(dataString); | |
} | |
// if the file isn't open, pop up an error: | |
else { | |
Serial.println("error opening datalog.txt"); | |
} | |
delay(100); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment