Skip to content
Snippets Groups Projects
Select Git revision
  • a2feb1c37df2b129c57005f8340c23cd2138c579
  • main default protected
2 results

precipitationctl.ino

Blame
  • precipitationctl.ino 2.65 KiB
    #include "data.h"
    #include <Ticker.h>
    #include <ESP32Servo.h> 
    #define SERVO_MIN 0
    #define SERVO_MAX 180
    #define ADC_MAX 4096
    #define INTERVAL 5
    // 28.44
    
    int servoPin = 23;
    int pos = SERVO_MIN;
    
    Servo servo;
    
    int data_index = 23;
    double ptarget = 0.0f;
    double target = 0.0f;
    double current = 0.0f;
    double blend = 0.0f;
    
    Ticker ticker;
    
    double lerp(double a, double b, double t) {
      t = max(0.0, min(1.0, t));
      return a + t * (b - a);
    }
    
    void setServoNormalized(double pos) {
      pos = min(1.0, max(0.0, pos));
      // 
      pos = sqrt(pos);
      int span = abs(SERVO_MAX - SERVO_MIN);
      int servo_pos = SERVO_MIN + span * pos;
      // Serial.print(" --> ");
      // Serial.print(pos);
      // Serial.print(" --> ");
      // Serial.println(servo_pos);
      Serial.print(SERVO_MIN); // To freeze the lower limit
      Serial.print(" ");
      Serial.print(SERVO_MAX); // To freeze the upper limit
      Serial.print(" ");
      Serial.println(servo_pos);
      servo.write(servo_pos);
    }
    
    void setNewTarget() {
      ptarget = target;
      target = data[data_index];
      current = target;
      // Serial.print("New Target is [");
      // Serial.print(data_index);
      // Serial.print("]: ");
      // Serial.print(target);
    
      data_index++;
    
      // Reset index when longer than the data (loop over)
      if (data_index >= DATA_LENGTH) {
        data_index = 0;
        Serial.println("Die daten sind durch, wir fangen wieder vorne an...");
      }
    }
    
    float testval = 0.0f;
    float increment = 0.01f;
    
    void setup() {
      Serial.begin(115200);
    
      // Allow allocation of all timers
    	ESP32PWM::allocateTimer(0);
    	ESP32PWM::allocateTimer(1);
    	ESP32PWM::allocateTimer(2);
    	ESP32PWM::allocateTimer(3);
      
      servo.setPeriodHertz(50);// Standard 50hz servo
      servo.attach(servoPin, 1000, 2000);   // attaches the servo on pin 18 to the servo object
                                             // using SG90 servo min/max of 500us and 2400us
                                             // for MG995 large servo, use 1000us and 2000us,
                                             // which are the defaults, so this line could be
                                             // "myservo.attach(servoPin);"
    
      // Set new Target every 28.44 seconds (leads to 3 days)
      ticker.attach(INTERVAL, setNewTarget);  // Interval in seconds
    }
    
    void loop() {
      unsigned long currentMillis = millis();
      blend = fmod(float(currentMillis), (INTERVAL * 1000.0f)) / (INTERVAL * 1000.0f);
      current = lerp(ptarget, target, blend);
      // Serial.print(ptarget);
      // Serial.print(" (");
      // Serial.print(int(blend*100));
      // Serial.print("%) ");
      // Serial.print(target);
      // Serial.print(" (");
      // Serial.print(int((1.0-blend)*100));
      // Serial.print("%) = ");
      // Serial.println(current);
    
      setServoNormalized(current);
      delay(2);
    }