Advertisement
Guest User

Untitled

a guest
Dec 8th, 2019
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.28 KB | None | 0 0
  1. //need to define digitalRead() enabled pin for square wave length reading from the photo-interrupter
  2. //need to define pin for gate input to the transistor that enables the motor;
  3. //PWM pin drives the transistor base
  4.  
  5. #define PH_INTERRUPTER 33 //pin to enable the reading from the photo-interrupter
  6. #define MOTOR_PIN 13
  7.  
  8.  
  9. void setup() {
  10. //PWM pin that controls the speed of the motor via the transistor logic gate
  11. //digitalRead pin that reads the speed of the motor via square wave signal
  12. //length as a part of full cycle of light
  13. pinMode(MOTOR_PIN, OUTPUT);
  14. pinMode(PH_INTERRUPTER, INPUT);
  15. Serial.begin(9600);
  16. Serial.println("start");
  17. }
  18.  
  19. void loop() {
  20. //code for outputting sensor value
  21. //delay between each counting procedure cycle
  22. delay(1000);
  23. //the pulseIn function measures the time length of a signal of certain type - HIGH or LOW
  24. //and returns its length in ms, or 0 if no such was detected.
  25. float duration_on = pulseIn(PH_INTERRUPTER,HIGH);//signal caught
  26. float duration_off = pulseIn(PH_INTERRUPTER,LOW);//no light
  27. //speed of motor is calculated as the amount of full cycles of the same measured time
  28. //length (duration_on/_off) can be fitted within a minute of time (60000ms),
  29. //2 signals per physical spin
  30.  
  31. long speed_of_Motor = (60000000)/((duration_on + duration_off) *2);
  32. Serial.println(speed_of_Motor);
  33.  
  34. //code for running the motor with PWM
  35. //setting the speed
  36. int speedset_rpm = 1550 //within a range between 0 and 3700 rpm to convert
  37. int converted_speedset_PWM = speedset_rpm/3700*255;
  38. //setting duty cycle for the PWM after conversion from speedset
  39. int motor_PWM_duty_cycle = 1; //value needs to be between 0 and 255
  40. //set by the user, calibrated
  41. analogWrite(MOTOR_PIN, motor_PWM_duty_cycle);
  42. //set tolerance
  43. int tolereance = 20;
  44. int max_speed_set = speedset + tolerance;
  45. int min_speed_set = speedset - tolerance;
  46. if (speed_of_Motor <= min_speed_set){
  47. //reset speed to the set by user
  48. digitalWrite(MOTOR_PIN,HIGH);
  49. }
  50. if (speed_of_Motor >= max_speed_set){
  51. //reset speed to the set by user
  52. digitalWrite(MOTOR_PIN,LOW);
  53. }
  54. delay(500);
  55. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement