daily pastebin goal
34%
SHARE
TWEET

Untitled

a guest Jun 20th, 2018 62 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <CmdParser.hpp>
  2.  
  3. const float motorAngle = 1.8;
  4. const float stepSize = 1;
  5. unsigned long rpm = 100; //Default Speed - min = 100 max = 400
  6.  
  7. const float Cspu = 476;
  8. const float Mspu = 555;
  9. const float Yspu = 500;
  10. const float Kspu = 530;
  11.  
  12. bool clean = true, pump = false;
  13. unsigned long Red, Green, Blue, Volume;
  14. unsigned long Cyan, Magenta, Yellow, Key;
  15. float Csteps, Msteps, Ysteps, Ksteps;
  16.  
  17. #define enPin 8
  18. #define cDirPin 5
  19. #define cStepPin 2
  20. #define mDirPin 6
  21. #define mStepPin 3
  22. #define yDirPin 7
  23. #define yStepPin 4
  24. #define kDirPin 13
  25. #define kStepPin 12
  26. #define buzPin A1
  27.  
  28. void setup() {
  29.   pinMode(enPin, OUTPUT);
  30.   pinMode(cDirPin, OUTPUT);
  31.   pinMode(cStepPin, OUTPUT);
  32.   pinMode(mDirPin, OUTPUT);
  33.   pinMode(mStepPin, OUTPUT);
  34.   pinMode(yDirPin, OUTPUT);
  35.   pinMode(yStepPin, OUTPUT);
  36.   pinMode(kDirPin, OUTPUT);
  37.   pinMode(kStepPin, OUTPUT);
  38.  
  39.   pinMode(buzPin, OUTPUT);
  40.  
  41.   digitalWrite(enPin, HIGH);
  42.  
  43.   Serial.begin(38400);
  44.   Serial.println("Send command to operate......");  // PUMP 10 20 30 100 // SPEED 500 // CLEAN
  45. }
  46.  
  47. void loop() {
  48.   cmdRead(Red, Green, Blue, Volume);
  49. }
  50.  
  51. void cmdRead(unsigned long &R, unsigned long &G, unsigned long &B, unsigned long &vol) {
  52.   CmdParser cmdParser;
  53.   CmdBuffer<64> myBuffer;
  54.   if (myBuffer.readFromSerial(&Serial)) {
  55.     if (cmdParser.parseCmd(&myBuffer) != CMDPARSER_ERROR) {
  56.       Serial.println(" ");
  57.       Serial.print("Parsing Command : ");
  58.       Serial.println(myBuffer.getStringFromBuffer());
  59.       if (cmdParser.getParamCount() == 5) {
  60.         if(cmdParser.equalCmdParam(0, "PUMP")){
  61.           Serial.println(" ");Serial.print("Command : ");
  62.           Serial.println(cmdParser.getCommand());
  63.           R = atoi(cmdParser.getCmdParam(1));
  64.           G = atoi(cmdParser.getCmdParam(2));
  65.           B = atoi(cmdParser.getCmdParam(3));
  66.           vol = atoi(cmdParser.getCmdParam(4));
  67.           if(!clean && pump) {
  68.             Serial.print("Base Volume : ");Serial.print(Volume);Serial.println("ml");
  69.             Serial.println(" ");
  70.             Serial.print("RGB : ");Serial.print(Red);Serial.print(" ");Serial.print(Green);Serial.print(" ");Serial.println(Blue);
  71.             getCMYK(Cyan, Magenta, Yellow, Key);
  72.             getSteps(Csteps, Msteps, Ysteps, Ksteps);
  73.             makeColor();
  74.             Serial.println(" ");
  75.             Serial.println("Send command to operate......");
  76.           }
  77.           else {
  78.             Serial.println("Load the pumps then try again...!");
  79.             Serial.println(" ");
  80.             Serial.println("Send command to operate......");
  81.           }
  82.          }
  83.         else {
  84.           Serial.println("Invalid Command...!");
  85.           Serial.println(" ");
  86.         }
  87.       }
  88.      
  89.       else if (cmdParser.getParamCount() == 2) {
  90.         if(cmdParser.equalCmdParam(0, "SPEED")) {
  91.           rpm = atoi(cmdParser.getCmdParam(1));
  92.           if (rpm > 400) {
  93.             rpm = 400;
  94.           }
  95.           else if (rpm < 100) {
  96.             rpm = 100;
  97.           }
  98.           Serial.print("Flow Rate set to ");Serial.print(rpm);Serial.println(" RPM");
  99.           Serial.println(" ");
  100.         }
  101.         else {
  102.           Serial.println("Invalid Command");
  103.           Serial.println(" ");
  104.         }
  105.       }
  106.       else if (cmdParser.getParamCount() == 1) {
  107.         if(cmdParser.equalCmdParam(0, "LOAD")) {
  108.           if(clean) {
  109.             loadPump();
  110.             pump = true;
  111.             clean = false;
  112.             Serial.println(" ");
  113.           }
  114.           else {
  115.             Serial.println("Already Loaded...!");
  116.             Serial.println(" ");
  117.           }
  118.         }
  119.         else if(cmdParser.equalCmdParam(0, "LOAD_NOT_REQUIRED")) {
  120.           Serial.println("Please ensure that fuild is loaded before continuing.....!");
  121.           clean = false;
  122.           pump = true;
  123.         }
  124.         else if(cmdParser.equalCmdParam(0, "CLEAN")) {
  125.           if(!clean) {
  126.             cleanPump();
  127.             clean = true;
  128.             pump = false;
  129.             Serial.println(" ");
  130.           }
  131.           else {
  132.             Serial.println("Pumps are clean...!");
  133.             Serial.println(" ");
  134.           }
  135.         }
  136.         else {
  137.           Serial.println("Invalid Command");
  138.           Serial.println(" ");
  139.         }
  140.       }
  141.       else {
  142.         Serial.println("Invalid Command");
  143.         Serial.println(" ");
  144.       }
  145.     }
  146.   }
  147. }
  148.  
  149. void getCMYK(unsigned long &Cyan, unsigned long &Magenta, unsigned long &Yellow, unsigned long &Key) {
  150.   Serial.println("Converting RGB to CMYK (0-255 => 0-100)");
  151.   float R = (float)Red/255.0;
  152.   float G = (float)Green/255.0;
  153.   float B = (float)Blue/255.0;
  154.   float K = (1 - max(max(R, G),B));
  155.   float C = (1 - R - K)/(1 - K);
  156.   float M = (1 - G - K)/(1 - K);
  157.   float Y = (1 - B - K)/(1 - K);
  158.   Cyan = (float)C*100;
  159.   Magenta = (float)M*100;
  160.   Yellow = (float)Y*100;
  161.   Key = (float)K*100;
  162.   Serial.print("CMYK : ");Serial.print(Cyan);Serial.print(" ");Serial.print(Magenta);Serial.print(" ");Serial.print(Yellow);Serial.print(" ");Serial.println(Key);
  163. }
  164.  
  165. void getSteps(float &Csteps, float &Msteps, float &Ysteps, float &Ksteps) {
  166.   float Cvol = ((float)(Cyan*Volume))/100;
  167.   float Mvol = ((float)(Magenta*Volume))/100;
  168.   float Yvol = ((float)(Yellow*Volume))/100;
  169.   float Kvol = ((float)(Key*Volume))/100;
  170.  
  171.   Csteps = Cvol * Cspu;
  172.   Msteps = Mvol * Mspu;
  173.   Ysteps = Yvol * Yspu;
  174.   Ksteps = Kvol * Kspu;
  175.  
  176.   Serial.println(" ");
  177.   Serial.print("Color Volume: ");Serial.print(Cvol);Serial.print(" ");Serial.print(Mvol);Serial.print(" ");Serial.print(Yvol);Serial.print(" ");Serial.println(Kvol);
  178.   Serial.println(" ");
  179. }
  180.  
  181. void loadPump() {
  182.   digitalWrite(enPin, LOW);
  183.   Serial.print("Loading Fluid into Pumps...");
  184.   cStepperRotate(3000, rpm);
  185.   Serial.print("...");
  186.   mStepperRotate(3000, rpm);
  187.   Serial.print("...");
  188.   yStepperRotate(3000, rpm);
  189.   Serial.print("...");
  190.   kStepperRotate(3000, rpm);
  191.   Serial.println("...Done!");
  192. }
  193.  
  194. void cleanPump() {
  195.   digitalWrite(enPin, LOW);
  196.   Serial.print("Cleaning Pumps...");
  197.   cStepperRotate(-3000, rpm);
  198.   Serial.print("...");
  199.   mStepperRotate(-3000, rpm);
  200.   Serial.print("...");
  201.   yStepperRotate(-3000, rpm);
  202.   Serial.print("...");
  203.   kStepperRotate(-3000, rpm);
  204.   Serial.println("...Done!");
  205.   digitalWrite(enPin, HIGH);
  206. }
  207.  
  208. void makeColor() {
  209.   digitalWrite(enPin, LOW);
  210.   if (Csteps != 0) {
  211.     Serial.print("Pumping Cyan with ");
  212.     Serial.print(Csteps);
  213.     Serial.print(" Steps");
  214.     cStepperRotate(Csteps, rpm);
  215.     Serial.println("    .....Done!");
  216.     setBuzz();
  217.   }
  218.   if (Msteps != 0) {
  219.     Serial.print("Pumping Magenta with ");
  220.     Serial.print(Msteps);
  221.     Serial.print(" Steps");
  222.     mStepperRotate(Msteps, rpm);
  223.     Serial.println("    .....Done!");
  224.     setBuzz();
  225.   }
  226.   if (Ysteps != 0) {
  227.     Serial.print("Pumping Yellow with ");
  228.     Serial.print(Ysteps);
  229.     Serial.print(" Steps");
  230.     yStepperRotate(Ysteps, rpm);
  231.     Serial.println("    .....Done!");
  232.     setBuzz();
  233.   }
  234.   if (Ksteps != 0) {
  235.     Serial.print("Pumping Key with ");
  236.     Serial.print(Ksteps);
  237.     Serial.print(" Steps");
  238.     kStepperRotate(Ksteps, rpm);
  239.     Serial.println("    .....Done!");
  240.     setBuzz();
  241.   }
  242.    
  243.   Serial.println(" ");
  244.   Serial.println("All colors pumped!");
  245.   Serial.println(" ");
  246. }
  247.  
  248. void setBuzz() {
  249.   analogWrite(buzPin, 255);
  250.   delay(100);
  251.   analogWrite(buzPin, 0);
  252.   delay(50);
  253. }
  254.  
  255. void cStepperRotate(float steps, float rpm) {
  256.   float stepsPerRotation = (360.00 / motorAngle) / stepSize;
  257.   unsigned long stepPeriodmicroSec = ((60.0000 / (rpm * stepsPerRotation)) * 1E6 / 2.0000) - 5;
  258.  
  259.   if (steps > 0) {
  260.     digitalWrite(cDirPin, HIGH);
  261.   }
  262.   else {
  263.     digitalWrite(cDirPin, LOW);
  264.   }
  265.  
  266.   if (steps > 0) {
  267.     float totalSteps = steps;
  268.     for (unsigned long i = 0; i < totalSteps; i++) {
  269.       digitalWrite(cStepPin, HIGH);
  270.       delayMicroseconds(stepPeriodmicroSec);
  271.       digitalWrite(cStepPin, LOW);
  272.       delayMicroseconds(stepPeriodmicroSec);
  273.     }
  274.   }
  275.   else {
  276.     float totalSteps = steps * -1;
  277.     for (unsigned long i = 0; i < totalSteps; i++) {
  278.       digitalWrite(cStepPin, HIGH);
  279.       delayMicroseconds(stepPeriodmicroSec);
  280.       digitalWrite(cStepPin, LOW);
  281.       delayMicroseconds(stepPeriodmicroSec);
  282.     }
  283.   }
  284. }
  285.  
  286. void mStepperRotate(float steps, float rpm) {
  287.   float stepsPerRotation = (360.00 / motorAngle) / stepSize;
  288.   unsigned long stepPeriodmicroSec = ((60.0000 / (rpm * stepsPerRotation)) * 1E6 / 2.0000) - 5;
  289.  
  290.   if (steps > 0) {
  291.     digitalWrite(mDirPin, HIGH);
  292.   }
  293.   else {
  294.     digitalWrite(mDirPin, LOW);
  295.   }
  296.  
  297.   if (steps > 0) {
  298.     float totalSteps = steps;
  299.     for (unsigned long i = 0; i < totalSteps; i++) {
  300.       digitalWrite(mStepPin, HIGH);
  301.       delayMicroseconds(stepPeriodmicroSec);
  302.       digitalWrite(mStepPin, LOW);
  303.       delayMicroseconds(stepPeriodmicroSec);
  304.     }
  305.   }
  306.   else {
  307.     float totalSteps = steps * -1;
  308.     for (unsigned long i = 0; i < totalSteps; i++) {
  309.       digitalWrite(mStepPin, HIGH);
  310.       delayMicroseconds(stepPeriodmicroSec);
  311.       digitalWrite(mStepPin, LOW);
  312.       delayMicroseconds(stepPeriodmicroSec);
  313.     }
  314.   }
  315. }
  316.  
  317. void yStepperRotate(float steps, float rpm) {
  318.   float stepsPerRotation = (360.00 / motorAngle) / stepSize;
  319.   unsigned long stepPeriodmicroSec = ((60.0000 / (rpm * stepsPerRotation)) * 1E6 / 2.0000) - 5;
  320.  
  321.   if (steps > 0) {
  322.     digitalWrite(yDirPin, HIGH);
  323.   }
  324.   else {
  325.     digitalWrite(yDirPin, LOW);
  326.   }
  327.  
  328.   if (steps > 0) {
  329.     float totalSteps = steps;
  330.     for (unsigned long i = 0; i < totalSteps; i++) {
  331.       digitalWrite(yStepPin, HIGH);
  332.       delayMicroseconds(stepPeriodmicroSec);
  333.       digitalWrite(yStepPin, LOW);
  334.       delayMicroseconds(stepPeriodmicroSec);
  335.     }
  336.   }
  337.   else {
  338.     float totalSteps = steps * -1;
  339.     for (unsigned long i = 0; i < totalSteps; i++) {
  340.       digitalWrite(yStepPin, HIGH);
  341.       delayMicroseconds(stepPeriodmicroSec);
  342.       digitalWrite(yStepPin, LOW);
  343.       delayMicroseconds(stepPeriodmicroSec);
  344.     }
  345.   }
  346. }
  347.  
  348. void kStepperRotate(float steps, float rpm) {
  349.   float stepsPerRotation = (360.00 / motorAngle) / stepSize;
  350.   unsigned long stepPeriodmicroSec = ((60.0000 / (rpm * stepsPerRotation)) * 1E6 / 2.0000) - 5;
  351.  
  352.   if (steps > 0) {
  353.     digitalWrite(kDirPin, HIGH);
  354.   }
  355.   else {
  356.     digitalWrite(kDirPin, LOW);
  357.   }
  358.  
  359.   if (steps > 0) {
  360.     float totalSteps = steps;
  361.     for (unsigned long i = 0; i < totalSteps; i++) {
  362.       digitalWrite(kStepPin, HIGH);
  363.       delayMicroseconds(stepPeriodmicroSec);
  364.       digitalWrite(kStepPin, LOW);
  365.       delayMicroseconds(stepPeriodmicroSec);
  366.     }
  367.   }
  368.   else {
  369.     float totalSteps = steps * -1;
  370.     for (unsigned long i = 0; i < totalSteps; i++) {
  371.       digitalWrite(kStepPin, HIGH);
  372.       delayMicroseconds(stepPeriodmicroSec);
  373.       digitalWrite(kStepPin, LOW);
  374.       delayMicroseconds(stepPeriodmicroSec);
  375.     }
  376.   }
  377. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top