Étape 2: Code
Beaucoup d’essais et revérifié l’arrivé de rendre le code qu’il est en ce moment. Choses peuvent fonctionner différemment pour vos systèmes de plomberie, ou si vous utilisez un équipement différent tout au long du processus. Ce code a fonctionné pour notre projet à l’aide de l’équipement et les méthodes que nous utilisons.
L’image est purement pour la démonstration. Image source http://apcmag.com/arduino-masterclass-part-3-tv-w...
Pour que ce code spécifique, nous avons téléchargé plusieurs « bibliothèques » de comment programmer un arduino pour nos ordinateurs.
Logiciel d’étalonnage de l’Arduino
Remplissez le seau de 5 gallons d’eau et lire le numéro sur l’écran. Divisez ce nombre par 5 et vous avez votre facteur de conversion (con)
#include <LiquidCrystal.h>#include "RunningMedian.h" #define sensor 0 LiquidCrystal lcd(9, 8, 7, 6, 5, 4);
const float lowlim = 3000; // use this to differentiate between background noise and water flowing
float val =0; // Current reading for analog pin float ave; // Running average of the wave amplitude used in calculating Median float RMSAVE = 0; // Running RMS average used in calculating Median float start = 0; // Counter for how long the program runs at each calculation float interval = 0; // Used in calculating the length of the 6 sec calculation float RMStot = 0;
RunningMedian Median = RunningMedian(5); float sum = 0; boolean flag = 0;
void setup() { pinMode(sensor, INPUT); pinMode(17, OUTPUT); // Activated onboard LED (yellow) lcd.begin(8,2); ave = 530; // set average at midpoint }
// Compute 1000 averages, then print final value to a file. void loop(){ start = millis(); for (int j=0; j < 5; j++){ digitalWrite(17, LOW); // Has yellow LED light up when calculating for (int i=0; i <= 5000; i++){ val = (float) analogRead(sensor); ave = (ave * 0.999) + (val * 0.001); RMSAVE = (RMSAVE * 0.998) + .002*(abs((ave-val))); delayMicroseconds (50); //Modulates the sampling rate (now sampling at ~ 1000 Hz?) }
// Calcuulates the average for the 6-second period interval = millis()-start; RMStot = interval*RMSAVE; Median.add(RMStot); } digitalWrite(17, HIGH); // Has yellow LED light up off when resetting
if(Median.getMedian()>lowlim){ sum = sum + Median.getMedian(); flag = 0; TXLED1; }
if (Median.getMedian()
//Print output to LCD-display for calibration lcd.clear(); lcd.setCursor(0, 0); lcd.print(Median.getMedian()); lcd.setCursor(0, 1); lcd.print(sum); }
Logiciel pour la surveillance de processus (enregistre les données de la douche sur EEPROM [mémoire volatile sur Arduino]) :
Installer la bibliothèque d’exécution médiane de Arduino.cc avant le téléchargement de ce code. Programme d’étalonnage empirique permet de déterminer les « lowlim » et "con" (lowlim est utilisé pour séparer le bruit de fond de l’eau qui coule dans le tuyau et « con » est le facteur de conversion utilisé pour calculer la consommation d’eau) :
// include the library code:#include<EEPROM.h> #include "RunningMedian.h" #include <LiquidCrystal.h> #define sensor 0 // initialize the library with the numbers of the interface pins - this is lines up with how we soldered the Arduino to the LCD LiquidCrystal lcd(9, 8, 7, 6, 5, 4);
const float con = 234862; //Test-value for non-screen unit
const float lowlim = 7000; // use this to differentiate between background noise and water flowing
RunningMedian Average = RunningMedian(10); //Running out of memory (SRAM) when this is around 19 - leave plenty space, so no more then 10 adviced I think... RunningMedian Median = RunningMedian(5);
float val =0; // Current reading for analog pin float ave; // Running average of the wave amplitude used in calculating Median float RMSAVE = 0; // Running RMS average used in calculating Median float start = 0; // Counter for how long the program runs at each calculation float interval = 0; // Used in calculating the length of the 6 sec calculation float RMStot = 0; float volume = 0; float sum = 0;
int numshowers = EEPROM.read(0); int k = numshowers*4; //Use this to update the number of showers as stored in the zero-index EEPROM float time = 0;
boolean flag = 0; // Flag for storing when shower is turned off boolean flag2 = 0; // Flag for switching between "H20 used" and "Average use"
void setup() { pinMode(sensor, INPUT); pinMode(17, OUTPUT); // Activated onboard LED (yellow)
lcd.begin(8,2); ave = 530; // set average at midpoint Serial.begin(9600); // Need to write function to put what's in EEPROM memory into the RunningAverage function int l = 0; // Counter for copying from EEPROM to RunningAverage int m = 1; // Counter for finding memory-location in EEPROM to RunningAverage float TempAve = 0; while (l < EEPROM.read(0)) { int ReadAve[3]; ReadAve[0] = EEPROM.read(m)*100; ReadAve[1] = EEPROM.read(m+1)*10; ReadAve[2] = EEPROM.read(m+2); TempAve=ReadAve[0]+ReadAve[1]+ReadAve[2]; Average.add(TempAve/10); m = m + 4; l++; delay(50); }
}
Calculer les moyennes de 1000, puis imprimer la dernière valeur dans un fichier. void loop() {Démarrer = millis() ; pour (int j = 0; j < 5; j ++) {TXLED1; / / vert LED light uo lors du calcul pour (int j’ai = 0; j’ai < = 5000; i ++) {val = (float) analogRead(sensor) ; ave = (ave * 0,999) + (val * 0,001) ; RMSAVE = (RMSAVE * 0.998) + .002*(abs((ave-val))) ; delayMicroseconds (50) ; Module la fréquence d’échantillonnage (maintenant d’échantillonnage à 1000Hz?) // Compute 1000 averages, then print final value to a file. void loop(){ start = millis(); for (int j=0; j < 5; j++){ TXLED1; // Green LED light uo when calculating for (int i=0; i <= 5000; i++){ val = (float) analogRead(sensor); ave = (ave * 0.999) + (val * 0.001); RMSAVE = (RMSAVE * 0.998) + .002*(abs((ave-val))); delayMicroseconds (50); //Modulates the sampling rate (now sampling at 1000 Hz?) } // Calcuulates the average for the 6-second period interval = millis()-start; RMStot = interval*RMSAVE; Median.add(RMStot); } TXLED0; if(Median.getMedian()>lowlim){ sum = sum + Median.getMedian(); //Updates the "sum", amount used (non-converted) if the varlue is over the threshold flag = 1; time = time+interval/60000; // Updates how long the shower was digitalWrite(17, LOW); // Lights up the yellow LED if above threshold } // The following section saves the data from the shower into the EEPROM memory after the Median drops below the threshold, and then resents the "sum". // Total number of showers recorded is stored in the EEPROM memory location 0 [EEPROM.read(0)] if (Median.getMedian() Calcuulates la moyenne de l’intervalle de 6 secondes périodes = millis ()-Démarrer ; RMStot = intervalle * RMSAVE ; Median.Add(RMStot) ; #include<EEPROM.h> void setup(){ Serial.begin(9600); delay(10000); // Gives you 10 seconds to open serial port after uploading sketch Serial.println("Initializing the Serial port"); }int numshowers = EEPROM.read(0); //Records number of showers from the first index in the EEEPROM memory int i = 0; ///Counter that will update how many shower have been printed out to the serial port int k = 1; // Counter that shows which memory address in to get the shower number from int gal;void loop(){ while (i < numshowers) { delay(100); // Keeps serial port from overloading gal = EEPROM.read(k)*100 + EEPROM.read(k+1)*10+EEPROM.read(k+2); Serial.println(gal); Serial.print("Length of Shower: "); Serial.println(EEPROM.read(k+3)); k = k + 4; i++; } Serial.print("Showers taken: "); Serial.println(numshowers); Serial.print("Data points printed: "); Serial.println(i); delay(10000); // Means that the "Showers taken" & "Data points printed" only is repeated every 10 seconds } TXLED0 ; if(Median.getMedian() > lowlim) {somme = somme + Median.getMedian() ; //Updates la « somme », de la quantité utilisée (non converti) si la varlue est sur le drapeau de seuil = 1; temps = time + intervalle/60000; / / mises à jour combien de temps la douche était digitalWrite (17, LOW); / / la LED jaune s’allume si seuilLa section suivante enregistre les données de la douche dans la mémoire EEPROM après que la médiane est inférieure au seuil et puis est offensé par la « somme ». Nombre total d’averses enregistrées est stockée dans l’emplacement de mémoire EEPROM 0 [EEPROM.read(0)]
Logiciel de récupération de données de la douche d’EEPROM sur Arduino :
Télécharger le programme sur l’Arduino et ouvrir l’analyseur série pour lire les données dans la fenêtre Moniteur série
#include < EEPROM.h > void setup() {Serial.begin(9600) ; delay(10000); / / vous donne 10 secondes pour ouvrir le port série après téléchargement croquis Serial.println ("initialisation du port série");} int numshowers = EEPROM.read(0) ; Nombre d’enregistrements de douches du premier index de l’EEEPROM mémoire int i = 0 ; Compteur qui mettra à jour combien de douche ont été imprimés le port série int k = 1 ; Compteur qui indique quelle mémoire adresse dans pour obtenir le numéro de douche de gal int ; void loop() {tandis que (j’ai < numshowers) {delay(100); / / empêche le port série de surcharge gal = EEPROM.read (k) * 100 + EEPROM.read(k+1)*10+EEPROM.read(k+2) ; Serial.println(gal) ; Serial.Print ("longueur de douche:") ; Serial.println(EEPROM.Read(k+3)) ; k = k + 4 ; i ++ ;Serial.Print ("douches prises:") ; Serial.println(numshowers) ; Serial.Print ("les points de données imprimées:") ; Serial.println(i) ; Delay(10000) ; Signifie que le « Douches prises » & « imprimé de points de données » n’est répété toutes les 10 secondes