Два датчика холла на Arduino UNO
- Войдите на сайт для отправки комментариев
Ср, 27/01/2016 - 23:08
Всем добрый вечер!
Я недавно начал работать с ардуино. Хочу сделать дозатор воды с помощью магнитного клапана и счетчика потока жидкости (датчик холла). Нашел работающий скетч который измеряет и выводит на COM порт: скорость потока воды, количество воды в итоге. Разобрался как управлять реле при нужных значениях. Далее решил расширить код для считывания данных с двух датчиков (в идеале нужно 4, но видимо придется покупать Мегу). Тут начались проблемы:
1) Теперь работает только второй датчик, который висит на PIN 3. Первый не подает признаков жизни.
2) В окне КОМ порта все очень и очень быстро обновляется. Не успеваю прочитать.
Подскажите, плз, как это исправить.
byte statusLed = 13; // Pokazivaet chto vse rabotaet
byte sensorInterrupt = 0; // 0 = digital pin 2
byte sensorPin = 2; // Podkluchaem datchik holla
byte sensorInterrupt2 = 1; // 1 = digital pin 3
byte sensorPin2 = 3; // Podkluchaem datchik holla 2
int LED_OK = 5;
int buttonStart = 4; // Knopka nachala processa Posle zasipaniya dereva
int relay_s = 6; // RELE podachi sulfata
// The hall-effect flow sensor outputs approximately 4.5 pulses per second per
// litre/minute of flow.
float calibrationFactor = 4.5;
float calibrationFactor2 = 4.5;
volatile byte pulseCount;
volatile byte pulseCount2;
float flowRate;
unsigned int flowMilliLitres;
unsigned long totalMilliLitres;
unsigned long oldTime;
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);
pinMode(sensorPin2, INPUT);
digitalWrite(sensorPin2, HIGH);
pinMode(LED_OK, OUTPUT);
digitalWrite(LED_OK, LOW);
pinMode(buttonStart, INPUT);
pinMode(relay_s, OUTPUT);
pulseCount = 0;
flowRate = 0.0;
flowMilliLitres = 0;
totalMilliLitres = 0;
oldTime = 0;
pulseCount2 = 0;
flowRate2 = 0.0;
flowMilliLitres2 = 0;
totalMilliLitres2 = 0;
oldTime2 = 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);
attachInterrupt(sensorInterrupt2, pulseCounter2, FALLING);
}
/**
* Main program loop
*/
void loop()
{
if((millis() - oldTime) > 1000) // Only process counters once per second
if((millis() - oldTime2) > 1000)
// Disable the interrupt while calculating flow rate and sending the value to
// the host
detachInterrupt(sensorInterrupt);
detachInterrupt(sensorInterrupt2);
// 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;
flowRate2 = ((1000.0 / (millis() - oldTime2)) * pulseCount2) / calibrationFactor2;
// 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();
oldTime2 = 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;
flowMilliLitres2 = (flowRate2 / 60) * 1000;
// Add the millilitres passed in this second to the cumulative total
totalMilliLitres += flowMilliLitres;
totalMilliLitres2 += flowMilliLitres2;
unsigned int frac;
// Print the flow rate for this second in litres / minute
Serial.print("Flow rate: ");
Serial.print(int(flowRate)); // Print the integer part of the variable
Serial.print("."); // Print the decimal point
// Determine the fractional part. The 10 multiplier gives us 1 decimal place.
frac = (flowRate - int(flowRate)) * 10;
Serial.print(frac, DEC) ; // Print the fractional part of the variable
Serial.print("L/min");
// Print the number of litres flowed in this second
Serial.print(" Current Liquid Flowing: "); // Output separator
Serial.print(flowMilliLitres);
Serial.print("mL/Sec");
// Print the cumulative total of litres flowed since starting
Serial.print(" Output Liquid Quantity: "); // Output separator
Serial.print(totalMilliLitres);
Serial.println("mL");
unsigned int frac2;
// Print the flow rate for this second in litres / minute
Serial.print("Flow rate2: ");
Serial.print(int(flowRate2)); // Print the integer part of the variable
Serial.print("."); // Print the decimal point
// Determine the fractional part. The 10 multiplier gives us 1 decimal place.
frac2 = (flowRate2 - int(flowRate2)) * 10;
Serial.print(frac2, DEC) ; // Print the fractional part of the variable
Serial.print("L/min");
// Print the number of litres flowed in this second
Serial.print(" Current Liquid Flowing2: "); // Output separator
Serial.print(flowMilliLitres2);
Serial.print("mL/Sec");
// Print the cumulative total of litres flowed since starting
Serial.print(" Output Liquid Quantity2: "); // Output separator
Serial.print(totalMilliLitres2);
Serial.println("mL");
// Reset the pulse counter so we can start incrementing again
pulseCount = 0;
pulseCount2 = 0;
// Enable the interrupt again now that we've finished sending output
attachInterrupt(sensorInterrupt, pulseCounter, FALLING);
attachInterrupt(sensorInterrupt2, pulseCounter2, FALLING);
if (digitalRead(buttonStart) == HIGH)
{
totalMilliLitres = 0;
digitalWrite(relay_s, HIGH);
}
if (totalMilliLitres >= 2000)
{
totalMilliLitres = 0;
digitalWrite(LED_OK, HIGH);
digitalWrite(relay_s, LOW);
}
}
/*
Insterrupt Service Routine
*/
void pulseCounter()
{
// Increment the pulse counter
pulseCount++;
}
void pulseCounter2()
{
// Increment the pulse counter
pulseCount2++;
}