Advertisement
Guest User

Untitled

a guest
Jun 17th, 2019
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.47 KB | None | 0 0
  1.  
  2. #include <NewPing.h>
  3. #include <PID_v1.h>
  4.  
  5. #define TRIGGER_PIN 10 // Arduino pin tied to trigger pin on the ultrasonic sensor.
  6. #define ECHO_PIN 9 // Arduino pin tied to echo pin on the ultrasonic sensor.
  7. #define MAX_DISTANCE 50 // Maximum distance we want to ping for (in centimeters). Maximum sensor distance is rated at 50cm.
  8.  
  9. int fan1 = 2; // IN1
  10. int fan2 = 4; // IN2
  11. int pwm = 0; // N1 used as fan speed control
  12.  
  13. // Physical constants for the apparatus -----------------------------
  14. float BALL_DIAMETER = 4.0; // in cm
  15. float TUBE_LENGTH = 33.0; // in cm
  16. float SENSOR_ZERO = (TUBE_LENGTH - BALL_DIAMETER);
  17.  
  18. //PID parameters.
  19. double kp=2; //proportional parameter
  20. double ki=5; //integral parameter
  21. double kd=1; //derivative parameter
  22.  
  23. //SetPoint
  24. double SetP = 15;
  25.  
  26.  
  27.  
  28. double heightInt, heightExt, heightDiff, command;
  29.  
  30.  
  31. PID myPID(&heightInt, &command, &SetP,kp,ki,kd, DIRECT);
  32.  
  33.  
  34. void setup(void)
  35. {
  36. Serial.begin(57600);
  37. // PWM output
  38. pinMode(fan1,OUTPUT);
  39. pinMode(fan2,OUTPUT);
  40. pinMode(pwm,OUTPUT);
  41.  
  42.  
  43.  
  44.  
  45. //turn the PID on
  46. myPID.SetOutputLimits(0, 255);
  47. myPID.SetMode(AUTOMATIC);
  48. myPID.SetTunings(Kp, Ki, Kd);
  49. }
  50.  
  51. void loop()
  52. {
  53. digitalWrite(fan1, HIGH);
  54. digitalWrite(fan2, LOW);
  55. unsigned int uS = sonar.ping(); // Send ping, get ping time in microseconds (uS).
  56. unsigned int distance = uS / US_ROUNDTRIP_CM ;
  57. heightInt = distance
  58.  
  59. myPID.Compute();
  60. analogWrite(pwm, command);
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement