Advertisement
Guest User

Untitled

a guest
Dec 9th, 2019
184
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.58 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. float duration_on = duration_on + pulseIn(PH_INTERRUPTER,HIGH);//signal caught-second reading to complete full round
  28. float duration_off = duration_off + pulseIn(PH_INTERRUPTER,LOW);//no light, complete round
  29. //speed of motor is calculated as the amount of full cycles of the same measured time
  30. //length of 2 consecutive cycles (duration_on/_off) can be fitted within a minute of time (60000000ms),
  31.  
  32. long speed_of_Motor = (60000000)/(duration_on + duration_off);
  33. Serial.println(speed_of_Motor);
  34.  
  35. //code for running the motor with PWM
  36. //setting the speed
  37. int speedset_rpm = 1200 //within a range between 0 and 1932 rpm to convert but obtainable range is 500 to 1500
  38. int converted_speedset_PWM = speedset_rpm/1932*255;
  39. //setting duty cycle for the PWM after conversion from speedset
  40. int motor_PWM_duty_cycle = converted_speedset_PWM; //value needs to be between 0 and 255
  41. //set by the user, calibrated
  42. analogWrite(MOTOR_PIN, motor_PWM_duty_cycle);
  43. //set tolerance in RPM
  44. int tolereance = 20;
  45. int max_speed_set = speedset + tolerance;
  46. int min_speed_set = speedset - tolerance;
  47. if (speed_of_Motor <= min_speed_set){
  48. //reset speed to the set by user
  49. analogWrite(MOTOR_PIN,motor_PWM_duty_cycle);
  50. }
  51. if (speed_of_Motor >= max_speed_set){
  52. //reset speed to the set by user
  53. analogaWrite(MOTOR_PIN,motor_PWM_duty_cycle);
  54. }
  55. delay(500);
  56. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement