help using pin 3 for sensor reading- multiple sensors

I want to keep this thread pointed to the subject so others can learn too. I want to have multiple sensors on a mega 2560. By multiple I mean flow sensors in this immediate issue, but in the future add temp and water temp, humidity and so on. I am using someone else code for a single sensor and it is working well. I am having an issue getting the second to work also. I verified the code but it doesn't spit out the data for sensor 2. I did verify that the MEGA uses pins 2,3,18,19 for sensors.

I doubled up the code which made sense to me but while I have one working the second shows the print function but no data leading me to believe it is pin three that I haven't activated to work with the sensor via code. I am brand new to coding and I believe that is the most important part of Arduino. so teach me please! Please, don't tell me what i am doing wrong without an explanation of how to fix it and why it is wrong- remember we want to learn not simply follow someone else instructions. :)

The question is: how do I make the 2nd sensor work off analog pin 3 (analog since it is data not an on/off sensor). Or more practically put- how do I enable multiple sensors to show what it is sensing?

Heres the code: /* Liquid flow rate sensor -DIYhacking.com Arvind Sanjeev

Measure the liquid/water flow rate using this code. Connect Vcc and Gnd of sensor to arduino, and the signal line to arduino digital pin 2.

Expanding to measure two or more sensors by Artemas with help from the Arduino family

*/

byte statusLed = 13;

byte sensorInterrupt = 0; // 0 = digital pin 2 byte sensorPin = 2; // The hall-effect flow sensor outputs approximately 4.5 pulses per second per // litre/minute of flow. float calibrationFactor = 4.5; volatile byte pulseCount; float flowRate; unsigned int flowMilliLitres; unsigned long totalMilliLitres; unsigned long oldTime;

byte sensorInterrupt2 = 1; // 1 = digital pin 3 byte sensorPin2 = 3; // The hall-effect flow sensor outputs approximately 4.5 pulses per second per // litre/minute of flow. float calibrationFactor2 = 4.5; volatile byte pulseCount2; float flowRate2; unsigned int flowMilliLitres2; unsigned long totalMilliLitres2; unsigned long oldTime2;

void setup() {

// Initialize a serial connection for reporting values to the host Serial.begin(9600);

// Set up the status LED line as an output pinMode(statusLed, OUTPUT); digitalWrite(statusLed, HIGH); // We have an active-low LED attached

pinMode(sensorPin, INPUT); digitalWrite(sensorPin, HIGH);

pulseCount = 0; flowRate = 0.0; flowMilliLitres = 0; totalMilliLitres = 0; oldTime = 0;

// The Hall-effect sensor is connected to pin 2 which uses interrupt 0. // Configured to trigger on a FALLING state change (transition from HIGH // state to LOW state) attachInterrupt(sensorInterrupt, pulseCounter, FALLING);

}

/** * Main program loop */ void loop() {

if((millis() - oldTime) > 1000) // Only process counters once per second { // Disable the interrupt while calculating flow rate and sending the value to // the host detachInterrupt(sensorInterrupt);

// Because this loop may not complete in exactly 1 second intervals we calculate // the number of milliseconds that have passed since the last execution and use // that to scale the output. We also apply the calibrationFactor to scale the output // based on the number of pulses per second per units of measure (litres/minute in // this case) coming from the sensor. flowRate = ((1000.0 / (millis() - oldTime)) * pulseCount) / calibrationFactor;

// Note the time this processing pass was executed. Note that because we've // disabled interrupts the millis() function won't actually be incrementing right // at this point, but it will still return the value it was set to just before // interrupts went away. oldTime = millis();

// Divide the flow rate in litres/minute by 60 to determine how many litres have // passed through the sensor in this 1 second interval, then multiply by 1000 to // convert to millilitres. flowMilliLitres = (flowRate / 60) * 1000;

// Add the millilitres passed in this second to the cumulative total totalMilliLitres += flowMilliLitres;

unsigned int frac;

// Print the flow rate for GROW BED 1 this second in litres / minute Serial.print("Flow rate GROW BED 1: "); Serial.print(int(flowRate)); // Print the integer part of the variable Serial.print("L/min"); Serial.print("\t"); // Print tab space

// Print the cumulative total of GROW BED 1 litres flowed since starting Serial.print("Output Liquid Quantity: "); Serial.print(totalMilliLitres); Serial.println("mL"); Serial.print("\t"); // Print tab space Serial.print(totalMilliLitres/1000); Serial.print("L");

// Reset the pulse counter so we can start incrementing again pulseCount = 0; // Enable the interrupt again now that we've finished sending output attachInterrupt(sensorInterrupt, pulseCounter, FALLING);

// flow meter 2 // Print the flow rate for this second in litres / minute Serial.print("Flow rate FISHTANK: "); Serial.print(int(flowRate2)); // Print the integer part of the variable Serial.print("L/min 2"); Serial.print("\t"); // Print tab space

// Print the cumulative total of litres flowed since starting Serial.print("Output Liquid Quantity: "); Serial.print(totalMilliLitres); Serial.println("mL"); Serial.print("\t"); // Print tab space Serial.print(totalMilliLitres/1000); Serial.print("L");

// Reset the pulse counter so we can start incrementing again pulseCount = 0;

// Enable the interrupt again now that we've finished sending output attachInterrupt(sensorInterrupt, pulseCounter, FALLING); } }

/* Insterrupt Service Routine */ void pulseCounter() { // Increment the pulse counter pulseCount++; }

Where do you read the state of pin 3 ?

You have the first sensor generating an interrupt when a pulse happens. You have the second sensor connected to an external interrupt pin, but no interrupt handler handling the interrupts.

PAULS and HeliBob,

Great questions! talk to me like I am a 5th grader. Thanks for speedy replies!

PAUL how do I create the interrupt handler to handle the interrupts? i read the stuff you mention elsewhere on interrupts but it didn't help me.

HELIBOB could you elaborate please? how would I code the reading of the state of pin 3? please give an example.

i know the problem is with in 3 not getting the input but how to i fix that?

You will need a duplicate of the pulseCounter function as well (with updated variables); that's the ISR ( interrupt setvice routine) that PaulS is talking about.

You will also need to tell the Arduino that it needs to use this new function when an interrupt on pin 3 happens. You do that with attachInterrupt in setup().

artemas: talk to me like I am a 5th grader.

Okay. :)

Please read these two posts: How to use this forum - please read. and Read this before posting a programming question ...

I see why you have three stars :)

sterretje: You will need a duplicate of the pulseCounter function as well (with updated variables); that's the ISR ( interrupt setvice routine) that PaulS is talking about.

You will also need to tell the Arduino that it needs to use this new function when an interrupt on pin 3 happens. You do that with attachInterrupt in setup().

Thank you sterretje. That sets me on track again so I can research those parameters. Very helpful!