Permalink
Find file
1c0fff0 Sep 16, 2012
75 lines (55 sloc) 1.67 KB
long count = 0;
long countPerMinute = 0;
void setup() {
//uncomment this if runing on arduino 16mhz (uno, etc)
//setPwmFrequency(5,64);
setPwmFrequency(9,8); //pin 9 for 8Mhz. Comment this line if running on 16Mhz
analogWrite(9, 180); //starts PWM on pin 9. (replace 9 with 5 if runing on 16Mhz)
//calls 'countPulse' function when interupt pin goes low
//interupt 0 is pin 2 on the Arduino
attachInterrupt(0,countPulse,FALLING);
Serial.begin(9600);
}
void loop() {
delay(10000); //the count is incrementing during this delay
countPerMinute = 6 *count;
Serial.println(countPerMinute,DEC);
count=0; //reset the count
}
void setPwmFrequency(int pin, int divisor) {
byte mode;
if(pin == 5 || pin == 6 || pin == 9 || pin == 10) {
switch(divisor) {
case 1: mode = 0x01; break;
case 8: mode = 0x02; break;
case 64: mode = 0x03; break;
case 256: mode = 0x04; break;
case 1024: mode = 0x05; break;
default: return;
}
if(pin == 5 || pin == 6) {
TCCR0B = TCCR0B & 0b11111000 | mode;
} else {
TCCR1B = TCCR1B & 0b11111000 | mode;
}
} else if(pin == 3 || pin == 11) {
switch(divisor) {
case 1: mode = 0x01; break;
case 8: mode = 0x02; break;
case 32: mode = 0x03; break;
case 64: mode = 0x04; break;
case 128: mode = 0x05; break;
case 256: mode = 0x06; break;
case 1024: mode = 0x7; break;
default: return;
}
TCCR2B = TCCR2B & 0b11111000 | mode;
}
}
void countPulse(){
detachInterrupt(0);
count++;
while(digitalRead(2)==0){
}
attachInterrupt(0,countPulse,FALLING);
}