Skip to content

Instantly share code, notes, and snippets.

@dwblair
Last active August 29, 2015 14:01
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/30c3378906cd15288e22 to your computer and use it in GitHub Desktop.
Save dwblair/30c3378906cd15288e22 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
// https://learn.adafruit.com/tmp36-temperature-sensor
void setup()
{
Serial.begin(9600);
pinMode(fivefivefive, OUTPUT);
}
void loop()
{
Serial.println("yawn ...");
Serial.flush(); // this and next line for serial printouts only -- need to make sure serial flushed before sleeping
delay(2);
Sleepy::loseSomeTime(5000); // sleeping
delay(2); //for serial printouts only -- need to ensure uController woke up before printing
Serial.println("-- good morning!");
// 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

Fixed the serial issue -- and funnily, the oscillation periods have now returned to the values that are closer to what was theoretically expected. Odd. Looks like the Serial print statements might have affected the timings ... need to look into this ...

@dwblair
Copy link
Author

dwblair commented May 28, 2014

Now the code takes approx 12 mA when awake, and .5 mA when asleep. Not bad!

@dwblair
Copy link
Author

dwblair commented May 29, 2014

Sample output:

19.29 degrees C
sampling period=2 sec; #pulses=14; duration per pulse (sec)=0.14925
yawn ...
-- good morning!
19.29 degrees C
sampling period=2 sec; #pulses=14; duration per pulse (sec)=0.14916
yawn ...
-- good morning!
19.29 degrees C
sampling period=2 sec; #pulses=14; duration per pulse (sec)=0.14913
yawn ...
-- good morning!
19.61 degrees C
sampling period=2 sec; #pulses=14; duration per pulse (sec)=0.14924
yawn ...

And this is pretty good (is it a coincidence how close the values are?) given the theoretical prediction for the duration per pulse, using the astable 555 timer formula, and the resistor and capacitor values I'd used:

RA=21000;
RB=47;
C=10_10__-6;
frequency=1/(0.7_(RA+2_RB)_C);
1/f = duration per pulse = 0.14765799999999998

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