Can anybody help me with Arduino uno code to get data from 4 flow meter (hall sensor) at the same time
This is urgent
Is suppose the obvious thing is to ask you to present the code you have for just one....
And, show us a good schematic of your proposed circuit.
Show us a good image of your ‘actual’ wiring.
Give links to components.
quick, open IDE and take AnalogToSerial example
This is the two flow meter code which is working....
#define FLOWSENSORPIN 2
#define FLOWSENSORPIN2 3
// count how many pulses!
volatile uint16_t pulses = 0;
volatile uint16_t pulses2 = 0;
// track the state of the pulse pin
volatile uint8_t lastflowpinstate;
volatile uint8_t lastflowpinstate2;
// you can try to keep time of how long it is between pulses
volatile uint32_t lastflowratetimer = 0;
volatile uint32_t lastflowratetimer2 = 0;
// and use that to calculate a flow rate
volatile float flowrate;
volatile float flowrate2;
// Interrupt is called once a millisecond, looks for any pulses from the sensor!
SIGNAL(TIMER0_COMPA_vect) {
uint8_t x = digitalRead(FLOWSENSORPIN);
if (x == lastflowpinstate) {
lastflowratetimer++;
return; // nothing changed!
}
if (x == HIGH) {
//low to high transition!
pulses++;
}
lastflowpinstate = x;
flowrate = 1000.0;
flowrate /= lastflowratetimer; // in hertz
lastflowratetimer = 0;
}
SIGNAL(TIMER2_COMPA_vect) {
uint8_t x2 = digitalRead(FLOWSENSORPIN2);
if (x2 == lastflowpinstate2) {
lastflowratetimer2++;
return; // nothing changed!
}
if (x2 == HIGH) {
//low to high transition!
pulses2++;
}
lastflowpinstate2 = x2;
flowrate2 = 1000.0;
flowrate2 /= lastflowratetimer2; // in hertz
lastflowratetimer2 = 0;
}
void useInterrupt(boolean v) {
if (v) {
// Timer0 is already used for millis() - we'll just interrupt somewhere
// in the middle and call the "Compare A" function above
OCR0A = 0xAF;
TIMSK0 |= _BV(OCIE0A);
} else {
// do not call the interrupt function COMPA anymore
TIMSK0 &= ~_BV(OCIE0A);
}
}
void useInterrupt2(boolean b) {
if (b) {
// Timer0 is already used for millis() - we'll just interrupt somewhere
// in the middle and call the "Compare A" function above
OCR2A = 0xAF;
TIMSK2 |= _BV(OCIE2A);
} else {
// do not call the interrupt function COMPA anymore
TIMSK2 &= ~_BV(OCIE2A);
}
}
void setup()
{
Serial.begin(9600);
pinMode(FLOWSENSORPIN, INPUT);
digitalWrite(FLOWSENSORPIN, HIGH);
lastflowpinstate = digitalRead(FLOWSENSORPIN);
pinMode(FLOWSENSORPIN2, INPUT);
digitalWrite(FLOWSENSORPIN2, HIGH);
lastflowpinstate2 = digitalRead(FLOWSENSORPIN2);
useInterrupt(true);
useInterrupt2(true);
}
void loop() {
float liters = pulses;
liters /= 7.5;
liters /= 60.0;
float liters2 = pulses2;
liters2 /= 7.5;
liters2 /= 60.0;
//Serial.println(level);
Serial.print(liters);
Serial.print(" Liters");
Serial.print(" ");
Serial.print(liters2);
Serial.println(" Liters");
delay(100);
}
but when i edit the code for 4 flow meter by changing the pins and functions it shows errors
This is the code which I edited for 4 flow meter , but it shows error
#define FLOWSENSORPIN 2
#define FLOWSENSORPIN2 3
#define FLOWSENSORPIN3 4
#define FLOWSENSORPIN4 5
// count how many pulses!
volatile uint16_t pulses = 0;
volatile uint16_t pulses2 = 0;
volatile uint16_t pulses3 = 0;
volatile uint16_t pulses4 = 0;
// track the state of the pulse pin
volatile uint8_t lastflowpinstate;
volatile uint8_t lastflowpinstate2;
volatile uint8_t lastflowpinstate3;
volatile uint8_t lastflowpinstate4;
// you can try to keep time of how long it is between pulses
volatile uint32_t lastflowratetimer = 0;
volatile uint32_t lastflowratetimer2 = 0;
volatile uint32_t lastflowratetimer3 = 0;
volatile uint32_t lastflowratetimer4 = 0;
// and use that to calculate a flow rate
volatile float flowrate;
volatile float flowrate2;
volatile float flowrate3;
volatile float flowrate4;
// Interrupt is called once a millisecond, looks for any pulses from the sensor!
SIGNAL(TIMER0_COMPA_vect) {
uint8_t x = digitalRead(FLOWSENSORPIN);
if (x == lastflowpinstate) {
lastflowratetimer++;
return; // nothing changed!
}
if (x == HIGH) {
//low to high transition!
pulses++;
}
lastflowpinstate = x;
flowrate = 1000.0;
flowrate /= lastflowratetimer; // in hertz
lastflowratetimer = 0;
}
SIGNAL(TIMER2_COMPA_vect) {
uint8_t x2 = digitalRead(FLOWSENSORPIN2);
if (x2 == lastflowpinstate2) {
lastflowratetimer2++;
return; // nothing changed!
}
if (x2 == HIGH) {
//low to high transition!
pulses2++;
}
lastflowpinstate2 = x2;
flowrate2 = 1000.0;
flowrate2 /= lastflowratetimer2; // in hertz
lastflowratetimer2 = 0;
}
SIGNAL(TIMER3_COMPA_vect) {
uint8_t x3 = digitalRead(FLOWSENSORPIN3);
if (x3 == lastflowpinstate3) {
lastflowratetimer3++;
return; // nothing changed!
}
if (x3 == HIGH) {
//low to high transition!
pulses3++;
}
lastflowpinstate3 = x3;
flowrate3 = 1000.0;
flowrate3 /= lastflowratetimer3; // in hertz
lastflowratetimer3 = 0;
}
SIGNAL(TIMER4_COMPA_vect) {
uint8_t x4 = digitalRead(FLOWSENSORPIN4);
if (x4 == lastflowpinstate4) {
lastflowratetimer4++;
return; // nothing changed!
}
if (x4 == HIGH) {
//low to high transition!
pulses4++;
}
lastflowpinstate4 = x4;
flowrate4 = 1000.0;
flowrate4 /= lastflowratetimer4; // in hertz
lastflowratetimer4 = 0;
}
void useInterrupt(boolean v) {
if (v) {
// Timer0 is already used for millis() - we'll just interrupt somewhere
// in the middle and call the "Compare A" function above
OCR0A = 0xAF;
TIMSK0 |= _BV(OCIE0A);
} else {
// do not call the interrupt function COMPA anymore
TIMSK0 &= ~_BV(OCIE0A);
}
}
void useInterrupt2(boolean b) {
if (b) {
// Timer0 is already used for millis() - we'll just interrupt somewhere
// in the middle and call the "Compare A" function above
OCR2A = 0xAF;
TIMSK2 |= _BV(OCIE2A);
} else {
// do not call the interrupt function COMPA anymore
TIMSK2 &= ~_BV(OCIE2A);
}
}
void useInterrupt3(boolean a) {
if (a) {
// Timer0 is already used for millis() - we'll just interrupt somewhere
// in the middle and call the "Compare A" function above
OCR3A = 0xAF;
TIMSK3 |= _BV(OCIE3A);
} else {
// do not call the interrupt function COMPA anymore
TIMSK3 &= ~_BV(OCIE3A);
}
}
void useInterrupt4(boolean d) {
if (d ) {
// Timer0 is already used for millis() - we'll just interrupt somewhere
// in the middle and call the "Compare A" function above
OCR4A = 0xAF;
TIMSK4 |= _BV(OCIE4A);
} else {
// do not call the interrupt function COMPA anymore
TIMSK4 &= ~_BV(OCIE4A); }
}
void setup()
{
Serial.begin(9600);
pinMode(FLOWSENSORPIN, INPUT);
digitalWrite(FLOWSENSORPIN, HIGH);
lastflowpinstate = digitalRead(FLOWSENSORPIN);
pinMode(FLOWSENSORPIN2, INPUT);
digitalWrite(FLOWSENSORPIN2, HIGH);
lastflowpinstate2 = digitalRead(FLOWSENSORPIN2);
pinMode(FLOWSENSORPIN3, INPUT);
digitalWrite(FLOWSENSORPIN3, HIGH);
lastflowpinstate3 = digitalRead(FLOWSENSORPIN3);
pinMode(FLOWSENSORPIN4, INPUT);
digitalWrite(FLOWSENSORPIN4, HIGH);
lastflowpinstate4 = digitalRead(FLOWSENSORPIN4);
useInterrupt(true);
useInterrupt2(true);
useInterrupt3(true);
useInterrupt4(true);
}
void loop() {
float liters = pulses;
liters /= 7.5;
liters /= 60.0;
float liters2 = pulses2;
liters2 /= 7.5;
liters2 /= 60.0;
float liters3 = pulses3;
liters3 /= 7.5;
liters3 /= 60.0;
float liters4 = pulses4;
liters4 /= 7.5;
liters4 /= 60.0;
//Serial.println(level);
Serial.print(liters);
Serial.print(" Liters");
Serial.print(" ");
Serial.print(liters2);
Serial.println(" Liters");
Serial.print(" ");
Serial.print(liters3);
Serial.println(" Liters");
Serial.print(" ");
Serial.print(liters4);
Serial.println(" Liters");
delay(100);
}
Too "urgent" to use code tags, and make it easier for others to help you?
(Was the code unindented originally, or was that the forum taking it out?)
// Timer0 is already used for millis() - we'll just interrupt somewhere
// in the middle and call the "Compare A" function above
OCR3A = 0xAF;
TIMSK3 |= _BV(OCIE3A);
Arduino UNO and Arduino Nano don't have a "Timer3". Your sketch compiles without error or warning when I tell it to compile for Arduino MEGA which does have a Timer3 (and Timer4 and Timer5).
To do more than two interrupt pins on an Arduino UNO/Nano, use the PinChangeInterrupt library.
You might like to shorten / simplify your code using arrays.
That’ll make it a lot easier to read and debug.
Claiming "urgency" is an excellent way to cause people to ignore your posts for a day or two. ![]()
OP needs to read "how to post code" and then use the edit function (the "pencil" icon) to correct #6 and #7.
And of course, using interrupts is probably a Very Bad Idea. ![]()
What to use? Please can u help me with the code
Yep. Unless you measure something supersonic.
Just a few lines of code to put sensor clicks into an array (untested).
Leo..
const byte sensorPin[] {4, 5, 6, 7}; // pins
unsigned long sensorValue[4]; // accumulated value
boolean TickTock[4];
void setup() {
}
void loop() {
for (byte i = 0; i < 4; i++) { // test
if (digitalRead(sensorPin[i]) != TickTock[i]) { // if changed
TickTock[i] = !TickTock[i]; // flip
sensorValue[i] ++; // add
}
}
// use sensorValue[0] to sensorValue[3] here
}
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.