Hi,
I have a rain gauge attached to an Arduino MEGA2560.
The rain gauge switches a hall-effect sensor (A3144).
Under normal circumstances the output is LOW. When the tipping bucket rain gauge tips it brings the Signal pin 18 momentarily to HIGH.
When this happens an interrupt function is called which in turn sets the flag to 1.
Every second a function is called which checks the status of the flag. In case the flag is set it will then add one tip to the total of tips. Well, at least that's the plan. However, I can't see to have it working, no matter what I try. I would really appreciate if somebody can take a look over my code and guide me in the right direction.
My code is part of a much bigger program, which operates without any problems.
Here are the snippets of the relevant code:
For the variable declaration:
//TBRG
volatile int tbrgPin = 18; //The TBRG hall-effect switch is connected to this pin
int tbrgTips = 0; //TBRG tips counter
float tbrgTotal; //Total amount of precipitation in mm
int tbrgEvent = 0; //Flag to be set by interrupt function
For the setup part:
//TBRG
pinMode(tbrgPin, INPUT_PULLUP);
bouncer.attach(tbrgPin);
bouncer.interval(100); //Interval in ms
For the interrupt:
//Interrupt for when TBRG made a tip
attachInterrupt(digitalPinToInterrupt(tbrgPin), tbrgISR, FALLING);
For the loop function:
void loop(){
DateTime now = rtc.now(); //get the current date-time
if((now.second()) != prevSecond ){
RTC();
SDDailyStats(); //Call the SDDailyStats function and write to SD-card
checkForNewDayFunction();
tbrgFunction();
}
prevSecond = now.second();
rtc.clearINTStatus();
}
For the ISR:
void tbrgISR() {
tbrgEvent == 1; //Set the flag
if (bouncer.update() && bouncer.read() == LOW) {//Check whether this needs to stay inside the ISR
tbrgEvent = ! tbrgEvent;
digitalWrite(tbrgPin, tbrgEvent); }
}
And for the function itself:
void tbrgFunction() {
if (tbrgEvent == 1) {
Serial.print("It works");
tbrgTips = tbrgTips++; //Add 1 to current amount of TBRG tips
tbrgTotal = tbrgTips * 0.2; }
tbrgEvent == 0; //Reset the flag and wait for next interrupt to happen
}
I don't know whether the following information could be relevant or not, so, I will include it anyway.
I have a rotary encoder attached as well (To change LCD menus). This works great and has the following code:
//Interrupt for rotary encoder
PCICR |= (1 << PCIE2);
PCMSK2 |= (1 << PCINT18) | (1 << PCINT19);//18, 19
sei();
I copied this code from the forum but don't really understand how it works. However, the end result is great, so, I don't think there's a problem with this.
Anyway, many thanks for your upcoming responses.
Cheers,
Luc