Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //need to define digitalRead() enabled pin for square wave length reading from the photo-interrupter
- //need to define pin for gate input to the transistor that enables the motor;
- //PWM pin drives the transistor base
- #define PH_INTERRUPTER 33 //pin to enable the reading from the photo-interrupter
- #define MOTOR_PIN 13
- void setup() {
- //PWM pin that controls the speed of the motor via the transistor logic gate
- //digitalRead pin that reads the speed of the motor via square wave signal
- //length as a part of full cycle of light
- pinMode(MOTOR_PIN, OUTPUT);
- pinMode(PH_INTERRUPTER, INPUT);
- Serial.begin(9600);
- Serial.println("start");
- }
- void loop() {
- //code for outputting sensor value
- //delay between each counting procedure cycle
- delay(1000);
- //the pulseIn function measures the time length of a signal of certain type - HIGH or LOW
- //and returns its length in ms, or 0 if no such was detected.
- float duration_on = pulseIn(PH_INTERRUPTER,HIGH);//signal caught
- float duration_off = pulseIn(PH_INTERRUPTER,LOW);//no light
- float duration_on = duration_on + pulseIn(PH_INTERRUPTER,HIGH);//signal caught-second reading to complete full round
- float duration_off = duration_off + pulseIn(PH_INTERRUPTER,LOW);//no light, complete round
- //speed of motor is calculated as the amount of full cycles of the same measured time
- //length of 2 consecutive cycles (duration_on/_off) can be fitted within a minute of time (60000000ms),
- long speed_of_Motor = (60000000)/(duration_on + duration_off);
- Serial.println(speed_of_Motor);
- //code for running the motor with PWM
- //setting the speed
- int speedset_rpm = 1200 //within a range between 0 and 1932 rpm to convert but obtainable range is 500 to 1500
- int converted_speedset_PWM = speedset_rpm/1932*255;
- //setting duty cycle for the PWM after conversion from speedset
- int motor_PWM_duty_cycle = converted_speedset_PWM; //value needs to be between 0 and 255
- //set by the user, calibrated
- analogWrite(MOTOR_PIN, motor_PWM_duty_cycle);
- //set tolerance in RPM
- int tolereance = 20;
- int max_speed_set = speedset + tolerance;
- int min_speed_set = speedset - tolerance;
- if (speed_of_Motor <= min_speed_set){
- //reset speed to the set by user
- analogWrite(MOTOR_PIN,motor_PWM_duty_cycle);
- }
- if (speed_of_Motor >= max_speed_set){
- //reset speed to the set by user
- analogaWrite(MOTOR_PIN,motor_PWM_duty_cycle);
- }
- delay(500);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement