Advertisement
Guest User

Untitled

a guest
Jun 24th, 2019
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.37 KB | None | 0 0
  1. #include "TimerOne.h"
  2. #include <PID_v1.h>
  3.  
  4. int counter=0;
  5.  
  6. const int IN1 = 11;
  7. const int IN2 = 10;
  8. volatile float pot=0;
  9. const int POT = 0;
  10. const int ENA = 6;
  11.  
  12. double Setpoint, Input, Output;
  13. double Kp=2, Ki=5, Kd=1;
  14.  
  15. PID myPID(&Input, &Output, &Setpoint, Kp, Ki, Kd, DIRECT);
  16.  
  17.  
  18. void docount() // counts from the speed sensor
  19. {
  20. counter++; // increase +1 the counter value
  21. }
  22.  
  23. void timerIsr()
  24. {
  25. Timer1.detachInterrupt(); //stop the timer
  26. Serial.print("Motor Speed: ");
  27. int rotation = (counter / 30); // divide by number of holes in Disc
  28. Serial.print(rotation,DEC);
  29. Serial.println(" Rotation per second");
  30. counter=0; // reset counter to zero
  31. Timer1.attachInterrupt( timerIsr ); //enable the timer
  32. Serial.print("pot = ");
  33. Serial.print(pot);
  34. Serial.print("n");
  35.  
  36. //return rotation;
  37. }
  38.  
  39. void setup()
  40. {
  41. Serial.begin(9600);
  42.  
  43. pinMode(IN1, OUTPUT);
  44. pinMode(IN2, OUTPUT);
  45.  
  46. Timer1.initialize(1000000); // set timer for 1sec
  47. attachInterrupt(0, docount, FALLING); // increase counter when speed sensor pin goes High
  48. Timer1.attachInterrupt( timerIsr ); // enable the timer
  49.  
  50.  
  51. myPID.SetMode(AUTOMATIC);
  52. }
  53.  
  54. void loop()
  55. {
  56. //Input = timerIsr();
  57. pot=analogRead(POT)/4.01569;
  58. //Setpoint = sp;
  59.  
  60. //Serial.print(Input);
  61.  
  62. analogWrite(ENA, pot);
  63. digitalWrite(10, HIGH); // set rotation of motor to Clockwise
  64. digitalWrite(11, LOW);
  65.  
  66. delay (250);
  67. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement