Show Posts
Pages: [1]
1  Using Arduino / Programming Questions / Re: Newbe Needs Help with Water Flow Sensor Program on: August 17, 2014, 06:31:41 am
I am just learning about functions so my efforts have been hit or miss.  I finally printed out the code last night and went through it line by line so I have a much better understanding of how it works.  I had been working on the assumption the program started counting at the top of the program.  Turns out the trigger is at the end of the program.  I will try to write a new function today and see how well that works.  If I can isolate the trigger and counter then I am home free.

BTW you are correct in everything that you said.  It can only count one at sensor at a  time.  It counts pulses for exactly one second. then stops counting, drops out of the loop to do the calculations.  My plan is to have it count once every 30 seconds for each sensor then calculate total gallons consumed for each sensor and keep a running total.  From there I can determine power usage for getting the water from our well for the two open loop geothermal heat pumps.  I just replaced our 25 year old geothermal heat pump so I am curious if it lives up to the hype.

BTW I can get simple test functions to work so I am making progress.  At my age if I can remember something for more than a day I am lucky!

2  Using Arduino / Programming Questions / Newbe Needs Help with Water Flow Sensor Program on: August 16, 2014, 02:09:03 pm
OK before I get slammed, I have no programming experience and am learning as I go.  I am also an old retired idiot so talk in simple terms and be nice ;-)

Here is my problem:  I used the below sketch to calibrate two field effect water flow sensors.  The program worked great and I calibrated the two sensors then installed them on my two geothermal heat pumps.

I wanted to be able to switch between the two sensors within the program (installed on pins 2 and 3) also wanted to add 5 analog water tank depth sensors.  Plus add some calculations to the  program.

Here is the problem:  I can't seem to add anything to the program without screwing up the timing and the results.  I was reading up on functions and hoped I could create a function that could  pass the interrupt and sensor pin then have the function  write to an array with the values for the flow rate, elapsed time, and gallons consumed.  I have failed miserably for the last two days.  I just can't figure it out.  I am hoping that someone with some experience could help me or tell me I am on the wrong path.

Thanks in advance

 * Water Flow Gauge
 * Uses a hall-effect flow sensor to measure the rate of water flow and
 * output it via the serial connection once per second. The hall-effect
 * sensor connects to pin 2 and uses interrupt 0, and an LED on pin 13
 * pulses with each interrupt. Two volume counters and current flow rate
 * are also displayed on a 2-line by 16-character LCD module, and the
 * accumulated totals are stored in non-volatile memory to allow them to
 * continue incrementing after the device is reset or is power-cycled.
 * Two counter-reset buttons are provided to reset the two accumulating
 * counters. This allows one counter to be left accumulating indefinitely
 * as a "total" flow volume, while the other can be reset regularly to
 * provide a counter for specific events such as having a shower, running
 * an irrigation system, or filling a washing machine.
 * Copyright 2009 Jonathan Oxer <>
 * Copyright 2009 Hugh Blemings <>
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
1239.4L 8073.4L

// Specify the pins for the two counter reset buttons and indicator LED
byte resetButtonA = 11;
byte resetButtonB = 12;
byte statusLed    = 13;

//byte sensorInterrupt = 0;  // 0 = pin 2; 1 = pin 3
//byte sensorPin       = 2;

byte sensorInterrupt = 0;  // 0 = pin 2; 1 = pin 3
byte sensorPin       = 2;

// The hall-effect flow sensor outputs approximately 4.5 pulses per second per
// litre/minute of flow.
//float calibrationFactor = 4.5;
float calibrationFactorHeat = 4.5*3.78541*1.1; //adjustment to gallons and calibration green wire heat pump
float calibrationFactorHot = (4.5*3.78541)* 1.30; //adjustment to gallons and calibration white wire hot water
volatile byte pulseCount; 

float flowRate;
unsigned int flowGallons;
unsigned long totalGallonsA;
unsigned long totalGallonsB;

unsigned long oldTime;
unsigned long ElapsedMilliseconds;

void setup()

  // Initialize a serial connection for reporting values to the host
  // Set up the status LED line as an output
  pinMode(statusLed, OUTPUT);
  digitalWrite(statusLed, HIGH);  // We have an active-low LED attached
  // Set up the pair of counter reset buttons and activate internal pull-up resistors
  pinMode(resetButtonA, INPUT);
  digitalWrite(resetButtonA, HIGH);
  pinMode(resetButtonB, INPUT);
  digitalWrite(resetButtonB, HIGH);
  pinMode(sensorPin, INPUT);
  digitalWrite(sensorPin, HIGH);

  pulseCount        = 0;
  flowRate          = 0.0;
  flowGallons   = 0;
  totalGallonsA = 0;
  totalGallonsB = 0;
  oldTime = 0;
  ElapsedMilliseconds = 0;

  // The Hall-effect sensor is connected to pin 2 which uses interrupt 0.
  // Configured to trigger on a FALLING state change (transition from HIGH
  // state to LOW state)
  attachInterrupt(sensorInterrupt, pulseCounter, FALLING);

 * Main program loop
void loop()
  if(digitalRead(resetButtonA) == LOW)
    totalGallonsA = 0;

  if(digitalRead(resetButtonB) == LOW)
    totalGallonsB = 0;

  if( (digitalRead(resetButtonA) == LOW) || (digitalRead(resetButtonB) == LOW) )
    digitalWrite(statusLed, LOW);
  } else {
    digitalWrite(statusLed, HIGH);
  if((millis() - oldTime) > 1000)    // Only process counters once per second
    // Disable the interrupt while calculating flow rate and sending the value to
    // the host
    //lcd.setCursor(15, 0);
    // Because this loop may not complete in exactly 1 second intervals we calculate
    // the number of milliseconds that have passed since the last execution and use
    // that to scale the output. We also apply the calibrationFactor to scale the output
    // based on the number of pulses per second per units of measure (litres/minute in
    // this case) coming from the sensor.
  //  flowRate = ((1000.0 / (millis() - oldTime)) * pulseCount) / calibrationFactorHeat;
    flowRate = ((((millis() - oldTime) / 1000) * pulseCount) / calibrationFactorHeat); 
    // Note the time this processing pass was executed. Note that because we've
    // disabled interrupts the millis() function won't actually be incrementing right
    // at this point, but it will still return the value it was set to just before
    // interrupts went away.
    // added next line
    ElapsedMilliseconds = ((millis()-oldTime) + millis());//corrected millis use for cals
           Serial.println("  (millis()-oldTime)");
    oldTime = millis();
    // Divide the flow rate in litres/minute by 60 to determine how many litres have
    // passed through the sensor in this 1 second interval, then multiply by 1000 to
    // convert to Gallons.
    // flowGallons = (flowRate / 60) * 1000;   
    flowGallons = (flowRate / 60) * 1000;
    //flowGallons = (flowRate / ElapsedMilliseconds) * 1000; //changed to this
    // Add the Gallons passed in this second to the cumulative total
    totalGallonsA += flowGallons;
    totalGallonsB += flowGallons;
    // During testing it can be useful to output the literal pulse count value so you
    // can compare that and the calculated flow rate against the data sheets for the
    // flow sensor. Uncomment the following two lines to display the count value.
    Serial.print(pulseCount, DEC);
    Serial.println ("  pulseCount");
    // Write the calculated value to the serial port. Because we want to output a
    // floating point value and print() can't handle floats we have to do some trickery
    // to output the whole number part, then a decimal point, then the fractional part.
    unsigned int frac;
    // Print the flow rate for this second in litres / minute

    Serial.print(int(flowRate));  // Print the integer part of the variable
    Serial.print(".");             // Print the decimal point
    // Determine the fractional part. The 10 multiplier gives us 1 decimal place.
    frac = (flowRate - int(flowRate)) * 10;
    Serial.print(frac, DEC) ;      // Print the fractional part of the variable
    Serial.println("  GPM ");
    // Print the number of litres flowed in this second
   // Serial.print(flowGallons);
   // Serial.println("  Flow Gallons ");    // Output separator

    // Print the cumulative total of litres flowed since starting
    Serial.println("  Total Gallons A");             // Output separator
    Serial.println("  Total Gallons A");

 //   if(int(flowRate) < 10)
 //   {
 //     lcd.print(" ");
 //   }


    // Reset the pulse counter so we can start incrementing again
    pulseCount = 0;
    // Enable the interrupt again now that we've finished sending output
    attachInterrupt(sensorInterrupt, pulseCounter, FALLING);
    Serial.println("");    // Output separator



 * Invoked by interrupt0 once per rotation of the hall-effect sensor. Interrupt
 * handlers should be kept as small as possible so they return quickly.
void pulseCounter()
  // Increment the pulse counter
3  Using Arduino / Project Guidance / Re: Can two Arduinos read one sensor? on: August 11, 2014, 12:42:32 am
You are correct the water sensor uses a pulsed output so there is only one sensor pin used.  The trouble I have is that I have one pin free.  I am using a touch screen shield for the Arduino to display the results of several sensors.  I have to use the Arduino Uno to match another display which is mounted in a wall.

The project monitors 18 temperature sensors, two water flow sensors, 5 tank depth sensors ( reading 5 275 gallon totes) and a probe sensor to control a pump to irrigate the yard when the 5 totes fill to capacity (1500 gallons). I have two geothermal heat pumps that are open loop.  The water from the geothermal is captured in the tanks.  I want to monitor all aspects of the heat pump, hense the abundance of sensors.

Many will say this project is overkill and I should set the sensors up differently but this how I am learning the Arduino AND getting some useful data.

I could see no reason why two Arduinos could not measure the same pulsed voltage output  as long as everything was powered by the same supply.  But just to make sure I thought I would run it past this group.  Worst case is I fry a couple Ardunios or get funny readings.  When I hit 60 I decided to try to teach myself and C and learn the Arduino.  I must say it is fun, but a little frustrating at times.

Thanks for all the ideas.
4  Using Arduino / Project Guidance / Can two Arduinos read one sensor? on: August 10, 2014, 05:44:08 pm
I am fairly new to Arduino hardware and software.  I am using a field effect water flow meter.  If two Arduinos are powered from the same power can two Arduinos read the same sensor without issue or am I too simple minded to understand the issue?

5  Using Arduino / Project Guidance / Re: Anyone interested in a fast PWM in-phase/out of phase library? on: January 10, 2013, 01:21:23 pm
Concept is simple.  I wanted to create an AC type output from two PWM pins using the same timer (timer 1) at a very high speed.  The purpose is to create a higher sound pressure level output from a piezoelectric transducer than the standard pulsed-DC method used in the Tone library.  Basically, instead of a pulsed-DC +5v and 0v (5 volt range) signal, driving two pins out of phase yields an effective +5v and -5v (10 volt range).

The tone library won't work as it doesn't do in/out of phase (also may have high frequency issues when the ATmega is running at low speed).  The TimerOne library won't work because it also doesn't do in/out of phase PWM.

I couldn't find such a library, so I wrote a very fast port register level library that provides a perfect in-phase and out of phase PWM signal on two PWM pins.  On an Arduino Uno you would tie the piezoelectric buzzer to pins 9 and 10 and using my library specify the frequency and you're done.  Even if running the ATmega at 4 MHz like I was, this library can still create an ultrasonic frequency, and much louder than with the Tone library as it's driving both pins out of phase with each other.  It always keeps the duty at 50% for the highest output.

Anyway, did I miss a library that does this?  Or, is there interest in me turning it into a library for the community.  It wouldn't be difficult, but there's no reason to bother if no one is interested.  Think of it like a high-power and faster version of the Tone library but fixed to the assigned PWM pins for timer 1 (9 & 10 for Uno) (14 & 15 for Teensy 2.0) (11 & 12 for Mega).


While I don't have a project right now that would use it, why would anyone say no to a new useful library?

6  Using Arduino / Displays / Re: Hacktronics 20x4 LCD Ripping my hair out ;repost with complete information. on: November 27, 2012, 04:54:12 pm
Just in the nick of time, I had one hair left. 
Thanks to your suggestions I went back to square one and tried to load Blink.  No luck loading Blink so I took a look at my com ports and I had 10+ of the darn things ranging into the high 20's.  On top of that apparently 1.0.2 does not warn if the com port was not found, or at least it did not with mine.  So going back to square one I confirmed which com port that Arduino was using and selected it from the menu and the darn thing uploaded and started working.  Talk about feeling like and idiot!  All is good with life again and I hope the rest of the hairs grow back.

Thanks for all the help...I learned an important lesson here.
7  Using Arduino / Displays / Re: Hacktronics 20x4 LCD Ripping my hair out ;repost with complete information. on: November 27, 2012, 02:28:26 pm
Serial monitor is empty for new (out of the box Arduino) with Sketch loaded.  Same with Arduino connected to the display and with the display removed (nothing in the serial monitor).

I will try to upload blinky and see what happens.    I will also try different machine to upload.

Still tearing out my hair...not many left!
8  Using Arduino / Displays / Re: Hacktronics 20x4 LCD Ripping my hair out ;repost with complete information. on: November 27, 2012, 01:06:40 pm
Display disconnected, tried power from wall wart on two different Arduino UNO R3's with same results. While I am a newbie at this I have completed several projects, one using an LCD shield, lots of LED projects, one wire i2c temperature sensors and two wire i2c for LCD.  Normally I can figure it out within a few hours using sample code or looking around the internet.  This one has me stumped and I have three of these things to be used for pending projects. 

Thanks for the help so far.

What next?
9  Using Arduino / Displays / Hacktronics 20x4 LCD Ripping my hair out ;repost with complete information. on: November 27, 2012, 12:48:46 pm
Repost...not sure why the first one posted before I was done.
I have been ripping my hair out for three days trying to get the Hacktronics 20x4 LCD to work in 4 bit mode. Here is the pin config that they recommend

1 (VSS) to GND Arduino pin*
2 (VDD) to + 5v Arduino pin
3 (contrast) to potentiometer to GND Arduino pin*
4 RS to Arduino pin 12
5 R/W to Arduino pin 11
6 Enable to Arduino pin 10
7 No connection
8 No connection
9 No connection
10 No connection
11 (Data 4) to Arduino pin 5
12 (Data 5) to Arduino pin 4
13 (Data 6) to Arduino pin 3
14 (Data 7) to Arduino pin 2
15 Backlight to Resistor to Arduino pin 13**
16 Backlight GND to GND Arduino pin*
This is the demo sketch that I am using
// character LCD example code

#include <LiquidCrystal.h>

// Connections:
// rs (LCD pin 4) to Arduino pin 12
// rw (LCD pin 5) to Arduino pin 11
// enable (LCD pin 6) to Arduino pin 10
// LCD pin 15 to Arduino pin 13
// LCD pins d4, d5, d6, d7 to Arduino pins 5, 4, 3, 2
LiquidCrystal lcd(12, 11, 10, 5, 4, 3, 2);

int backLight = 13;    // pin 13 will control the backlight

void setup()
  pinMode(backLight, OUTPUT);
  digitalWrite(backLight, HIGH); // turn backlight on. Replace 'HIGH' with 'LOW' to turn it off.
  lcd.begin(20,4);              // columns, rows.  use 16,2 for a 16x2 LCD, etc.
  lcd.clear();                  // start with a blank screen
  lcd.setCursor(0,0);           // set cursor to column 0, row 0 (the first row)
  lcd.print("Hello, World");    // change this text to whatever you like. keep it clean.
  lcd.setCursor(0,1);           // set cursor to column 0, row 1
  // if you have a 4 row LCD, uncomment these lines to write to the bottom rows
  // and change the lcd.begin() statement above.
  //lcd.setCursor(0,2);         // set cursor to column 0, row 2
  //lcd.print("Row 3");
  //lcd.setCursor(0,3);         // set cursor to column 0, row 3
  //lcd.print("Row 4");

void loop()

Problem is that no characters are displayed and the backlight flashes on and off (like Blinky) along with onboard LED  Moved backlight to +5 with resistor solving that problem.  Contrast works fine.  I get two lines of square boxed even though sketch set to 20x4.  Program complies fine with no errors however when uploading to Ardunio UNO R3 I get the following message in lower box of compiler " Binary Sketch.........(of a 32.256 byte maximum)  avrdude: stk500_getsync() : not in sync: resp=0x00"

I have soldered the pins to the LCD and pushed them into the breadboard.  I have tripple checked the wiring.  No matter what I do I have the same problem plus the LED on pin 13 continues to blink.  Thinking I might have damaged or miswired something I loaded the Sketch onto a brand new Arduino UNO R3 (not attached to the LCD) and got the same blinking LED.  Since there is nothing in the Void Loop why is the LED flashing?
This morning I updated the Arduino compiler to 1.0.2 and tried the example sketch changing the wiring to match that sketch....same exact problem.
Please be gental with me I am an old guy and can't take much more ;-)

10  Using Arduino / Displays / Re: SainSmart 20 x 4 LCD wanting to use I2C from Arduino Uno on: November 22, 2012, 01:06:04 pm
I had same problem reported by others using the SainSmart examples and libraries.  Finally found this and followed Andrew's advice and now it works!  Must agree with displayed message.

Thanks guys!
Pages: [1]