Skip to content

Instantly share code, notes, and snippets.

@dwblair
Created May 28, 2014 23:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dwblair/0f7fe40351dbb6dc5339 to your computer and use it in GitHub Desktop.
Save dwblair/0f7fe40351dbb6dc5339 to your computer and use it in GitHub Desktop.
#include <JeeLib.h>
ISR(WDT_vect) { Sleepy::watchdogEvent(); }
long pulseCount = 0; //a pulse counter variable
unsigned long pulseTime,lastTime, duration, totalDuration;
int samplingPeriod=2; // the number of seconds to measure 555 oscillations
int fivefivefive = 13; // the pin that powers the 555 subcircuit
int sensorPin = 0; // the analog pin from which we'll make a temp measurement
void setup()
{
Serial.begin(9600);
pinMode(fivefivefive, OUTPUT);
}
void loop()
{
//Sleepy::loseSomeTime(5000); // sleeping
delay(2000);
// now make a temp measurement
/*
int reading = analogRead(sensorPin);
// converting that reading to voltage, for 3.3v arduino use 3.3
float voltage = reading * 3.3;
voltage /= 1024.0;
// print out the voltage
//Serial.print(voltage); Serial.println(" volts");
// now print out the temperature
float temperatureC = (voltage - 0.5) * 100 ; //converting from 10 mv per degree wit 500 mV offset
//to degrees ((voltage - 500mV) times 100)
Serial.print(temperatureC); Serial.println(" degrees C");
*/
// now make a conductivity measurement
//turn on the 555 system
digitalWrite(fivefivefive,HIGH); //turns on the 555 timer subcircuit
pulseCount=0; //reset the pulse counter
totalDuration=0; //reset the totalDuration of all pulses measured
attachInterrupt(1,onPulse,RISING); //attach an interrupt counter to interrupt pin 1 (digital pin #3) -- the only other possible pin on the 328p is interrupt pin #0 (digital pin #2)
pulseTime=micros(); // start the stopwatch
delay(samplingPeriod*1000); //give ourselves samplingPeriod seconds to make this measurement, during which the "onPulse" function will count up all the pulses, and sum the total time they took as 'totalDuration'
detachInterrupt(1); //we've finished sampling, so detach the interrupt function -- don't count any more pulses
//turn off the 555 system
digitalWrite(fivefivefive,LOW);
if (pulseCount>0) { //use this logic in case something went wrong
double durationS=totalDuration/double(pulseCount)/1000000.; //the total duration, in seconds, per pulse (note that totalDuration was in microseconds)
// print out stats
Serial.print("sampling period=");
Serial.print(samplingPeriod);
Serial.print(" sec; #pulses=");
Serial.print(pulseCount);
Serial.print("; duration per pulse (sec)=");
Serial.println(durationS,5);
}
}
void onPulse()
{
pulseCount++;
//Serial.print("pulsecount=");
//Serial.println(pulseCount);
lastTime = pulseTime;
pulseTime = micros();
duration=pulseTime-lastTime;
totalDuration+=duration;
//Serial.println(totalDuration);
}
@dwblair
Copy link
Author

dwblair commented May 28, 2014

Running on Arduino Mini Pro, it uses 12mA when using 555 timer; 5 mA when sleeping; no low-power sleep modes yet enabled.

@dwblair
Copy link
Author

dwblair commented May 28, 2014

Ah -- problems with serial if use loseSomeTime -- here's the fix:
http://jeelabs.net/boards/7/topics/1760

which goes into next Gist ...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment