Advertisement
Guest User

Untitled

a guest
Jul 2nd, 2024
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.13 KB | None | 0 0
  1. #include <CapacitiveSensor.h>
  2.  
  3. //Arduino Pin Assignments
  4. const int touchSendPin = 14;
  5. const int touchReceivePin = 15;
  6. const int motorDown   = 18;   //H-Bridge control to make the motor go down
  7. const int motorUp     = 19;   //H-Bridge control to make the motor go up
  8.  
  9. //Inputs
  10. const int faderWiper  = 17;   //Position of fader relative to GND
  11. const int controlPot  = 16;   //Potentiometer to set position of fader
  12.  
  13. CapacitiveSensor touchSensor(touchSendPin, touchReceivePin);
  14.  
  15. volatile bool touched  = false; //Is the fader currently being touched?
  16.  
  17. long previousTime = 0;
  18. float ePrevious = 0;
  19. float eIntegral = 0;
  20.  
  21. int freq = 31372;
  22. uint16_t target = 0;
  23.  
  24. uint16_t pot_max_value;
  25. uint16_t pot_min_value;
  26.  
  27. uint16_t position;
  28.  
  29. unsigned long previousMillis = 0;   // storing last millis
  30. const long interval = 5000;  
  31.  
  32. void setup()
  33. {
  34.   Serial.begin(115200);
  35.   pinMode (motorUp, OUTPUT);
  36.   pinMode (motorDown, OUTPUT);
  37.   analogWriteFrequency(motorUp, freq);
  38.   analogWriteFrequency(motorDown, freq);
  39.   analogWrite(motorUp, 0);
  40.   analogWrite(motorDown, 0);
  41.   calibration();
  42. }
  43.  
  44. void loop()                    
  45. {
  46.  
  47.   unsigned long currentMillis = millis();
  48.   if (currentMillis - previousMillis >= interval) {
  49.     // save current time
  50.     previousMillis = currentMillis;
  51.  
  52.     // generate target value between 0 and 1023
  53.     target = random(0, 1023);
  54.   }
  55.  
  56.   checkTouch();
  57.   position = analogRead(faderWiper);
  58.  
  59.  
  60.     if (!touched) {
  61.   float kp = 6;
  62.   float ki = 2;
  63.     float kd = 0.08;
  64.   float u = pidController(target, kp, kd, ki);
  65.     moveMotor(u);
  66.   } else {
  67.     stopMotor();
  68.   }
  69.   Serial.print("Target_Position:");
  70.   Serial.print(target);
  71.   Serial.print(", ");
  72.     Serial.print("Fader_Position:");
  73.     Serial.print(position);
  74.     Serial.print(", ");
  75.     Serial.print("Max_Value:");
  76.     Serial.print(pot_max_value);
  77.     Serial.print(", ");
  78.     Serial.print("Min_Value:");
  79.     Serial.println(pot_min_value);
  80.  
  81.     delay(1);
  82. }
  83.  
  84. float pidController(int target, float kp, float kd, float ki)
  85. {
  86.   long currentTime = micros();
  87.   float deltaT = ((float)(currentTime - previousTime)) / 1.0e6;
  88.  
  89.   int e = position - target;
  90.   float eDerivative = (e - ePrevious) / deltaT;
  91.   eIntegral = eIntegral + e * deltaT;
  92.  
  93.   float u = (kp * e) + (kd * eDerivative) + (ki * eIntegral);
  94.  
  95.   previousTime = currentTime;
  96.   ePrevious = e;
  97.  
  98.   return u;
  99. }
  100.  
  101. void moveMotor(float u)
  102. {
  103.   float speed = fabs(u);
  104.   if (speed > 255) {
  105.     speed = 255;
  106.   }
  107.  
  108.   if (u < 0) {
  109.     analogWrite(motorUp, speed);
  110.     analogWrite(motorDown, LOW);
  111.   } else if (u > 0) {
  112.     analogWrite(motorDown, speed);
  113.     analogWrite(motorUp, LOW);
  114.   } else {
  115.     stopMotor();
  116.   }
  117. }
  118.  
  119. void stopMotor()
  120. {
  121.   analogWrite(motorDown, HIGH);
  122.     analogWrite(motorUp, HIGH);
  123. }
  124.  
  125. void checkTouch()
  126. {
  127.   touched = touchSensor.capacitiveSensor(5) > 9000;
  128. }
  129.  
  130. void calibration()
  131. {
  132.   stopMotor();
  133.     analogWrite(motorUp, 255);
  134.   analogWrite(motorDown, LOW);
  135.     delay(1000);
  136.     pot_max_value = analogRead(faderWiper);
  137.  
  138.     analogWrite(motorUp, LOW);
  139.   analogWrite(motorDown, 255);
  140.   delay(1000);
  141.     pot_min_value = analogRead(faderWiper);
  142.  
  143.     stopMotor();
  144. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement