Advertisement
Guest User

Makita speed control

a guest
Feb 15th, 2018
555
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.62 KB | None | 0 0
  1. #include <SPI.h>
  2.  
  3. SPISettings settingsPot(10000000, MSBFIRST, SPI_MODE0);
  4.  
  5. const byte speedControllerSSpin = 10; // Chip Select pin of the digital potentiometer
  6. const byte spindleRelayPin = 7; // Pin to turn on/off the spindle with a relay
  7.  
  8. const int MIN_SPINDLE_RPM = 7930;
  9. const int MAX_SPINDLE_RPM = 36330;
  10. const float spindleK = (float)(MAX_SPINDLE_RPM - MIN_SPINDLE_RPM) / 256.0; // Slope of the linear function that converts RPM to 0-255 for the digital pot
  11.  
  12. int spindleSpeed = -1;
  13. uint16_t spindleRPM = 0;
  14.  
  15. void setup()
  16. {
  17.   pinMode(speedControllerSSpin, OUTPUT);
  18.   pinMode(spindleRelayPin, OUTPUT);
  19.   digitalWrite(speedControllerSSpin, HIGH);
  20.   digitalWrite(spindleRelayPin, LOW);
  21.   SPI.begin(); // wake up the SPI bus.
  22.  
  23.   spindleSpeed = -1;
  24.   sendSpindleSpeed();
  25. }
  26.  
  27. void loop(){
  28.     setSpindleRPM(10000); //Sets the spindle to 10k RPM
  29. }
  30.  
  31. void setSpindleRPM(uint16_t rpm) {
  32.   if (rpm < MIN_SPINDLE_RPM) {
  33.     rpm = MIN_SPINDLE_RPM;
  34.   }
  35.   if ( rpm > MAX_SPINDLE_RPM ) {
  36.     rpm = MAX_SPINDLE_RPM;
  37.   }
  38.   if (spindleRPM == rpm) {
  39.     return;
  40.   }
  41.   spindleSpeed = floor((float)(rpm - MIN_SPINDLE_RPM) / spindleK);
  42.   spindleRPM = rpm;
  43.   sendSpindleSpeed();
  44. }
  45.  
  46. void sendSpindleSpeed() {
  47.   if (spindleSpeed >= 0) {
  48.     noInterrupts();
  49.     SPI.beginTransaction(settingsPot);
  50.     digitalWrite(speedControllerSSpin, LOW);
  51.     SPI.transfer(0); // send command byte
  52.     SPI.transfer(spindleSpeed); // send value (0~255)
  53.     digitalWrite(speedControllerSSpin, HIGH);
  54.     digitalWrite(spindleRelayPin, HIGH);
  55.     SPI.endTransaction;
  56.     interrupts();
  57.   }
  58.   else {
  59.     digitalWrite(spindleRelayPin, LOW);
  60.   }
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement