Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <SPI.h>
- SPISettings settingsPot(10000000, MSBFIRST, SPI_MODE0);
- const byte speedControllerSSpin = 10; // Chip Select pin of the digital potentiometer
- const byte spindleRelayPin = 7; // Pin to turn on/off the spindle with a relay
- const int MIN_SPINDLE_RPM = 7930;
- const int MAX_SPINDLE_RPM = 36330;
- 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
- int spindleSpeed = -1;
- uint16_t spindleRPM = 0;
- void setup()
- {
- pinMode(speedControllerSSpin, OUTPUT);
- pinMode(spindleRelayPin, OUTPUT);
- digitalWrite(speedControllerSSpin, HIGH);
- digitalWrite(spindleRelayPin, LOW);
- SPI.begin(); // wake up the SPI bus.
- spindleSpeed = -1;
- sendSpindleSpeed();
- }
- void loop(){
- setSpindleRPM(10000); //Sets the spindle to 10k RPM
- }
- void setSpindleRPM(uint16_t rpm) {
- if (rpm < MIN_SPINDLE_RPM) {
- rpm = MIN_SPINDLE_RPM;
- }
- if ( rpm > MAX_SPINDLE_RPM ) {
- rpm = MAX_SPINDLE_RPM;
- }
- if (spindleRPM == rpm) {
- return;
- }
- spindleSpeed = floor((float)(rpm - MIN_SPINDLE_RPM) / spindleK);
- spindleRPM = rpm;
- sendSpindleSpeed();
- }
- void sendSpindleSpeed() {
- if (spindleSpeed >= 0) {
- noInterrupts();
- SPI.beginTransaction(settingsPot);
- digitalWrite(speedControllerSSpin, LOW);
- SPI.transfer(0); // send command byte
- SPI.transfer(spindleSpeed); // send value (0~255)
- digitalWrite(speedControllerSSpin, HIGH);
- digitalWrite(spindleRelayPin, HIGH);
- SPI.endTransaction;
- interrupts();
- }
- else {
- digitalWrite(spindleRelayPin, LOW);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement