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
- //speed of motor is calculated as the amount of full cycles of the same measured time
- //length (duration_on/_off) can be fitted within a minute of time (60000ms),
- //2 signals per physical spin
- long speed_of_Motor = (60000000)/((duration_on + duration_off) *2);
- Serial.println(speed_of_Motor);
- //code for running the motor with PWM
- //setting the speed
- int speedset_rpm = 1550 //within a range between 0 and 3700 rpm to convert
- int converted_speedset_PWM = speedset_rpm/3700*255;
- //setting duty cycle for the PWM after conversion from speedset
- int motor_PWM_duty_cycle = 1; //value needs to be between 0 and 255
- //set by the user, calibrated
- analogWrite(MOTOR_PIN, motor_PWM_duty_cycle);
- //set tolerance
- 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
- digitalWrite(MOTOR_PIN,HIGH);
- }
- if (speed_of_Motor >= max_speed_set){
- //reset speed to the set by user
- digitalWrite(MOTOR_PIN,LOW);
- }
- delay(500);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement