Hi, I'm just getting started with Arduino. I have a an arduino Uno that I want to use to read from 8 sensors. The sensors are flow meters and output a pulse signal. I have a test working with one sensor.
Here is my test code:
static int siPulseCounter=0; //Variable keeping track of pulse counts
static int siSecondCount=0; //Variable keeping track no. of seconds in the timer1 block
void SetupTimer1() //Setting up timer1 with appropriate register values
{
TCCR1A = 0; // No PWM is used.So set this to zero.
TCCR1B = 1<<CS12 | 0<<CS11 | 0<<CS10; // Input clock is set to F(cpu)/256
TIMSK1 = 1<<TOIE1; // Enable timer interrupt to detect overflow
/***
Set the timer count to overflow to in 1 second.
My board has a 16MHz oscillator.
Reload Timer Value = ((2 ^ Bits) - 1) - ((Input Frequency / Prescale) / Target Frequency)
= 65535 - ((16 x 10 ^6 Hz)/256)/1 Hz
= 65535 - 62500
= 3035
***/
TCNT1=0x0bdb; //3035 in hex
}
ISR(TIMER1_OVF_vect) // Timer1 ISR when it overflows
{
TCNT1=0x0bdb; //Reset Timer1 to count another second
siSecondCount++; //Increase second count
if(siSecondCount>=1) //If 60 Seconds are attained Print the Pulse count to serial port.
{
Serial.print("Pulse Count is ");
Serial.print(siPulseCounter,DEC); //Print as decimal value
Serial.print("\n");
siPulseCounter=0; //Reset counter values for next minute cycle
siSecondCount=0;
}
}
void setup(void)
{
pinMode(iPulsePin, INPUT); // Set Pulsepin to accept inputs
digitalWrite(iPulsePin, LOW);
attachInterrupt(0, count, RISING); // Caputres external interrupt 0 at iPulsepin at rising edge and calls funtion count defined below
Serial.begin(9600); // Begin Serial communication at baudrate 9600 bps
SetupTimer1(); // Call Setuptimer1 function
Serial.println(" Initialising, Please wait...");
}
void loop(void)
{
}
void count() // Function called by AttachInterrupt at interrupt at int 0
{
siPulseCounter++; //increment the pulse count
}
This is working fine and providing the pulses counted for the last hour.
I am using attachInterupt but now realise that the Uno only has 2 external interrupts.
Could this just be done in the loop, so each time it loops it checks to see which of the 8 inputs are high, then increment a counter for that sensor or could it possibly miss a pulse?
Tom