Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <HX711_ADC.h>
- #include <Wire.h>
- #include <LiquidCrystal_I2C.h> // LCD i2c Library einbinden
- 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>
- int maxU = 2700;
- int potpin =0;
- int raw,raw_last,raw_min,raw_max=0;
- int rawdown;
- int hysterese=10;
- int ledPin =9;
- int writeValue; // Use this variable for writing to LED
- LiquidCrystal_I2C lcd1(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);
- LiquidCrystal_I2C lcd2(0x3F, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);
- const int Eingang = 2; // Pin 2 ist Eingang für Sensor
- const int AVG = 4; // Glättung über mindestens 4 Messwerte
- volatile unsigned long dauer=0; // microsekunden seit dem letzten Interrupt
- volatile unsigned long last=0; // Zählerwert beim letzten Interrupt
- volatile unsigned long average=1; // Integrierte Dauer // =1: divide by 0 vermeiden
- volatile int avgcnt=0; // Anzahl Messwerte im Puffer
- void setup()
- {
- // put your setup code here, to run once:
- Serial.begin(9600);
- Serial.println("Gewählte Drehzahl in Prozent=");
- Serial.println();
- pinMode(ledPin, OUTPUT);
- pinMode(potpin, INPUT); // Eingangspin auf Eingang stellen
- lcd2.begin(16, 2); // Display hat 2 Zeilen a 16 Zeichen
- lcd2.print("Drehzahl ");
- attachInterrupt(0, readmicros, RISING ); // Interrupt
- LoadCell.begin();
- LoadCell.start(2000);
- LoadCell.setCalFactor(106763.15789474/9.81);
- lcd1.begin(16,2);
- lcd1.backlight();
- }
- void loop(){
- raw=analogRead(potpin);
- raw_min=raw_last-hysterese;
- raw_max=raw_last+hysterese;
- rawdown=raw-100;
- writeValue = (255./1023.) * raw; //Calculate Write Value for LED
- analogWrite(ledPin, writeValue);
- if((raw!=raw_last))
- {
- if((raw>raw_max)||(raw<raw_min))
- {
- Serial.println(raw/10);
- lcd2.setCursor(0, 00);
- lcd2.print("Drehzahl ");
- lcd2.print(raw/10);
- lcd2.print("%");
- raw_last=raw;
- }
- }
- char buf[17]; // Pufferstring für sprintf
- unsigned long drehzahl=0; // selbstredend
- if (dauer != 0) {
- drehzahl = myround(60000000 / average); // Drehzahl ausrechnen und runden
- }
- else
- {
- drehzahl = 0; // keine Messung? -> Stillstand
- avgcnt = 0;
- }
- sprintf(buf, "%4lu/min ", drehzahl);
- Serial.println();
- // als 5stellig formatierte Zahl in den Puffer schreiben
- lcd2.setCursor(0, 1); // cursor an den Anfang der 2. Zeile (fängt mit 0 an)
- lcd2.print(buf); // Puffer ausgeben
- lcd2.print(maxU); // Max UPM Schwellwert zum schalten eines Relais ab xUPM
- lcd2.print("Max"); // Max UPM Anzeige Display
- dauer >>= 10; // Flag für Stillstand ( : 1024 )
- delay(500); //eine halbe Sekunde schlafen
- LoadCell.update();
- float i = LoadCell.getData();
- lcd1.setCursor(0, 00);
- lcd1.print("force [Nm]: "); //Ausgabe am LCD1
- lcd1.setCursor(0, 1);
- lcd1.print(i, 3);
- }
- // mehr als zwei Werte pro Sekunde kann eh keiner lesen
- void readmicros() { // Interrupt-Routine
- detachInterrupt(0); // Interrupt ausschalten damit er uns nicht beißt
- int avgmax;
- unsigned long us = micros(); // Microsekundenzähler auslesen
- if (last == 0) { // erster Messwert?
- last = us; // merken und nicht weiter bearbeiten
- } else {
- if ( us < last ) { // Zählerüberlauf
- dauer = 4294967295 - last + us; // erzeugt einen Fehler von 1µS - vernachlässigbar
- } else {
- dauer = us - last; // Differenz zum letzten Durchlauf berechnen
- }
- if (dauer > 5000) { // ignorieren wenn <= 5ms (Kontaktpreller)
- average = dauer + average * avgcnt++; // Wert in buffer und mit Faktor avgcnt glätten
- average /= avgcnt; // und zurückrechnen
- avgmax = 1000000 / dauer; // dynamische Größe des Integrationspuffers
- if (avgmax < AVG) avgmax = AVG; // Trägheit mindestens 1 Sekunde
- if (avgcnt >= avgmax) avgcnt--;
- last = us; // und wieder den letzten Wert merken
- }
- }
- attachInterrupt(0, readmicros, RISING ); // Interrupt wieder einschalten
- }
- unsigned long myround(unsigned long value) { // Gewichtete Rundung
- int rto;
- if (value > 3000) { // Rundungswert bestimmen
- rto = 100;
- } else if (value > 1500) {
- rto = 50;
- } else if (value > 500) {
- rto = 10;
- } else if (value > 100) {
- rto = 5;
- } else {
- return (value);
- }
- return (_myround(value, rto));
- }
- unsigned long _myround(unsigned long value, int roundto) {
- value += (roundto >> 1); // halben roundto Wert addieren
- value /= roundto; // integer division
- value *= roundto; // integer multiplikation
- return (value);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement