Fairly new to Arduino and writing sketches. I'm looking to set up a system for controlling and monitoring a reverse osmosis system using an uno. I will likely be moving to a mega at some point for additional pin outputs. I'm implementing one piece of hardware at a time and learning that before i move onto the next one.
I have managed to provide a gravity pressure sensor reading as a percentage to a 16x2 ic2 display, alarms for low level with a buzzer. Alarm mutes using a microswitch. I am waiting for a waterproof ultrasonic sensor to arrive to replace the pressure sensor to provide more resolution on holding tank water levels. I have also been able to set up a micro sd card reader and clock module to record the water levels to a text file.
Future plans are to use the YF-S201 water flow sensor to provide LPM and total L with an alarm at 40000l of water passed. I have found plenty of examples of code that show how this can be implemented through the use of interrupts. However all the examples I've seen only show the code to run a water flow and totalizer but they do not run other functions and take data from other sensors.
So im trying to understand how that will work in my case. Will the other code just not run while the hall effect flow sensor is outputting a change? (this would likely be upto 6 hours) I need other functions to continue in that time such as a cut off float to shut the water system off when full, recording sensor data to SD card and lcd print. how do i make these happen at the same time? How would i count every pulse while other loop code functions?
Interruptions are not so simple. Many people use them unnecessarily and this often leads to more problems. It seems to me that your project may not need interruptions and you can look at the 'BlinkWithoutDelay' example.
The principle of how a microcontroller can do several things 'simultaneously' is explained. In a similar way, you can read the sensors and display information on the display and to the memory card.
You could use one of the timers to accumulate the pulses from the sensor and check that on a regular basis in your main loop. The timers have a compare/match register that can do the testing for you and possibly generate an interrupt. This is a sWAG as I have never done it with the Arduino but have seen it done successfully in other micros.
The easiest way to do this is use the T1 pin on the Atmega328P to count edges of your signal. This will increment the TCNT1, and every second, you get the current value of TCNT1 and accumulate that value to another variable, and zero out TCNT1 to start counting again. As long as you don't get 65000 edges per second, you don't have to worry about TCNT1 overflowing.
// Enable Counter on T1 pin rising edge
DDRD &= ~(1<<PORTD5);
TCCR1A = 0;
TCCR1C = 0;
TIMSK1 = 0;
TCNT1 = 0;
TCCR1B = 1<<CS12 | 1<<CS11 | 1<<CS10;
// then from a 1 second interrupt handler
// 1 Hz ticker from DS3231
ISR(INT0_vect)
{
SQW_FLAG = true;
// stop timer 1
TCCR1B = 0;
asm("NOP");
cycleCounter = TCNT1;
TCNT1 = 0;
asm("NOP");
TCCR1B = 1<<CS12 | 1<<CS11 | 1<<CS10;
}
// then on your main loop, just accumulate the value to another variable
loop()
{
if(SQW_FLAG)
{
SQW_FLAG = false;
total += cycleCounter;
}
}