Water flow sensor yf - Wersja do druku +- Arduino Polska Forum (https://forum.arduinopolska.pl) +-- Dział: Korzystanie z Arduino (https://forum.arduinopolska.pl/dzial-korzystanie-z-arduino) +--- Dział: Programowanie w Arduino (https://forum.arduinopolska.pl/dzial-programowanie-w-arduino) +--- Wątek: Water flow sensor yf (/watek-water-flow-sensor-yf) |
Water flow sensor yf - KarmelYeti - 02-12-2022 Witam Mam pytanie czy jest jakaś możliwość podłączenia 4 czujników przepływu (water flow sensor yf-s401) do arduino nano nie używając multipleksera ? Sensor ten opiera się na czujniku Halla i do transmisji sygnału wykorzystuje przerwania. Problem w tym że arduino które posiadam mają tylko dwa piny obsługujące przerwania pin 2 i 3 . Czy jest może możliwość obsługi tych czujników przez arduino nano i funkcję pinchange ?. Będę bardzo wdzięczny za pomoc bo już od tygodnia nad tym myślę... byte sensorInterrupt_1 = 0; // 0 = digital pin 2 byte sensorPin_1 = 2; float calibrationFactor_1 = 5.5; volatile byte pulseCount_1; byte pulseCount1;//protected transfer float flowRate_1; unsigned int flowMilliLitres_1; unsigned long totalMilliLitres_1; unsigned long totalLitres_1; //For FlowSensor_2 Ble byte sensorInterrupt_2 = 1; // 1 = digital pin 3 byte sensorPin_2 = 3; //float calibrationFactor_2 = 5.44; float calibrationFactor_2 = 6.44; volatile byte pulseCount_2; byte pulseCount2;//protected transfer float flowRate_2; unsigned int flowMilliLitres_2; unsigned long totalMilliLitres_2; unsigned long totalLitres_2; //sensor reading and print timing unsigned long readInterval = 1000; unsigned long lastRead; void setup() { Serial.begin(115200); //Setup_FlowSensor_1 pinMode(sensorPin_1, INPUT); digitalWrite(sensorPin_1, HIGH); pulseCount_1 = 0; flowRate_1 = 0.0; flowMilliLitres_1 = 0; totalMilliLitres_1 = 0; totalLitres_1 = 0; attachInterrupt(sensorInterrupt_1, pulseCounter_1, FALLING); //Setup_FlowSensor_2 pinMode(sensorPin_2, INPUT); digitalWrite(sensorPin_2, HIGH); pulseCount_2 = 0; flowRate_2 = 0.0; flowMilliLitres_2 = 0; totalMilliLitres_2 = 0; totalLitres_2 = 0; attachInterrupt(sensorInterrupt_2, pulseCounter_2, FALLING); } void flowSensors() { if ((millis() - lastRead) > readInterval) // process counters once per second { lastRead += readInterval; noInterrupts(); pulseCount1 = pulseCount_1; pulseCount_1 = 0; pulseCount2 = pulseCount_2; pulseCount_2 = 0; interrupts(); flowRate_1 = (pulseCount1) / calibrationFactor_1; flowRate_2 = (pulseCount2) / calibrationFactor_2; flowMilliLitres_1 = (flowRate_1 / 60) * 1000; totalMilliLitres_1 += flowMilliLitres_1; totalLitres_1 = (totalMilliLitres_1 / 1000); flowMilliLitres_2 = (flowRate_2 / 60) * 1000; totalMilliLitres_2 += flowMilliLitres_2; totalLitres_2 = (totalMilliLitres_2 / 1000); Serial.print("FlowRate_1= "); Serial.print(flowRate_1); Serial.print(" Total_1= "); Serial.println(totalLitres_1); Serial.println(" "); //pulseCount_1 = 0; Serial.print("FlowRate_2= "); Serial.print(flowRate_2); Serial.print(" Total_2= "); Serial.println(totalLitres_2); Serial.println(" "); //pulseCount_2 = 0; } } void pulseCounter_1() { pulseCount_1++; } void pulseCounter_2() { pulseCount_2++; } void loop() { flowSensors(); } RE: Water flow sensor yf - kaczakat - 02-12-2022 https://github.com/NicoHood/PinChangeInterrupt tu jest biblioteka. Może są inne/lepsza, ta pierwsza wpadła z Google na hasło: arduino pin change interrupt library. RE: Water flow sensor yf - KarmelYeti - 02-12-2022 Czy ten kod odpali pin 5 jako przerwanie ? #include "PinChangeInterrupt.h" #define INTERRUPT_PIN 5; int flowPin = 5; double flowRate; volatile int count; void setup() { Serial.begin(9600); pinMode(flowPin, INPUT); attachPinChangeInterrupt(digitalPinToChangeInterrupt(5),Flow, CHANGE); } void loop() { count = 0; interrupsts; delay (1000); noInterrupts(); flowRate = flowRate *60; flowrate = flowRate / 1000; Serial.println (flowRate); } void Flow() { count++ } RE: Water flow sensor yf - kaczakat - 02-12-2022 Zajrzyj do przykładów biblioteki, którą zainstalowałeś, może być dużo bibliotek o tej samej nazwie, a każda inne metody postępowania. Edit: A kod nie zadziała, są literówki, wielkość liter ma znaczenie, nie używasz count, double w AVR nie istnieje, to zwykły float, kod programu wklejaj tu na forum w znaczniki code, nie ruszaj przerwań, drukowanie, delay, millis i wszystko w Arduino z nich korzysta, jak chcesz to możesz wyłączać te które sam włączasz, masz attach i detach w bibliotece. Edit2: W sumie to w przykładach są błędy, poklikaj sobie tym przykładem: https://wokwi.com/projects/349970041591038547 RE: Water flow sensor yf - KarmelYeti - 12-12-2022 (02-12-2022, 23:29)kaczakat napisał(a): Zajrzyj do przykładów biblioteki, którą zainstalowałeś, może być dużo bibliotek o tej samej nazwie, a każda inne metody postępowania.Dzięki wielkie za cenne informację |