- français
- English
code du prototype 2 avec le light to frequency
Fichier counter.h
/*
Simple library for Arduino implementing a hardware counter
for a Geigier counter for example
Copyright (c) 2011, Robin Scheibler aka FakuFaku
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
* Neither the name of the <organization> nor the
names of its contributors may be used to endorse or promote products
derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef COUNTER_H
#define COUNTER_H
// Link to arduino library
#if ARDUINO >= 100
#include <Arduino.h>
#else
#include <WProgram.h>
#endif
// set bit macro
#ifndef sbi
#define sbi(sfr, bit) (_SFR_BYTE(sfr) |= _BV(bit))
#endif
#define TCCRnA TCCR1A
#define TCCRnB TCCR1B
#define TCNTn TCNT1
#define TIFRn TIFR1
#define TOVn TOV1
#define TIMSKn TIMSK1
#define TOIEn TOIE1
#define TIMERn_OVF_vect TIMER1_OVF_vect
// Defining the Class for the counter
class HardwareCounter
{
// public
public:
HardwareCounter(int timer_pin, long delay);
void start();
int available();
unsigned long count();
// privatee
private:
long _start_time;
long _delay;
unsigned int _count;
unsigned int _pin;
};
#endif /* COUNTER_H */
fichier counter.cpp
/*
Simple library for Arduino implementing a hardware counter
for a Geigier counter for example
Copyright (c) 2011, Robin Scheibler aka FakuFaku
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
* Neither the name of the <organization> nor the
names of its contributors may be used to endorse or promote products
derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "Counter.h"
#include <limits.h>
// need to have the global variable to count
// overflows
unsigned int g_ovf_n;
// Constructor
HardwareCounter::HardwareCounter(int timer_pin, long delay)
{
// register delay
_delay = delay;
// register timer pin
_pin = timer_pin;
}
// call this to start the counter
void HardwareCounter::start()
{
// set pin as digital input
pinMode(_pin, INPUT);
// hardware counter setup ( refer atmega168.pdf chapter 16-bit counter1)
TCCRnA=0; // reset timer/countern control register A
TCCRnB=0; // reset timer/countern control register A
// set timer/counter1 hardware as counter , counts events on pin Tn ( arduino pin 5 on 168, pin 47 on Mega )
// normal mode, wgm10 .. wgm13 = 0
sbi (TCCRnB ,CS10); // External clock source on Tn pin. Clock on rising edge.
sbi (TCCRnB ,CS11);
sbi (TCCRnB ,CS12);
// start counting now
TCCRnB = TCCRnB | 7; // Counter Clock source = pin Tn , start counting now
// set overflow interrupt
TIMSKn |= _BV(TOIEn);
// set start time
_start_time = millis();
// The counter needs to be reset after
// the counter is setup (This is important)!
TCNTn=0; // counter value = 0
// reset number of overflow
g_ovf_n = 0;
// set count to zero (optional)
_count = 0;
}
// call this to read the current count and save it
unsigned long HardwareCounter::count()
{
TCCRnB = TCCRnB & ~7; // Gate Off / Counter Tn stopped
_count = TCNTn; // Set the count in object variable
TCCRnB = TCCRnB | 7; // restart counting
return 0xfffful*g_ovf_n + _count;
}
// This indicates when the count over the determined period is over
int HardwareCounter::available()
{
// get current time
unsigned long now = millis();
// do basic check for millis overflow
if (now >= _start_time)
return (now - _start_time >= _delay);
else
return (ULONG_MAX + now - _start_time >= _delay);
}
// This takes care of the counter overflow problem
ISR(TIMERn_OVF_vect)
{
// increment number of overflows
g_ovf_n++;
}
Fichier du programme
/*
* Copyright (c) 2013, BioDesign for the Real World, Robin Scheibler <fakufaku@gmail.com>,
* Sachiko Hirosue <shirosue@gmail.com>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* Neither the name of the <ORGANIZATION> nor the names of its contributors may
* be used to endorse or promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <math.h>
#include "Counter.h"
#define TIMER_LENGTH 1000 //You can change this parameter if you want the device
//to be more sensitive.
#define NUMBER_OF_CALIBRATION_MEASURES 2 //number of measures used to do the
//calibration
#define AVG_LEN 1
#define PBS 807200.0
#define led_on() digitalWrite(pinLed, HIGH);
#define led_off() digitalWrite(pinLed, LOW);
#define led_config() pinMode(pinLed, OUTPUT);
float blank = 0;
int pinLed = 11;
int pinCounter = 5;
HardwareCounter hwc(pinCounter, TIMER_LENGTH);
void setup()
{
//led_config();
Serial.begin(57600); //begin the serial monitoring to dialog with
Serial.println("Start of the measurements");
pinMode(pinLed, OUTPUT);
//analogWrite(pinLed, 50);g
led_on();
calibration(); //do the calibration of the device.
}
void calibration() //do the calculation of the blank and then substract it
//to all others measures
{
int blank_sum;
for (int j = 0 ; j < NUMBER_OF_CALIBRATION_MEASURES ; j++)
{
for (int i = 0 ; i < AVG_LEN ; i++)
{
hwc.start();
while(!hwc.available());
blank += hwc.count();
}
blank_sum += blank;
}
blank = (float) (blank_sum / NUMBER_OF_CALIBRATION_MEASURES);
blank /= (float)AVG_LEN;
Serial.println("blank :");
Serial.println(blank);
Serial.println(" ");
}
void loop()
{
unsigned long count = 0;
for (int i = 0 ; i < AVG_LEN ; i++)
{
hwc.start();
while(!hwc.available());
count += hwc.count();
}
count /= (float)AVG_LEN;
if(count <= blank )
count = 0;
else if( count > blank )
count -= blank;
Serial.println(count);
if( count != 0 )
{
Serial.println(-log10(count/PBS)); //helps if the concentration are ten
} //multiple serial dilutions
}