Advertisement
learnelectronics

MOSFET Speed Control with Tach

Oct 25th, 2017
1,561
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.79 KB | None | 0 0
  1. /*
  2.  * MOSFET Speed Control with Tach
  3.  *
  4.  * learnelectronics
  5.  * 23 OCT 2013
  6.  *
  7.  * www.youtube.com/c/learnelectronics
  8.  */
  9.  
  10. #include <Wire.h>                                                     //Wire library for I2c display
  11. #include <Adafruit_SSD1306.h>                                         //Adafruit OLED display library
  12.  
  13. #define OLED_RESET 4                                                  //does nothing, but needed for library
  14.  
  15. Adafruit_SSD1306 display(OLED_RESET);                                 //create instance of SSD1306 library called display
  16.  
  17.  
  18. int val;                                                              //calibration value
  19. long last=0;                                                          //used in timing - holds thelast milis count
  20. int stat=LOW;                                                         //used in calibration
  21. int stat2;                                                            //used in calibration
  22. int counter=0;                                                        //used to count light/dark iterations
  23.  
  24. int sens=75;                                                          //calibration value
  25. int setRPM = 0;                                                       // initial speed value set at 0              
  26.              
  27. int slots=20;                                                         //# slots in disc
  28.  
  29. int milisecs=500;                                                     //time of sample
  30.  
  31.  
  32. void setup()
  33. {
  34.   display.begin(SSD1306_SWITCHCAPVCC, 0x3C);                          //begin oled
  35.   display.display();                                                  //show bufer
  36.   delay(10);                                                          //wait 10 mS
  37.   display.clearDisplay();                                             //clear display and buffer
  38.  
  39.   Serial.begin(9600);                                                 //serial comms for debuging
  40.   pinMode(5,OUTPUT);                                                  // pin to switch MOSFET on/off
  41.   pinMode(A2,INPUT);                                                  // pin to read from potentiometer
  42.   pinMode(13,OUTPUT);                                                 //digital pin 13 set for output
  43. }
  44.  
  45. void loop()
  46. {
  47.  
  48.   int j = (analogRead(A2));                                           //read the value from pot 0-1023
  49.   setRPM = map(j,0,1023,0,255);                                       //map the 0-1023 from the pot to PWM friendly 0-255
  50.   analogWrite(5,setRPM);                                              //set the motor speed by switching the MOSFET on/off
  51.  
  52.   val=analogRead(0);                                                  //light or dark?
  53.   if(val<sens)                                                        //within calibartion value?
  54.     stat=LOW;                                                         //set var to LOW
  55.    else                                                               //if not
  56.     stat=HIGH;                                                        //set var HIGH
  57.    digitalWrite(13,stat);                                             //LED on or off
  58.                          
  59.  
  60.    if(stat2!=stat){                                                   //if there has been a change
  61.                      
  62.      counter++;                                                       //increment counter
  63.      stat2=stat;                                                      //remember last value
  64.    }
  65.    if(millis()-last>=milisecs){                                       //has time passed?
  66.      double rps=((double)counter/slots)/2.0*1000.0/milisecs;          //do maths
  67.      double rpm=((double)counter/slots)/2.0*60000.0/(milisecs);       //do more maths
  68.      display.clearDisplay();                                          //clear display & buffer
  69.      display.setTextSize(2);                                          //prepare to show stuff
  70.      display.setTextColor(WHITE);
  71.      display.setCursor(0,0);
  72.      //display.print((counter/2.0));
  73.      //display.println("  RPS ");
  74.      //display.print(rps);
  75.      display.print("RPM ");                                           //print out the units we are using to buffer
  76.      display.println(rpm);                                            //print out the value returned by the maths
  77.      display.display();                                               //show me the buffer!
  78.      
  79.      counter=0;                                                       //reset counter
  80.      last=millis();                                                   //remember this moment
  81.  
  82.                                                                       //the time is gone, the song is over
  83.                                                                       //I thought I'd something more to say...
  84.    }
  85. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement