[Solved]:Yun and Frequency Counter functions.

Forum: I can't find a functioning YUN Freq Counter example that runs. Some code will compile and make the PWM outputs, but I can't get a digital input to count a varying frequency as applied to the designated pin. Is there an Arduino better suited for this application? The water flow sensors output a 5V negative going pulse of varying width, so an edge detector is needed. They came with cheap RO controllers that mate to the sensors, and those work fine. ~4400 pulses is a liter, more or less. The Controllers display volume to 10,000 liters and measure flow rate as well. Did a lot reading on http://openenergymonitor.org/ for background, but the Yun doesn't seem to be a favorite.

I started my Yun projects about a month ago, with the idea I could learn to record water flow and keep statistics. In the reconstructed PEX pipes in our house plumbing I have 3 hall effect flow sensors installed. I meter the main line water volume, irrigation flow, and the waste-water to flush the filtration components. Those sensors and water pressure would be metered with one Yun. Plan to use a second Yun to monitor, with proper signal conditioning and scaling, our electrical use. None of these measurements or functions are life-critical but upon successful completion, the project would be duplicated and expanded perhaps to monitor the health of our community well system. It has multiple pumps on 3PH 208 and 240VAC. Monitoring the level of a 20,000 US Gal reservoir, flow meters, air pressure on the service tank and a myriad of ambient conditions would be included to show system health. The intent is to monitor only, and get use statistics, in particular to see if some power savings can be achieved and to get advance notice of maintenance issues via text or web service.

I worked in an Aerospace engineering environment before retirement, so electronics doesn't baffle me, but I'm lacking in coding skill. The rudimentary C++ is beginning to make sense, but beyond VEE, VBA and a little linux I'm dumb. Some of the YUN documentation has errors or omissions. I have been pretty good about getting .ino/pde examples to work, so that's behind me. e.i. I got the WiFi Blue LED (dead out of the box) to function as I thought it should, and the USB LED too, in the advanced configuration panel.

I have successfully managed to hack together a DC voltmeter, DHT11 temp, humidity, and light level and display them on the monitor. A RTC 3231 has been wired up , set up, enabled the 32KHz oscillator and the SQW output. Keeps time. I use a TEK 2465A DV scope for waveforms. I am grateful to the community for the arduino code and examples but for the Yun, I've had to learn to code properly for successful WiFi loading and find that the Yun is pretty short on physical resources, as so many pins have duplicate/triplicate functions. SO I'm probably going to have to choose a different flavor when going for the BIG time.

So to get to the point. I cannot find what I believe is a functioning example of A YUN frequency counter. This is essential to logging water flow and use. The several 'hard-coded" examples do nothing on the monitor or output what the PWM is generating, maybe. Because the pins/functions of the YUN are not serviced the same, or the timers (e.g. TIMER1) can't be accessed if I am using I2C; Frustrated right now. If I can't do it I'd like to know. I 'd like to read and accumulate continuously the pulses from multiple flow sensors at or above 1 KHz. The sensors are three wire, +/-/signal TTL. I have a 1 KHz SQW generator that is suitable for simulations.

I may be faced with an external hardware solution, or hacking the cheap RO controllers that mate to the sensors. That looms as a possibility. Is there an Arduino better suited for this application?

So for now I am about to try to build on an ADS1015 12-Bit ADC - 4 Channel as my next interface while I sort through the Freq Counter issues.

I'd put in a code example that loads and works WiFi. but my Freq Counter efforts are badly broken. So far even the USB (serial) loads haven't worked. And it seems FreqCounter.h includes a file I can't find.

This uses TImer1 - not available on YUN
Frequency Counter Arduino Sketch
by: Jim Lindblom
SparkFun Electronics
License: Beerware
(that's all I am pasting here)

This is Odd file <avr/interrupt.h>

/*
  FreqCounter.h - Library for a Frequency Counter c.
  Created by Martin Nawrath, KHM Lab3, Dec. 2008
  Released into the public domain.
*/

#ifndef FreqCounter_h
#define FreqCounter_h


#include <avr/interrupt.h>
//#if defined(ARDUINO) && ARDUINO >= 100
#include "Arduino.h"
//#else
//#include "WProgram.h"
//#endif

namespace FreqCounter {

	extern unsigned long f_freq;
	extern volatile unsigned char f_ready;
	extern volatile unsigned char f_mlt;
	extern volatile unsigned int f_tics;
	extern volatile unsigned int f_period;
	extern volatile unsigned int f_comp;
	
	void start(int ms);
	
	
}

#endif

I have found several forums including one by feurig on November 3, 2013, that suggest that remapping the YUN pins for the Timer1 function may be a solution that I should pursue. And this "topic,125252.0.html" However at least one 'work-around' <known_16bit_timers.h> will disable SPI. So I'll see where this goes. And Nick Gammon has a software solution that needs more study by this noob. At least now I know I am not missing something obvious.

I am humbled that hundreds of our members/guests at least took the time to review the issue. I'll check back when a solution has been coded and tested. SO far the YUN does individual functions fine, but maybe it's too primitive for the tasks at hand. Good learning platform though and I do have much to learn.
Thanks.

Solved.
For days I was looking at solving interrupts but the obvious solution (example sketch) was in another thread of Nicks. Took a couple of days to absorb all the nuances of the YUN vs other flavors. But it all works. First load over the WiFI.
Sincerely grateful for Nick Gammon's contributions. To test it, I am applying a tone from my Galaxy 4S Android that has a sweep function tone generator APP. Taking the output from the phone jacks 1 VPP AC to a simple 5V 2N3904 amp and direct-couple output to Pin D4, ICP1 InputCaptureUnit on the YUN.

Used the code at

and made it YUN friendly.
And thanks., Nick, for NOT handing me the solution.

// Frequency timer using input capture unit
// Author: Nick Gammon
// Date: 31 August 2013

// Input: Pin D4 on YUN Bill Gilbert 20141015_13:44 Works

#include "Bridge.h"
#include "Console.h"

volatile boolean first;
volatile boolean triggered;
volatile unsigned long overflowCount;
volatile unsigned long startTime;
volatile unsigned long finishTime;

// timer overflows (every 65536 counts)
ISR (TIMER1_OVF_vect) 
{
  overflowCount++;
}  // end of TIMER1_OVF_vect

ISR (TIMER1_CAPT_vect)
  {
  // grab counter value before it changes any more
  unsigned int timer1CounterValue;
  timer1CounterValue = ICR1;  // see datasheet, page 117 (accessing 16-bit registers)
  unsigned long overflowCopy = overflowCount;
  
  // if just missed an overflow
  if ((TIFR1 & bit (TOV1)) && timer1CounterValue < 0x7FFF)
    overflowCopy++;
  
  // wait until we noticed last one
  if (triggered)
    return;

  if (first)
    {
    startTime = (overflowCopy << 16) + timer1CounterValue;
    first = false;
    return;  
    }
    
  finishTime = (overflowCopy << 16) + timer1CounterValue;
  triggered = true;
  TIMSK1 = 0;    // no more interrupts for now
  }  // end of TIMER1_CAPT_vect
  
void prepareForInterrupts ()
  {
  noInterrupts ();  // protected code
  first = true;
  triggered = false;  // re-arm for next time
  // reset Timer 1
  TCCR1A = 0;
  TCCR1B = 0;
  
  TIFR1 = bit (ICF1) | bit (TOV1);  // clear flags so we don't get a bogus interrupt
  TCNT1 = 0;          // Counter to zero
  overflowCount = 0;  // Therefore no overflows yet
  
  // Timer 1 - counts clock pulses
  TIMSK1 = bit (TOIE1) | bit (ICIE1);   // interrupt on Timer 1 overflow and input capture
  // start Timer 1, no prescaler
  TCCR1B =  bit (CS10) | bit (ICES1);  // plus Input Capture Edge Select (rising on D8)
  interrupts ();
  }  // end of prepareForInterrupts
  

void setup () 
  {
      // initialize serial communication:
  Bridge.begin();
  Console.begin(); 
 
  while (!Console){
    ; // wait for Console port to connect.
  }
  Console.println("You're connected to the Console!!!!");
   delay(1000); 
  
  Console.begin();       
  Console.println("Frequency Counter");
  // set up for interrupts
  prepareForInterrupts ();   
  } // end of setup

void loop () 
  {
  // wait till we have a reading
  if (!triggered)
    return;
 
  // period is elapsed time
  unsigned long elapsedTime = finishTime - startTime;
  // frequency is inverse of period, adjusted for clock period
  float freq = F_CPU / float (elapsedTime);  // each tick is 62.5 nS at 16 MHz
  
  Console.print ("Took: ");
  Console.print (elapsedTime);
  Console.print (" counts. ");

  Console.print ("Frequency: ");
  Console.print (freq);
  Console.println (" Hz. ");

  // so we can read it  
  delay (500);

  prepareForInterrupts ();   
}   // end of loop}

Gammon-Yun_FreqCounter_oct15-141015a.zip (1.4 KB)

I tried to do same thing but through different route via handheld frequency meter come with USB port to Yun ( linux side )

frequency 0~250Mhz.