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);
- 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);
- #define PotiPin 0 //PIN Potti
- #define HallPin 2 //PIN Hallsensor
- #define OctoPin 9 //PIN Octokoppler
- //#define ledPin 13 //PIN Led-Octo
- #define pwmPin 9
- /*
- int PotiPin = 0; //PIN Potti
- const int HallPin = 2;
- int OctoPin =9;
- int ledPin =13;
- */
- int Prozent;
- int Umax = 1500.; // Max Drehzahl des Motors
- int upmSoll;
- float Force;
- unsigned long pwm = 0;
- char buf[17];
- unsigned long upmIst = 0;
- 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;
- void setup() {
- Serial.begin(9600);
- pinMode(PotiPin, INPUT);
- pinMode(HallPin, INPUT);
- pinMode(OctoPin, OUTPUT);
- attachInterrupt(PotiPin, readmicros, RISING);
- LoadCell.begin();
- LoadCell.start(2000);
- LoadCell.setCalFactor(106763.15789474/9.81);
- lcd1.begin(16,2);
- lcd1.backlight();
- lcd2.begin(16, 2); // Display hat 2 Zeilen a 16 Zeichen
- lcd2.backlight();
- }
- void loop() {
- Prozent = analogRead(PotiPin)*(4.9296/49.89255); //*(5./4.914)
- Serial.print("Prozent:");
- Serial.print(Prozent);
- // sprintf(Prozent, "%3lu");
- upmSoll = ((Umax*(analogRead(PotiPin)*(4.9296/49.89255)))/100);
- Serial.print("_UpmSoll:");
- Serial.print(upmSoll);
- UpmIst();
- if (upmIst >= upmSoll){
- Serial.print("_ERROR32:BREMSEEEEE_");
- pwm = Umax -(Umax - upmIst);
- pwm = map(pwm,0,Umax,0,255);
- analogWrite(pwmPin, pwm);
- digitalWrite(pwmPin, HIGH);
- }else{
- Serial.print("_UpmSoll<UmpIst_");
- digitalWrite(OctoPin, LOW);
- digitalWrite(pwmPin, LOW);
- }
- Lcd1();
- Lcd2();
- LoadCell.update();
- Force = LoadCell.getData();
- }
- void UpmIst (void){
- if (dauer != 0) {
- upmIst = myround(60000000 / average); // Drehzahl ausrechnen und runden
- } else {
- upmIst = 0; // keine Messung? -> Stillstand
- avgcnt = 0;
- }
- sprintf(buf, "%4lu", upmIst);
- Serial.println("_UpmIst: " + String(buf));
- dauer >>= 10; // Flag für Stillstand ( : 1024 )
- delay(500);
- }
- // mehr als zwei Werte pro Sekunde kann eh keiner lesen
- void readmicros() { // Interrupt-Routine
- detachInterrupt(PotiPin); // 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(PotiPin, 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);
- }
- void Lcd1(void){ //Ausgabe-Komplett am LCD1
- lcd1.setCursor(0, 0);
- lcd1.print("Force[Nm]:");
- lcd1.setCursor(10, 0);
- lcd1.print(Force, 3); //gewicht ausgabe
- lcd1.setCursor(0, 1);
- lcd1.print("Drehzahl ");
- lcd1.print(Prozent); //Prozent ausgabe
- lcd1.print("%");
- }
- void Lcd2(void){ //Ausgabe-Komplett am LCD2
- lcd2.clear(); // Soll es BLINKEN ODER NICHT????
- lcd2.setCursor(0, 00);
- lcd2.print("SOLL IST MAX U/M");
- lcd2.setCursor(0, 1);
- lcd2.print(upmSoll); //Soll-Umdrehung ausgeben
- lcd2.setCursor(6, 1);
- lcd2.print(upmIst); //Ist-Umdrehung ausgeben
- lcd2.setCursor(12, 1);
- lcd2.print(Umax); //Max UPM Schwellwert zum schalten der Bremse
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement