Advertisement
GeekGarage

Culdron (Motor & RGB LED)

Oct 20th, 2019
523
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Motor and RGB-LED control by GeekGarage.dk
  2. // Published on October 20 2019
  3.  
  4. //////////////////////////////////////////
  5. // YOUR CONFIGURATION - EDIT BELOW THIS //
  6. //////////////////////////////////////////
  7.  
  8. //Defines PIN Reference (To save memory instead of int) - Change PIN NUMBER according to your wirering!
  9. #define MOTOR_PIN_1 5
  10. #define MOTOR_PIN_2 6
  11. #define MOTOR_PIN_3 7
  12. #define MOTOR_PIN_4 8
  13.  
  14. #define PIN_RED 9
  15. #define PIN_GREEN 10
  16. #define PIN_BLUE 11
  17.  
  18. //Motor
  19. int Motor_Direction = 1; // 1 = clockwise, -1 = counter clockwise - Initial direction or if change is disabled
  20. bool Motor_Change_Direction = true; //Should motor change direction based on the min and max timers?
  21. unsigned long Min_Motor_Time_Direction = 5000; //How long should the motor minimum move in one direction in milliseconds
  22. unsigned long Max_Motor_Time_Direction = 30000; //How long should the motor maximum move in one direction in milliseconds
  23.  
  24. unsigned short Motor_Speed = 1; //Initial motor speed or if speed change is disabled this is the speed used
  25. bool Motor_Change_Speed = true; //Should the motor change speed?
  26. unsigned short Min_Motor_Speed = 1; //The fastest speed the motor can run.
  27. unsigned short Max_Motor_Speed = 5; //The slowest speed the motor can run
  28. unsigned long Min_Motor_Speed_Change_Time = 5000; //The minimum time before motor change speed in milliseconds
  29. unsigned long Max_Motor_Speed_Change_Time = 30000; //The maximum time before motor change speed in milliseconds
  30.  
  31. //LED
  32.  
  33. unsigned short RGB_Value[3] = {44,104,168}; //These are the Red, Green and Blue values, in that order, and value from 0-255.
  34.  
  35. bool Should_Flicker = true;
  36. unsigned long Min_Time_Between_Flicker = 6000; //Minimum time between flickers in milliseconds
  37. unsigned long Max_Time_Between_Flicker = 30000; //Maximum time between flickers in milliseconds
  38. unsigned long Min_Flicker_Time = 3000; //Minimum flicker time in milliseconds
  39. unsigned long Max_Flicker_Time = 7000; //Maximum flicker time in milliseconds
  40.  
  41. //BREATH DOES NOTHING FOR NOW!!!
  42. bool Should_Breath = true;
  43. unsigned long Min_Time_Between_Breath = 6000; //Minimum time between breath in milliseconds
  44. unsigned long Max_Time_Between_Breath = 12000; //Maximum time between breath in milliseconds
  45. unsigned long Min_Breath_Time = 3000; //Minimum flicker time in milliseconds
  46. unsigned long Max_Breath_Time = 7000; //Maximum flicker time in milliseconds
  47.  
  48.  
  49. ///////////////////////////////////////////////////////////////
  50. ///////////////////////////////////////////////////////////////
  51. // DO NOT EDIT BELOW THIS UNLESS YOU KNOW WHAT YOU ARE DOING //
  52. ///////////////////////////////////////////////////////////////
  53. ///////////////////////////////////////////////////////////////
  54.  
  55. //Initialize base values
  56. int MotorStep = 7;
  57. unsigned long Motor_Time_Direction = random(Min_Motor_Time_Direction,Max_Motor_Time_Direction);
  58. unsigned long Motor_Speed_Change_Time = random(Min_Motor_Speed_Change_Time,Max_Motor_Speed_Change_Time);
  59. unsigned long Time_Between_Flicker = random(Min_Time_Between_Flicker,Max_Time_Between_Flicker);
  60. unsigned long Flicker_Time = random(Min_Flicker_Time,Max_Flicker_Time);
  61. unsigned long Time_Between_Breath = random(Min_Time_Between_Breath,Max_Time_Between_Breath);
  62. unsigned long Breath_Time = random(Min_Breath_Time,Max_Breath_Time);
  63. unsigned long LEDStartMillis_Flick = millis();
  64. unsigned long LEDStartMillis_Breath = millis();
  65. unsigned long LEDCurrentMillis_Flick = millis();
  66. unsigned long LEDCurrentMillis_Breath = millis();
  67. unsigned short FlickState = 0;
  68. unsigned short BreathState = 0;
  69. const unsigned short Org_RGB_Value[3] = {RGB_Value[0],RGB_Value[1],RGB_Value[2]};
  70.  
  71.  
  72. // Timers setup for "multitasking"
  73. unsigned long currentMillis = millis();
  74. unsigned long prevMotor_Time_Direction = 0;
  75. unsigned long prevMotor_Speed_Change_Time = 0;
  76. unsigned long prevMotor_Speed = 0;
  77. unsigned long prevTime_Between_Flicker = 0;
  78. unsigned long prevTime_Between_Breath = 0;
  79. unsigned long prevLEDMillis_Flick = 0;
  80. unsigned long prevLEDMillis_Breath = 0;
  81.  
  82.  
  83. void setup()
  84. {
  85.     //Setup Motor Pins
  86.     pinMode(MOTOR_PIN_1, OUTPUT);
  87.     pinMode(MOTOR_PIN_2, OUTPUT);
  88.     pinMode(MOTOR_PIN_3, OUTPUT);
  89.     pinMode(MOTOR_PIN_4, OUTPUT);
  90.    
  91.     //Setup LED Pins
  92.     pinMode(PIN_RED, OUTPUT);
  93.     pinMode(PIN_GREEN, OUTPUT);
  94.     pinMode(PIN_BLUE, OUTPUT);
  95.    
  96.     analogWrite(PIN_RED, RGB_Value[0]);
  97.     analogWrite(PIN_GREEN, RGB_Value[1]);
  98.     analogWrite(PIN_BLUE, RGB_Value[2]);
  99. }
  100.  
  101. void loop() {
  102.     currentMillis = millis();
  103.     //Random pick based on config
  104.     if (Motor_Change_Direction && (currentMillis - prevMotor_Time_Direction >= Motor_Time_Direction)) {
  105.         Motor_Direction = randomDirection();
  106.         Motor_Time_Direction = random(Min_Motor_Time_Direction,Max_Motor_Time_Direction);
  107.         prevMotor_Time_Direction = currentMillis;
  108.     }
  109.    
  110.     if (Motor_Change_Speed && (currentMillis - prevMotor_Speed_Change_Time >= Motor_Speed_Change_Time)) {
  111.         Motor_Speed = random(Min_Motor_Speed,Max_Motor_Speed);
  112.         Motor_Speed_Change_Time = random(Min_Motor_Speed_Change_Time,Max_Motor_Speed_Change_Time);
  113.         prevMotor_Speed_Change_Time = currentMillis;    
  114.     }
  115.  
  116.     if (Should_Flicker && (currentMillis - prevTime_Between_Flicker >= Time_Between_Flicker)) { //Run if flick enabled and >= timer
  117.         if(LEDStartMillis_Flick == 0){ //Get start millis for flick to keep track of run time
  118.             LEDStartMillis_Flick = currentMillis;
  119.         }
  120.         LEDCurrentMillis_Flick = millis(); //millis used to keep track of last state change
  121.         if(LEDCurrentMillis_Flick-LEDStartMillis_Flick <= Flicker_Time){
  122.             if(LEDCurrentMillis_Flick-prevLEDMillis_Flick >= random(30,120)){ //Change state every 30 to 120 ms at random
  123.                 if (FlickState == 1){
  124.                     analogWrite(PIN_RED, RGB_Value[0]);
  125.                     analogWrite(PIN_GREEN, RGB_Value[1]);
  126.                     analogWrite(PIN_BLUE, RGB_Value[2]);
  127.                     FlickState = 0;
  128.                 } else {
  129.                     analogWrite(PIN_RED, random(0,(RGB_Value[0])));
  130.                     analogWrite(PIN_GREEN, random(0,(RGB_Value[1])));
  131.                     analogWrite(PIN_BLUE, random(0,(RGB_Value[2])));
  132.                     FlickState = 1;
  133.                 }
  134.                 prevLEDMillis_Flick = LEDCurrentMillis_Flick;
  135.             }
  136.         }
  137.         if(LEDCurrentMillis_Flick-LEDStartMillis_Flick >= Flicker_Time){
  138.             LEDStartMillis_Flick = 0;
  139.             Time_Between_Flicker = random(Min_Time_Between_Flicker,Max_Time_Between_Flicker);
  140.             Flicker_Time = random(Min_Flicker_Time,Max_Flicker_Time);
  141.             prevTime_Between_Flicker = currentMillis;
  142.             analogWrite(PIN_RED, Org_RGB_Value[0]);
  143.             analogWrite(PIN_GREEN, Org_RGB_Value[1]);
  144.             analogWrite(PIN_BLUE, Org_RGB_Value[2]);
  145.             FlickState = 0;
  146.         }
  147.     }
  148.  
  149.     if (Should_Breath && (currentMillis - prevTime_Between_Breath >= Time_Between_Breath)) {
  150.         if(LEDStartMillis_Breath == 0){ //Get start millis for flick to keep track of run time
  151.             LEDStartMillis_Breath = currentMillis;
  152.         }
  153.         LEDCurrentMillis_Breath = millis(); //millis used to keep track of last state change
  154.         if(LEDCurrentMillis_Breath-LEDStartMillis_Breath <= Breath_Time){
  155.             if(LEDCurrentMillis_Breath-prevLEDMillis_Breath >= random(30,120)){ //Change state every 30 to 120 ms at random
  156.                 if (BreathState == 1){
  157.                     //Breath one direction
  158.                     BreathState = 0;
  159.                 } else {
  160.                     //Breath other direction
  161.                     BreathState = 1;
  162.                 }
  163.                 prevLEDMillis_Breath = LEDCurrentMillis_Breath;
  164.             }
  165.         }
  166.         if(LEDCurrentMillis_Breath-LEDStartMillis_Breath >= Breath_Time){
  167.             LEDStartMillis_Breath = 0;
  168.             Time_Between_Breath = random(Min_Time_Between_Breath,Max_Time_Between_Breath);
  169.             Breath_Time = random(Min_Breath_Time,Max_Breath_Time);
  170.             prevTime_Between_Breath = currentMillis;
  171.             analogWrite(PIN_RED, Org_RGB_Value[0]);
  172.             analogWrite(PIN_GREEN, Org_RGB_Value[1]);
  173.             analogWrite(PIN_BLUE, Org_RGB_Value[2]);
  174.             BreathState = 0;
  175.         }
  176.     }
  177.  
  178. //Start of motor movement
  179.     if (currentMillis - prevMotor_Speed >= Motor_Speed) {
  180.         if (Motor_Direction == 1) {
  181.             MotorStep++;
  182.         } else {
  183.             MotorStep--;
  184.         }
  185.         if (MotorStep > 7) {
  186.             MotorStep = 0;
  187.             MotorAdvance(MotorStep);
  188.         } else if (MotorStep < 0) {
  189.             MotorStep = 7;
  190.             MotorAdvance(MotorStep);
  191.         } else {
  192.             MotorAdvance(MotorStep);
  193.         }
  194.         prevMotor_Speed = currentMillis;
  195.     }
  196. //End of motor movement
  197. }
  198.  
  199. void MotorAdvance(int MotorStep) {
  200.     switch (MotorStep) {
  201.     case 0:
  202.         digitalWrite(MOTOR_PIN_1, LOW);
  203.         digitalWrite(MOTOR_PIN_2, LOW);
  204.         digitalWrite(MOTOR_PIN_3, LOW);
  205.         digitalWrite(MOTOR_PIN_4, HIGH);
  206.         break;
  207.     case 1:
  208.         digitalWrite(MOTOR_PIN_1, LOW);
  209.         digitalWrite(MOTOR_PIN_2, LOW);
  210.         digitalWrite(MOTOR_PIN_3, HIGH);
  211.         digitalWrite(MOTOR_PIN_4, HIGH);
  212.         break;
  213.     case 2:
  214.         digitalWrite(MOTOR_PIN_1, LOW);
  215.         digitalWrite(MOTOR_PIN_2, LOW);
  216.         digitalWrite(MOTOR_PIN_3, HIGH);
  217.         digitalWrite(MOTOR_PIN_4, LOW);
  218.         break;
  219.     case 3:
  220.         digitalWrite(MOTOR_PIN_1, LOW);
  221.         digitalWrite(MOTOR_PIN_2, HIGH);
  222.         digitalWrite(MOTOR_PIN_3, HIGH);
  223.         digitalWrite(MOTOR_PIN_4, LOW);
  224.         break;
  225.     case 4:
  226.         digitalWrite(MOTOR_PIN_1, LOW);
  227.         digitalWrite(MOTOR_PIN_2, HIGH);
  228.         digitalWrite(MOTOR_PIN_3, LOW);
  229.         digitalWrite(MOTOR_PIN_4, LOW);
  230.         break;
  231.     case 5:
  232.         digitalWrite(MOTOR_PIN_1, HIGH);
  233.         digitalWrite(MOTOR_PIN_2, HIGH);
  234.         digitalWrite(MOTOR_PIN_3, LOW);
  235.         digitalWrite(MOTOR_PIN_4, LOW);
  236.         break;
  237.     case 6:
  238.         digitalWrite(MOTOR_PIN_1, HIGH);
  239.         digitalWrite(MOTOR_PIN_2, LOW);
  240.         digitalWrite(MOTOR_PIN_3, LOW);
  241.         digitalWrite(MOTOR_PIN_4, LOW);
  242.         break;
  243.     case 7:
  244.         digitalWrite(MOTOR_PIN_1, HIGH);
  245.         digitalWrite(MOTOR_PIN_2, LOW);
  246.         digitalWrite(MOTOR_PIN_3, LOW);
  247.         digitalWrite(MOTOR_PIN_4, HIGH);
  248.         break;
  249.     default:
  250.         digitalWrite(MOTOR_PIN_1, LOW);
  251.         digitalWrite(MOTOR_PIN_2, LOW);
  252.         digitalWrite(MOTOR_PIN_3, LOW);
  253.         digitalWrite(MOTOR_PIN_4, LOW);
  254.         break;
  255.     }
  256. }
  257.  
  258. int randomDirection() {
  259.     return (int) random(2) * 2 - 1;
  260. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement