Advertisement
Guest User

Untitled

a guest
Jul 17th, 2019
247
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 8.50 KB | None | 0 0
  1. #include <Adafruit_GFX.h>
  2. #include <Adafruit_PWMServoDriver.h>
  3. #include <Adafruit_SSD1306.h>
  4. #include <Conceptinetics.h>
  5. #include <Encoder.h>
  6. #include <SPI.h>
  7. #include <TMCStepper.h>
  8. #include <AccelStepper.h>
  9. #include <Wire.h>
  10. #include <TimerOne.h>
  11.  
  12. /* Relay start */
  13. const byte RELAY_PIN = 22;
  14. /* Relay end */
  15.  
  16. /* DMX shield start */
  17. const int CHANNELS_TOTAL = 5;
  18. const int MAX_START_CHANNEL = 512 - CHANNELS_TOTAL + 1;
  19. DMX_Slave dmxSlave(CHANNELS_TOTAL);
  20.  
  21. int startChannel = 1;
  22. int lastDisplayedStartChannel = 0;
  23.  
  24. int lastChannel1Value = -1;
  25. int lastChannel2Value = -1;
  26. int lastChannel3Value = -1;
  27. int lastChannel4Value = -1;
  28. int lastChannel5Value = -1;
  29. /* DMX shield end */
  30.  
  31. /* Display start */
  32. const int DISPLAY_WIDTH = 128;
  33. const int DISPLAY_HEIGHT = 32;
  34. const byte OLED_RESET = 4; // reset pin (-1 if sharing Arduino reset pin)
  35. Adafruit_SSD1306 display(DISPLAY_WIDTH, DISPLAY_HEIGHT, &Wire, OLED_RESET);
  36. const int REFRESH_EVERY = 40; // in millis
  37. long lastDisplayRefresh = 0;
  38. /* Display end */
  39.  
  40. /* Encoder start */
  41. const byte ENCODER_A_PIN = 18;
  42. const byte ENCODER_B_PIN = 19;
  43. const byte ENCODER_SW_PIN = 24;
  44. Encoder encoder(ENCODER_A_PIN, ENCODER_B_PIN);
  45. long lastEncoderPosition = 0;
  46. int lastSwPinValue = LOW;
  47. /* Encoder end */
  48.  
  49. /* Stepper start */
  50. const byte EN_PIN = 50; // Enable
  51. const byte SW_MOSI = 48; // Software Master Out Slave In (MOSI)
  52. const byte SW_SCK = 46; // Software Slave Clock (SCK)
  53. const byte CS_PIN = 44; // Chip select
  54. const byte SW_MISO = 42; // Software Master In Slave Out (MISO)
  55. const byte STEP_PIN = 40; // Step
  56. const byte DIR_PIN = 38; // Direction
  57.  
  58. const float R_SENSE = 0.11; // Match to your driver (SilentStepStick series use 0.11)
  59.  
  60. TMC2130Stepper driver = TMC2130Stepper(CS_PIN, R_SENSE, SW_MOSI, SW_MISO, SW_SCK); // Software SPI
  61. AccelStepper stepper = AccelStepper(stepper.DRIVER, STEP_PIN, DIR_PIN);
  62.  
  63. const int BASE_STEP_COUNT = 200;
  64. const int MICROSTEPS = 16;
  65. const int STEPS_PER_REV = BASE_STEP_COUNT * MICROSTEPS;
  66. /* Stepper end */
  67.  
  68. /* LEDs start */
  69. Adafruit_PWMServoDriver pwmDriver = Adafruit_PWMServoDriver();
  70. const byte PWM_LED1_PIN = 0;
  71. const byte PWM_LED2_PIN = 1;
  72. const byte PWM_LED3_PIN = 2;
  73. const byte PWM_LED4_PIN = 3;
  74.  
  75. int led1CurrentValue = 4095;
  76. int led2CurrentValue = 4095;
  77. int led3CurrentValue = 4095;
  78. int led4CurrentValue = 4095;
  79.  
  80. int led1GoalValue = 4095;
  81. int led2GoalValue = 4095;
  82. int led3GoalValue = 4095;
  83. int led4GoalValue = 4095;
  84. /* LEDs end */
  85.  
  86. /* Mapped values start */
  87. int brightnessMapping[256];
  88. float speedMapping[256];
  89. /* Mapped values end */
  90.  
  91. void setup() {
  92.   initializePins();
  93.   initializePwmDriver();
  94.   initializeDisplay();
  95.   initializeStepper();
  96.   showChannelSelector();
  97.   precalcValues();
  98.   enableDmxShield();
  99.  
  100.   Timer1.initialize(100);
  101.   Timer1.attachInterrupt(pollStepper);
  102. }
  103.  
  104. void pollStepper() {
  105.   stepper.run();
  106. }
  107.  
  108. void loop() {
  109.   /* LED #1 */
  110.   if (led1CurrentValue != led1GoalValue) {
  111.     led1CurrentValue += (led1GoalValue > led1CurrentValue) ? 1 : -1;
  112.     pwmDriver.setPWM(PWM_LED1_PIN, 0, led1CurrentValue);
  113.   }
  114.  
  115.   /* LED #2 */
  116.   if (led2CurrentValue != led2GoalValue) {
  117.     led2CurrentValue += (led2GoalValue > led2CurrentValue) ? 1 : -1;
  118.     pwmDriver.setPWM(PWM_LED2_PIN, 0, led2CurrentValue);
  119.   }
  120.  
  121.   /* LED #3 */
  122.   if (led3CurrentValue != led3GoalValue) {
  123.     led3CurrentValue += (led3GoalValue > led3CurrentValue) ? 1 : -1;
  124.     pwmDriver.setPWM(PWM_LED3_PIN, 0, led3CurrentValue);
  125.   }
  126.  
  127.   /* LED #4 */
  128.   if (led4CurrentValue != led4GoalValue) {
  129.     led4CurrentValue += (led4GoalValue > led4CurrentValue) ? 1 : -1;
  130.     pwmDriver.setPWM(PWM_LED4_PIN, 0, led4CurrentValue);
  131.   }
  132. }
  133.  
  134. void initializePins() {
  135.   pinMode(RELAY_PIN, OUTPUT);
  136.   digitalWrite(RELAY_PIN, LOW);
  137.  
  138.   pinMode(ENCODER_SW_PIN, INPUT);
  139.  
  140.   pinMode(CS_PIN, OUTPUT);
  141.   digitalWrite(CS_PIN, HIGH);
  142. }
  143.  
  144. void initializePwmDriver() {
  145.   pwmDriver.begin();
  146.   pwmDriver.setPWMFreq(1600);
  147.   pwmDriver.setPWM(PWM_LED1_PIN, 0, led1CurrentValue);
  148.   pwmDriver.setPWM(PWM_LED2_PIN, 0, led2CurrentValue);
  149.   pwmDriver.setPWM(PWM_LED3_PIN, 0, led3CurrentValue);
  150.   pwmDriver.setPWM(PWM_LED4_PIN, 0, led4CurrentValue);
  151. }
  152.  
  153. void initializeDisplay() {
  154.   display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
  155.   display.clearDisplay();
  156.   display.setTextSize(2);
  157.   display.setTextColor(WHITE);
  158.   display.cp437(true); // use full 256 char 'Code Page 437' font
  159.   display.display();
  160. }
  161.  
  162. void initializeStepper() {
  163.   driver.begin();
  164.   driver.rms_current(600);
  165.   driver.en_pwm_mode(1); // Enable extremely quiet stepping
  166.   driver.pwm_autoscale(1);
  167.   driver.microsteps(MICROSTEPS);
  168.  
  169.   stepper.setMaxSpeed(0);
  170.   stepper.setAcceleration(STEPS_PER_REV * 25);
  171.   stepper.setEnablePin(EN_PIN);
  172.   stepper.setPinsInverted(false, false, true);
  173.   stepper.enableOutputs();
  174. }
  175.  
  176. void showChannelSelector() {
  177.   lastEncoderPosition = encoder.read();
  178.   while (true) {
  179.     int currentSwPinValue = digitalRead(ENCODER_SW_PIN);
  180.     if (lastSwPinValue == HIGH && currentSwPinValue == LOW) {
  181.       displaySelectedChannels();
  182.       break;
  183.     } else {
  184.       lastSwPinValue = currentSwPinValue;
  185.     }
  186.  
  187.     long currentEncoderPosition = encoder.read();
  188.     long diff = currentEncoderPosition - lastEncoderPosition;
  189.     lastEncoderPosition = currentEncoderPosition - (diff % 4);
  190.  
  191.     startChannel += diff / 4;
  192.     startChannel = (startChannel < 1) ? 1 : startChannel;
  193.     startChannel = (startChannel > MAX_START_CHANNEL) ? MAX_START_CHANNEL : startChannel;
  194.  
  195.     bool displayRedrawn = displayStartChannel();
  196.     if (!displayRedrawn) {
  197.       delay(2);
  198.     }
  199.   }
  200. }
  201.  
  202. bool displayStartChannel() {
  203.   if (startChannel == lastDisplayedStartChannel) {
  204.     return false;
  205.   }
  206.  
  207.   long currentTime = millis();
  208.   if (lastDisplayRefresh == 0 || currentTime - lastDisplayRefresh >= REFRESH_EVERY) {
  209.     display.clearDisplay();
  210.     display.setCursor(1, 1);
  211.     display.println("Start ch.:");
  212.     display.println(startChannel);
  213.     display.display();
  214.  
  215.     lastDisplayRefresh = currentTime;
  216.     lastDisplayedStartChannel = startChannel;
  217.  
  218.     return true;
  219.   }
  220.   return false;
  221. }
  222.  
  223. void displaySelectedChannels() {
  224.   display.clearDisplay();
  225.   display.setCursor(4, 8);
  226.   display.print("ch.");
  227.   display.print(startChannel);
  228.   display.print("-");
  229.   display.println(startChannel + CHANNELS_TOTAL - 1);
  230.   display.display();
  231. }
  232.  
  233. void precalcValues() {
  234.   for (int i = 0; i < 256; i++) {
  235.     brightnessMapping[i] = mapBrightness(i);
  236.     speedMapping[i] = mapSpeed(i);
  237.   }
  238. }
  239.  
  240. int mapBrightness(int channelValue) {
  241.   if (channelValue == 0) {
  242.     return 4095;
  243.   }
  244.  
  245.   int mappedValue;
  246.   if (channelValue <= 20) {
  247.     mappedValue = map(channelValue, 0, 20, 4075, 4055);
  248.   } else if (channelValue <= 50) {
  249.     mappedValue = map(channelValue, 20, 50, 4055, 4000);
  250.   } else if (channelValue <= 200) {
  251.     mappedValue = map(channelValue, 50, 200, 4000, 1500);
  252.   } else {
  253.     mappedValue = map(channelValue, 200, 255, 1500, 0);
  254.   }
  255.   return mappedValue;
  256. }
  257.  
  258. float mapSpeed(int channelValue) {
  259.   float rpm = ((float)channelValue / 255 * 69) + 1;
  260.   return STEPS_PER_REV * rpm / 60;
  261. }
  262.  
  263. void enableDmxShield() {
  264.   digitalWrite(RELAY_PIN, HIGH);
  265.   dmxSlave.onReceiveComplete(onFrameReceiveComplete);
  266.   dmxSlave.enable();
  267.   dmxSlave.setStartAddress(startChannel);
  268. }
  269.  
  270. void onFrameReceiveComplete(void) {
  271.   int channel1Value = dmxSlave.getChannelValue(1);
  272.   int channel2Value = dmxSlave.getChannelValue(2);
  273.   int channel3Value = dmxSlave.getChannelValue(3);
  274.   int channel4Value = dmxSlave.getChannelValue(4);
  275.   int channel5Value = dmxSlave.getChannelValue(5);
  276.  
  277.   /* Channel #1, stepper */
  278.   if (channel1Value != lastChannel1Value) {
  279.     lastChannel1Value = channel1Value;
  280.     stepper.setMaxSpeed(speedMapping[channel1Value]);
  281.  
  282.     // flawed solution, need a better periodic workaround
  283.     if (abs(stepper.distanceToGo()) < STEPS_PER_REV) {
  284.       stepper.move(1000000);
  285.     }
  286.   }
  287.  
  288.   /* Channel #2, LED #1 */
  289.   if (channel2Value != lastChannel2Value) {
  290.     lastChannel2Value = channel2Value;
  291.     led1GoalValue = brightnessMapping[channel2Value];
  292.   }
  293.  
  294.   /* Channel #3, LED #2 */
  295.   if (channel3Value != lastChannel3Value) {
  296.     lastChannel3Value = channel3Value;
  297.     led2GoalValue = brightnessMapping[channel3Value];
  298.   }
  299.  
  300.   /* Channel #4, LED #3 */
  301.   if (channel4Value != lastChannel4Value) {
  302.     lastChannel4Value = channel4Value;
  303.     led3GoalValue = brightnessMapping[channel4Value];
  304.   }
  305.  
  306.   /* Channel #5, LED #4 */
  307.   if (channel5Value != lastChannel5Value) {
  308.     lastChannel5Value = channel5Value;
  309.     led4GoalValue = brightnessMapping[channel5Value];
  310.   }
  311. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement