Advertisement
Guest User

Untitled

a guest
Jan 23rd, 2019
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.45 KB | None | 0 0
  1.  
  2. #include <HX711_ADC.h>
  3. #include <Wire.h>
  4. #include <LiquidCrystal_I2C.h>                // LCD i2c Library einbinden
  5. HX711_ADC LoadCell(3, 4); // parameters: dt pin, sck pin<span data-mce-type="bookmark" style="display: inline-block; width: 0px; overflow: hidden; line-height: 0;" class="mce_SELRES_start"></span>
  6.  
  7. int maxU = 2700;
  8. int potpin =0;
  9. int raw,raw_last,raw_min,raw_max=0;
  10. int rawdown;
  11. int hysterese=10;
  12. int ledPin =9;
  13. int writeValue; // Use this variable for writing to LED
  14.  
  15. LiquidCrystal_I2C lcd1(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);
  16. LiquidCrystal_I2C lcd2(0x3F, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);
  17.  
  18. const int Eingang = 2;                        // Pin 2 ist Eingang für Sensor
  19. const int AVG = 4;                            // Glättung über mindestens 4 Messwerte
  20. volatile unsigned long dauer=0;               // microsekunden seit dem letzten Interrupt
  21. volatile unsigned long last=0;                // Zählerwert beim letzten Interrupt
  22. volatile unsigned long average=1;             // Integrierte Dauer // =1: divide by 0 vermeiden
  23. volatile int avgcnt=0;                        // Anzahl Messwerte im Puffer
  24.  
  25.  
  26. void setup()
  27. {
  28.   // put your setup code here, to run once:
  29.  
  30.   Serial.begin(9600);
  31.   Serial.println("Gewählte Drehzahl in Prozent=");
  32.   Serial.println();
  33.  
  34.   pinMode(ledPin, OUTPUT);
  35.   pinMode(potpin, INPUT);                    // Eingangspin auf Eingang stellen
  36.  
  37.  
  38.   lcd2.begin(16, 2);                           // Display hat 2 Zeilen a 16 Zeichen
  39.   lcd2.print("Drehzahl  ");
  40.  
  41.   attachInterrupt(0, readmicros, RISING );    // Interrupt
  42.  
  43.   LoadCell.begin();
  44.   LoadCell.start(2000);
  45.   LoadCell.setCalFactor(106763.15789474/9.81);
  46.   lcd1.begin(16,2);
  47.   lcd1.backlight();
  48. }
  49. void loop(){
  50.  
  51.   raw=analogRead(potpin);
  52.   raw_min=raw_last-hysterese;
  53.   raw_max=raw_last+hysterese;
  54.   rawdown=raw-100;
  55.  
  56.  
  57.  
  58.   writeValue = (255./1023.) * raw; //Calculate Write Value for LED
  59.   analogWrite(ledPin, writeValue);  
  60.  
  61.   if((raw!=raw_last))
  62.   {
  63.     if((raw>raw_max)||(raw<raw_min))
  64.     {
  65.     Serial.println(raw/10);
  66.     lcd2.setCursor(0, 00);
  67.     lcd2.print("Drehzahl  ");
  68.     lcd2.print(raw/10);
  69.     lcd2.print("%");
  70.     raw_last=raw;
  71.     }
  72.   }            
  73.   char buf[17];                               // Pufferstring für sprintf
  74.   unsigned long drehzahl=0; // selbstredend
  75.   if (dauer != 0) {
  76.     drehzahl = myround(60000000 / average);   // Drehzahl ausrechnen und runden
  77.   }
  78.   else
  79.   {
  80.     drehzahl = 0;                             // keine Messung? -> Stillstand
  81.     avgcnt = 0;                  
  82.   }
  83.   sprintf(buf, "%4lu/min ", drehzahl);
  84.   Serial.println();
  85.  
  86.  
  87.   // als 5stellig formatierte Zahl in den Puffer schreiben
  88.   lcd2.setCursor(0, 1);                        // cursor an den Anfang der 2. Zeile (fängt mit 0 an)
  89.   lcd2.print(buf);                             // Puffer ausgeben
  90.   lcd2.print(maxU);                            // Max UPM Schwellwert zum schalten eines Relais ab xUPM
  91.   lcd2.print("Max");                           // Max UPM Anzeige Display
  92.   dauer >>= 10;                               // Flag für Stillstand ( : 1024 )
  93.   delay(500);                                 //eine halbe Sekunde schlafen
  94.  
  95.   LoadCell.update();
  96.   float i = LoadCell.getData();
  97.   lcd1.setCursor(0, 00);
  98.   lcd1.print("force [Nm]: "); //Ausgabe am LCD1
  99.   lcd1.setCursor(0, 1);
  100.   lcd1.print(i, 3);
  101.   }
  102.                                               // mehr als zwei Werte pro Sekunde kann eh keiner lesen
  103. void readmicros() {                           // Interrupt-Routine
  104.   detachInterrupt(0);                         // Interrupt ausschalten damit er uns nicht beißt
  105.   int avgmax;
  106.   unsigned long us = micros();                // Microsekundenzähler auslesen
  107.   if (last == 0) {                            // erster Messwert?
  108.     last = us;                                // merken und nicht weiter bearbeiten
  109.   } else {
  110.     if ( us < last ) {                        // Zählerüberlauf
  111.       dauer = 4294967295 - last + us;         // erzeugt einen Fehler von 1µS - vernachlässigbar
  112.     } else {
  113.       dauer = us - last;                      // Differenz zum letzten Durchlauf berechnen
  114.     }
  115.     if (dauer > 5000) {                       // ignorieren wenn <= 5ms (Kontaktpreller)
  116.       average = dauer + average * avgcnt++;   // Wert in buffer und mit Faktor avgcnt glätten
  117.       average /= avgcnt;                      // und zurückrechnen
  118.       avgmax = 1000000 / dauer;               // dynamische Größe des Integrationspuffers
  119.       if (avgmax < AVG) avgmax = AVG;         // Trägheit mindestens 1 Sekunde
  120.       if (avgcnt >= avgmax) avgcnt--;
  121.       last = us;                              // und wieder den letzten Wert merken
  122.     }
  123.   }
  124.   attachInterrupt(0, readmicros, RISING );    // Interrupt wieder einschalten
  125.   }
  126.   unsigned long myround(unsigned long value) {  // Gewichtete Rundung
  127.   int rto;
  128.   if (value > 3000) {                         // Rundungswert bestimmen
  129.     rto = 100;
  130.   } else if (value > 1500) {
  131.     rto = 50;
  132.   } else if (value > 500) {
  133.     rto = 10;
  134.   } else if (value > 100) {
  135.     rto = 5;
  136.   } else {
  137.     return (value);
  138.   }
  139.   return (_myround(value, rto));
  140. }
  141.  
  142. unsigned long _myround(unsigned long value, int roundto) {
  143.   value += (roundto >> 1);                    // halben roundto Wert addieren
  144.   value /= roundto;                           // integer division
  145.   value *= roundto;                           // integer multiplikation
  146.   return (value);
  147.  
  148. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement