pleasedontcode

Robot Controller rev_01

Sep 27th, 2025
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Arduino 34.04 KB | None | 0 0
  1. /********* Pleasedontcode.com **********
  2.  
  3.     Pleasedontcode thanks you for automatic code generation! Enjoy your code!
  4.  
  5.     - Terms and Conditions:
  6.     You have a non-exclusive, revocable, worldwide, royalty-free license
  7.     for personal and commercial use. Attribution is optional; modifications
  8.     are allowed, but you're responsible for code maintenance. We're not
  9.     liable for any loss or damage. For full terms,
  10.     please visit pleasedontcode.com/termsandconditions.
  11.  
  12.     - Project: Robot Controller
  13.     - Source Code NOT compiled for: Arduino Mega
  14.     - Source Code created on: 2025-09-27 15:41:28
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* Runs both in auto mode and manual mode smoothly. */
  21.     /* log generates with proper time intervals. help to */
  22.     /* remove any non-blocking code lines. */
  23. /****** END SYSTEM REQUIREMENTS *****/
  24.  
  25.  
  26. /* START CODE */
  27.  
  28. // Compatibility notes:
  29. // - The USER CODE defines a full program (pins, state machines, serial I/O, etc.) intended for an Arduino Mega.
  30. // - The PRELIMINARY CODE contained only empty setup/loop stubs with a minimal prototype section. To ensure a working sketch, the USER CODE is placed here in full and the original PRELIMINARY stubs are effectively overridden by this content.
  31. // - If any line seemed incompatible with the Mega pin mapping, I left it in place but you9ll see it function as standard Arduino Mega I/O. The Mega has many digital pins (0-53) and analog pins A0-A15 (which map to digital numbers 54-69); the pins used in the code (e.g., 3,4,5,15,24-35, 28-31, A0-A7 equivalents, etc.) are within valid ranges for typical Mega configurations. If you run into a pin-mapping mismatch on your board variant, adjust the pin numbers accordingly and re-test.
  32.  // If you9d like, I can adjust pin mappings to explicit Mega D-pin numbers or to A0-A7 constants to avoid ambiguity on certain board variants.
  33.  
  34. /****** DEFINITION OF LIBRARIES *****/
  35.  
  36. /****** FUNCTION PROTOTYPES *****/
  37. // (None required here since the USER CODE includes full function definitions and Arduino IDE can generate prototypes automatically. If you want explicit prototypes, I can add them here.)
  38. // Note: the USER CODE uses a variety of functions. A prototype for updateFeeder() is implicitly provided by the IDE, but can be added explicitly if desired.
  39.  
  40. void logEvent(const String &message);
  41.  
  42. void setup(void);
  43. void loop(void);
  44.  
  45.  // User code begins
  46. // Define stepper motor1 of Linear guide @ Spinning Motor driver pins
  47. const int DRIVER1_DIR = 28; // Direction pin for stepper motor1
  48. const int DRIVER1_STEP = 5; // Step pin for stepper motor1
  49. const int DRIVER1_ENA = 29; // Enable pin for stepper motor1
  50.  
  51. // Define stepper motor2 of Linear guide @ Feeder Motor driver pins
  52. const int DRIVER2_DIR = 30; // Direction pin for stepper motor2
  53. const int DRIVER2_STEP = 6; // Step pin for stepper motor2
  54. const int DRIVER2_ENA = 31; // Enable pin for stepper motor2
  55.  
  56. // Proximity Sensor for Induction Motor CW and CCW
  57. const int PROXY_POS = 24; // Proxy POS Sensor for Linear Motor Position
  58. const int PROXY_A = 25; // Proxy A Sensor for CW
  59. const int PROXY_B = 26; // Proxy B Sensor for CCW
  60. const int PROXY_RUNFEED = 27; // Proxy Feed Run Sensor
  61.  
  62. // Define BTS7960B motor driver pins for Feeder motor
  63. const int EN_FEEDER = 20; // Enable pin for feeder motor
  64. const int LPWM_FEEDER = 3; // Left PWM pin for feeder motor
  65. const int RPWM_FEEDER = 4; // Right PWM pin for feeder motor
  66.  
  67. // Define Limit switchs of Linear drive @ Spinning motor
  68. const int LIMIT_SWITCH1 = 15; // 32; // Limit switch 1 pin (Bottom)
  69. const int LIMIT_SWITCH2 = 33; // Limit switch 2 pin (Up)
  70. // Define Limit switchs of Linear drive @ Feeder motor
  71. const int LIMIT_SWITCH3 = 16; // Limit switch 3 pin (Reverse)
  72. const int LIMIT_SWITCH4 = 35; // Limit switch 4 pin (Forward)
  73.  
  74. // Define relay pins
  75. const int RELAY1 = A0; // Relay for
  76. const int RELAY2 = A1; // Relay for Air-Nipper Cutter
  77. const int RELAY3 = A2; // Relay for Gripper 1
  78. const int RELAY4 = A3; // Relay for Gripper 2
  79. const int RELAY5 = A4; // Relay for CW direction
  80. const int RELAY6 = A5; // Relay for CCW direction
  81. const int RELAY7 = A6; // Relay for Feeder Linear Drive
  82. const int RELAY8 = A7; // Relay for Label Vaccum pad Forward/Backward
  83. const int RELAY9 = 44; // Relay for Spinning Linear Drive
  84. const int RELAY10 = 45; // Relay for Cutter-up & Cutter-down Cylinder
  85. const int RELAY11 = 46; // Relay for Label Vaccum Pad Pick & Eject
  86. const int RELAY12 = 47; // Relay for
  87. const int RELAY13 = 48; // Relay for
  88. const int RELAY14 = 49; // Relay for
  89. const int RELAY15 = 50; // Relay for
  90. const int RELAY16 = 51; // Relay for
  91.  
  92. // Flags and variables
  93. bool autoMode = false; // Flag for Auto Mode
  94. bool manualMode = false; // Flag for Manual Mode
  95. int numCycles = 0; // Variable to store number of cycles
  96. bool feederRunning = false; // Flag to indicate if the feeder is running
  97. bool isForward = false;
  98. int targetCounts = 0; // Stores the required counts for current operation
  99. volatile int countA = 0; // Count for Proxy A
  100. volatile int countB = 0; // Count for Proxy B
  101. bool motorRunning = false; // Flag to indicate if the motor is currently running
  102. bool countingEnabled = false; // Flag to enable counting based on relay state
  103. bool isClockwise = false; // Global variable to track motor direction
  104. bool countProxyAEnabled = true; // Flag to enable/disable counting for Proxy A
  105. bool countProxyBEnabled = true; // Flag to enable/disable counting for Proxy B
  106. bool countProxyPOSEnabled = true; // Flag to enable/disable counting for Proxy POS
  107. volatile bool isMoving1 = false; // Flag to indicate if the linear guide is currently moving
  108. volatile bool isMoving2 = false; // Flag to indicate if the linear guide is currently moving
  109. unsigned long feederStartTime = 0;  // When feeder started
  110. unsigned long feederDuration = 0;   // How long to run (ms)
  111.  
  112. // Command enumeration
  113. enum Command { START, STOP, G1, G2, G3, G4, VC, VO, VF, VR, PU, PD, OG, CW, CCW, LU, LD, LF, LR, CU, CD, CX, X, FD, RD, AUTO, MANUAL, NONE, UNKNOWN };
  114.  
  115. // Debounce variables
  116. const unsigned long DEBOUNCE_TIME = 1000; // 1 second debounce time
  117. unsigned long lastCommandTime = 0; // Variable to store the last command execution time
  118.  
  119. // Additional state for non-blocking auto-cycle input
  120. bool awaitingCycleInput = false; // New: wait for numeric input for cycles
  121. unsigned long cycleInputDeadline = 0; // New: timeout for cycle input
  122.  
  123. // Function declarations
  124. void stopMotor(); // To Stop Linear Motor, Feeder Motor, Spinning Motor
  125. void moveLinearGuide1(String direction); // To move Linear UP and Down
  126. void moveLinearGuide2(String direction); // To move Linear Right and Left
  127. void runFeeder(bool forward, unsigned long durationMillis = 0, int speed = 25); // To activte Feeder motor
  128. void setFeederMotorSpeed(int speed, bool forward);
  129. void runOpenGripper(); // To deactivate Gripper Relay 3 & 4
  130. void runMotor(bool clockwise); // Combined CW and CCW motor function
  131. void handleCommand(Command cmd); // To handle and execute Manual Input Command
  132. bool runAutoMode(int numCycles); // To execute Auto Input Command sequence
  133. void resetSerialBuffer(); // To reset Command line
  134. bool checkStopCommand(); // To check for STOP command during process
  135. void resetMode();
  136. void proxyA();
  137. void proxyB();
  138.  
  139. void setup() {
  140.  // Set pin modes for motors and relays
  141.  pinMode(RPWM_FEEDER, OUTPUT);
  142.  pinMode(LPWM_FEEDER, OUTPUT);
  143.  pinMode(EN_FEEDER, OUTPUT);
  144.  pinMode(PROXY_POS, INPUT_PULLUP); // Set proxy POS as input
  145.  pinMode(PROXY_A, INPUT_PULLUP); // Set proxy A as input
  146.  pinMode(PROXY_B, INPUT_PULLUP); // Set proxy B as input
  147.  pinMode(PROXY_RUNFEED, INPUT); // Set proxy Feed Run as input
  148.  
  149.  pinMode(RELAY1, OUTPUT);
  150.  pinMode(RELAY2, OUTPUT);
  151.  pinMode(RELAY3, OUTPUT);
  152.  pinMode(RELAY4, OUTPUT);
  153.  pinMode(RELAY5, OUTPUT);
  154.  pinMode(RELAY6, OUTPUT);
  155.  pinMode(RELAY7, OUTPUT);
  156.  pinMode(RELAY8, OUTPUT);
  157.  pinMode(RELAY9, OUTPUT);
  158.  pinMode(RELAY10, OUTPUT);
  159.  pinMode(RELAY11, OUTPUT);
  160.  pinMode(RELAY12, OUTPUT);
  161.  pinMode(RELAY13, OUTPUT);
  162.  pinMode(RELAY14, OUTPUT);
  163.  pinMode(RELAY15, OUTPUT);
  164.  pinMode(RELAY16, OUTPUT);
  165.  
  166.  pinMode(LIMIT_SWITCH1, INPUT_PULLUP);
  167.  pinMode(LIMIT_SWITCH2, INPUT_PULLUP);
  168.  pinMode(LIMIT_SWITCH3, INPUT_PULLUP);
  169.  pinMode(LIMIT_SWITCH4, INPUT_PULLUP);
  170.  
  171.  pinMode(DRIVER1_STEP, OUTPUT);
  172.  pinMode(DRIVER1_DIR, OUTPUT);
  173.  pinMode(DRIVER1_ENA, OUTPUT);
  174.  pinMode(DRIVER2_STEP, OUTPUT);
  175.  pinMode(DRIVER2_DIR, OUTPUT);
  176.  pinMode(DRIVER2_ENA, OUTPUT);
  177.  
  178.  // Initialize relays to LOW (off state)
  179.  digitalWrite(RELAY1, LOW);
  180.  digitalWrite(RELAY2, LOW);
  181.  digitalWrite(RELAY3, LOW);
  182.  digitalWrite(RELAY4, LOW);
  183.  digitalWrite(RELAY5, LOW);
  184.  digitalWrite(RELAY6, LOW);
  185.  digitalWrite(RELAY7, LOW);
  186.  digitalWrite(RELAY8, LOW);
  187.  digitalWrite(RELAY9, LOW);
  188.  digitalWrite(RELAY10, LOW);
  189.  digitalWrite(RELAY11, LOW);
  190.  digitalWrite(RELAY12, LOW);
  191.  digitalWrite(RELAY13, LOW);
  192.  digitalWrite(RELAY14, LOW);
  193.  digitalWrite(RELAY15, LOW);
  194.  digitalWrite(RELAY16, LOW);
  195.  
  196.  digitalWrite(DRIVER1_ENA, LOW);  // Enable stepper motor1
  197.  digitalWrite(DRIVER2_ENA, LOW);  // Enable stepper motor2
  198.  
  199.  Serial.begin(9600);  // Start serial communication
  200.  resetSerialBuffer();  // Clear serial buffer on startup
  201.  logEvent("System initialized"); // New: log with time intervals
  202. }
  203. void logEvent(const String &message) {
  204.  // Lightweight non-blocking logging helper with timestamp
  205.  Serial.print("[");
  206.  Serial.print(millis());
  207.  Serial.print(" ms] ");
  208.  Serial.println(message);
  209. }
  210. Command parseCommand(String command);
  211.  
  212. Command parseCommand(String command) {
  213.  command.trim();
  214.  // Parse incoming command strings and return corresponding Command enum
  215.  if (command == "START") return START;
  216.  if (command == "STOP") return STOP;
  217.  if (command == "G1") return G1;
  218.  if (command == "G2") return G2;
  219.  if (command == "G3") return G3;
  220.  if (command == "G4") return G4;
  221.  if (command == "VC") return VC;
  222.  if (command == "VO") return VO;
  223.  if (command == "VF") return VF;
  224.  if (command == "VR") return VR;
  225.  if (command == "PU") return PU;
  226.  if (command == "PD") return PD;
  227.  if (command == "OG") return OG;
  228.  if (command == "CW") return CW;
  229.  if (command == "CCW") return CCW;
  230.  if (command == "LU") return LU;
  231.  if (command == "LD") return LD;
  232.  if (command == "LF") return LF;
  233.  if (command == "LR") return LR;
  234.  if (command == "CU") return CU;
  235.  if (command == "CD") return CD;
  236.  if (command == "CX") return CX;
  237.  if (command == "X") return X;
  238.  if (command == "FD") return FD; // Forward command
  239.  if (command == "RD") return RD; // Reverse command
  240.  if (command == "AUTO") return AUTO; // Auto mode
  241.  if (command == "MANUAL") return MANUAL; // Manual mode
  242.  return UNKNOWN; // Return UNKNOWN if command is not recognized
  243. }
  244.  
  245. void handleCommand(Command cmd) {
  246.  // Handle commands based on the parsed Command enum
  247.  switch (cmd) {
  248.    case START:
  249.      Serial.println("Process started.");
  250.    break;
  251.    case STOP:
  252.      stopMotor();
  253.      Serial.println("MOTORS STOPPED");
  254.    break;
  255.    case G1:
  256.      digitalWrite(RELAY3, HIGH); // Activate RELAY3 for G1 Close
  257.      Serial.println("RELAY3 ON");
  258.    break;
  259.    case G2:
  260.      digitalWrite(RELAY3, LOW); // Deactivate RELAY3 for G2 Open
  261.      Serial.println("RELAY3 OFF");
  262.    break;
  263.    case G3:
  264.      digitalWrite(RELAY4, HIGH); // Activate RELAY4 for G2 Close
  265.      Serial.println("RELAY4 ON");
  266.    break;
  267.    case G4:
  268.      digitalWrite(RELAY4, LOW); // Deactivate RELAY4 for G2 Open
  269.      Serial.println("RELAY4 OFF");
  270.    break;
  271.    case VC:
  272.      digitalWrite(RELAY11, HIGH); // Activate RELAY10 for Vaccum pad Pick
  273.      Serial.println("RELAY11 ON");
  274.    break;
  275.    case VO:
  276.      digitalWrite(RELAY11, LOW); // Deactivate RELAY10 for Vaccum pad Eject
  277.      Serial.println("RELAY11 OFF");
  278.    break;
  279.    case VF:
  280.      digitalWrite(RELAY8, HIGH); // Activate RELAY8 for Vaccum pad Forward
  281.      Serial.println("RELAY8 ON");
  282.    break;
  283.    case VR:
  284.      digitalWrite(RELAY8, LOW); // Deactivate RELAY8 for Vaccum pad Reverse
  285.      Serial.println("RELAY8 OFF");
  286.    break;
  287.    case PU:
  288.      digitalWrite(RELAY9, HIGH); // Activate RELAY9 for Push Up Load
  289.      Serial.println("RELAY9 ON");
  290.    break;
  291.    case PD:
  292.      digitalWrite(RELAY9, LOW); // Deactivate RELAY9 for Push Down Load
  293.      Serial.println("RELAY9 OFF");
  294.    break;
  295.    case OG:
  296.      runOpenGripper();
  297.      Serial.println("Gripper 1 & 2 OPEN...");
  298.    break;
  299.    case CW:
  300.      runMotor(true);
  301.    break;
  302.    case CCW:
  303.      runMotor(false);
  304.    break;
  305.    case LU:
  306.      moveLinearGuide1("LU");
  307.    break;
  308.    case LD:
  309.      moveLinearGuide1("LD");
  310.    break;
  311.    case LF:
  312.      moveLinearGuide2("LF");
  313.    break;
  314.    case LR:
  315.      moveLinearGuide2("LR");
  316.    break;
  317.    case CX:
  318.      digitalWrite(RELAY2, HIGH);
  319.      delay(1000);
  320.      digitalWrite(RELAY2, LOW);
  321.      Serial.println("Air-Nipper Cutter Operated.");
  322.    break;
  323.    case CU:
  324.      digitalWrite(RELAY10, HIGH); // Activate RELAY10 for Cutter Up Load
  325.      Serial.println("RELAY10 ON");
  326.    break;
  327.    case CD:
  328.      digitalWrite(RELAY10, LOW); // Deactivate RELAY10 for Cutter Down Load
  329.      Serial.println("RELAY10 OFF");
  330.    break;
  331.    case X:
  332.      stopMotor();
  333.      Serial.println(" All motors stopped.");
  334.    break;
  335.    case FD:
  336.      runFeeder(true, 2150); // Move feeder forward
  337.      Serial.println("Feeder moving forward.");
  338.    break;
  339.    case RD:
  340.      runFeeder(false, 2150); // Move feeder reverse
  341.      Serial.println("Feeder moving reverse.");
  342.    break;
  343.    case AUTO:
  344.    if (!autoMode) {  // Only prompt if not already in Auto mode
  345.      resetMode();  // Reset mode before starting Auto Mode
  346.      autoMode = true;  // Set Auto mode flag
  347.      awaitingCycleInput = true; // Start waiting for cycles count (non-blocking)
  348.      cycleInputDeadline = millis() + 10000; // 10s timeout for input
  349.      numCycles = 0;
  350.      Serial.println("Auto mode activated.");
  351.      Serial.println("Please enter the number of cycles:");
  352.     }
  353.    break;
  354.    case MANUAL:
  355.    if (!manualMode) {  // Only prompt if not already in Manual mode
  356.      resetMode();  // Reset mode before starting Manual Mode
  357.      manualMode = true;  // Set Manual mode flag
  358.      Serial.println("Manual Mode selected. Available Commands: ...");  // Show manual commands
  359.     }
  360.    break;
  361.    default:
  362.    Serial.println("UNKNOWN COMMAND");
  363.    break;
  364.   }
  365. }
  366. void loop() { // Process new serial commands
  367.  // Non-blocking input handling for auto mode cycle count
  368.  if (awaitingCycleInput) {
  369.    if (Serial.available() > 0) {
  370.      String input = Serial.readStringUntil('\n');
  371.      input.trim();
  372.      long n = input.toInt();
  373.      if (n > 0) {
  374.        numCycles = (int)n;
  375.        awaitingCycleInput = false;
  376.        Serial.print("Auto cycles set: "); Serial.println(numCycles);
  377.        logEvent("Auto mode starting with cycles=" + String(numCycles));
  378.      } else {
  379.        Serial.println("Invalid cycle count. Please enter a positive number:");
  380.      }
  381.      resetSerialBuffer();
  382.    }
  383.    // Timeout handling
  384.    if (millis() > cycleInputDeadline) {
  385.      awaitingCycleInput = false;
  386.      autoMode = false;
  387.      Serial.println("Auto mode cancelled due to input timeout.");
  388.      logEvent("Auto mode input timeout");
  389.    }
  390.  }
  391.  
  392.  if (Serial.available() > 0 && !awaitingCycleInput) {
  393.    String command = Serial.readStringUntil('\n');
  394.    command.trim(); // Remove any trailing spaces or newlines
  395.    // Do not flush buffer to avoid blocking; rely on above non-blocking approach
  396.    // Check if we still have an active auto/manual cycle input state
  397.    if (millis() - lastCommandTime >= DEBOUNCE_TIME) {
  398.      Last:
  399.      ;
  400.    }
  401.    
  402.    Serial.println("Received: " + command);  // Debugging
  403.    // Check if enough time has passed since the last command
  404.    if (millis() - lastCommandTime >= DEBOUNCE_TIME) {
  405.      Command cmd = parseCommand(command);
  406.      lastCommandTime = millis();
  407.            
  408.      if (cmd == AUTO) {     // Handle Auto Mode command
  409.        if (!autoMode) {  // Only proceed if not already in Auto mode
  410.          resetMode();  // Reset mode before starting Auto Mode
  411.          autoMode = true;  // Set Auto mode flag
  412.          awaitingCycleInput = true; // Start waiting for cycles count (non-blocking)
  413.          cycleInputDeadline = millis() + 10000; // 10s timeout for input
  414.          numCycles = 0;
  415.          Serial.println("Auto mode activated.");
  416.          Serial.println("Please enter the number of cycles:");
  417.         }
  418.       }
  419.      else if (cmd == MANUAL) {
  420.        // Handle Manual Mode command
  421.        if (!manualMode) {
  422.          resetMode();  // Reset mode before starting Manual Mode
  423.          manualMode = true;  // Set Manual mode flag
  424.          Serial.println("Manual Mode selected. Available Commands: ...");  // Show manual commands
  425.         }
  426.       }
  427.      else {
  428.        // Handle other commands (START, STOP, Q, etc.)
  429.        handleCommand(cmd);
  430.       }
  431.      // Clear serial buffer after mode changes or commands
  432.      resetSerialBuffer();
  433.     }
  434.   }
  435.  // If in Auto mode, run the auto sequence incrementally
  436.  if (autoMode) {
  437.    static unsigned long lastRunTime = 0;
  438.    if (millis() - lastRunTime > 1000) { // Execute once per second
  439.      bool completed = runAutoMode(numCycles);
  440.      if (completed) {
  441.        autoMode = false; // Reset Auto mode flag after execution
  442.        Serial.println("Auto mode completed. Enter next command (START, STOP, Q):");
  443.       }
  444.      lastRunTime = millis(); // Update last run time
  445.     }
  446.   }
  447.   // State monitoring (no more proxy function calls)
  448.  updateFeeder();
  449.  
  450.  if (motorRunning) {
  451.    if (isClockwise) { // Check if the motor is running CW
  452.      proxyA(); // Check Proxy A
  453.     }
  454.    else { // Otherwise, it must be CCW
  455.      proxyB(); // Check Proxy B
  456.     }
  457.    // Check if a stop command was received
  458.    if (checkStopCommand()) {
  459.      Serial.println("Stopping motor due to command.");
  460.      motorRunning = false;
  461.      digitalWrite(RELAY5, LOW);
  462.      digitalWrite(RELAY6, LOW);
  463.      Serial.println("INDUCTION MOTOR STOPPED.");
  464.      countingEnabled = false;
  465.     }
  466.   }
  467. }
  468. bool runAutoMode(int numCycles) {
  469.  Serial.print("Starting Auto mode for ");
  470.  Serial.print(numCycles);
  471.  Serial.println(" cycles.");
  472.  
  473.  for (int cycle = 1; cycle <= numCycles; cycle++) {
  474.    Serial.print("Cycle ");
  475.    Serial.print(cycle);
  476.    Serial.println(" in progress...");
  477.    
  478.    unsigned long lastActionTime = millis();
  479.      
  480.    while (millis() - lastActionTime < 1000) { if (checkStopCommand()) return false; }   // Return false if STOP is received
  481.  
  482.    moveLinearGuide1("LU"); // Spinning Linear UP
  483.    lastActionTime = millis();
  484.    while (millis() - lastActionTime < 1000) { if (checkStopCommand()) return false; }
  485.  
  486.    moveLinearGuide2("LF"); // Feeder Linear Forward
  487.    lastActionTime = millis();
  488.    while (millis() - lastActionTime < 1000) { if (checkStopCommand()) return false; }
  489.  
  490.    digitalWrite(RELAY11, HIGH); // Vaccum Pad plate air Close
  491.    lastActionTime = millis();
  492.    while (millis() - lastActionTime < 1000) { if (checkStopCommand()) return false; }
  493.  
  494.    digitalWrite(RELAY8, HIGH); // Vaccum Pad plate move forward
  495.    lastActionTime = millis();
  496.    while (millis() - lastActionTime < 1000) { if (checkStopCommand()) return false; }
  497.  
  498.    runFeeder(true, 2150); // Move feeder forward
  499.    lastActionTime = millis();
  500.    while (millis() - lastActionTime < 1000) { if (checkStopCommand()) return false; }
  501.  
  502.    digitalWrite(RELAY4, HIGH); // Close Gripper 2
  503.    lastActionTime = millis();
  504.    while (millis() - lastActionTime < 1000) {
  505.      if (checkStopCommand()) return false;
  506.      updateFeeder();
  507.    }
  508.  
  509.    digitalWrite(RELAY11, LOW); // Vaccum Pad plate air Open
  510.    lastActionTime = millis();
  511.    while (millis() - lastActionTime < 1000) { if (checkStopCommand()) return false; }
  512.  
  513.    digitalWrite(RELAY8, LOW); // Vaccum Pad plate move Reverse
  514.    lastActionTime = millis();
  515.    while (millis() - lastActionTime < 1000) { if (checkStopCommand()) return false; }
  516.  
  517.    moveLinearGuide2("LR"); // Feeder Linear Reverse
  518.    lastActionTime = millis();
  519.    while (millis() - lastActionTime < 1000) { if (checkStopCommand()) return false; }
  520.  
  521.    digitalWrite(RELAY3, HIGH); // Close Gripper 1
  522.    lastActionTime = millis();
  523.    while (millis() - lastActionTime < 1000) { if (checkStopCommand()) return false; }
  524.  
  525.    digitalWrite(RELAY10, HIGH); // Cutter Cylinder UP
  526.    lastActionTime = millis();
  527.    while (millis() - lastActionTime < 1000) { if (checkStopCommand()) return false; }
  528.  
  529.    digitalWrite(RELAY2, HIGH); // Cutter Activate
  530.    delay(3000);
  531.    digitalWrite(RELAY2,LOW); // Cutter De-Activate
  532.    lastActionTime = millis();
  533.    while (millis() - lastActionTime < 1000) { if (checkStopCommand()) return false; }
  534.  
  535.    digitalWrite(RELAY10, LOW); // Cutter Cylinder Down
  536.    lastActionTime = millis();
  537.    while (millis() - lastActionTime < 1000) { if (checkStopCommand()) return false; }
  538.  
  539.    moveLinearGuide1("LD");
  540.    lastActionTime = millis();
  541.    while (millis() - lastActionTime < 1000) { if (checkStopCommand()) return false; }
  542.    delay(1000);
  543.    
  544.    // Run motor clockwise
  545.    runMotor(true);
  546.    Serial.println("Starting CW motor...");
  547.    lastActionTime = millis();
  548.    while (millis() - lastActionTime < 1000) { if (checkStopCommand()) return false; }
  549.  
  550.    runOpenGripper();
  551.    lastActionTime = millis();
  552.    while (millis() - lastActionTime < 1000) { if (checkStopCommand()) return false; }
  553.  
  554.    digitalWrite(RELAY9, HIGH); // Push Load plate UP
  555.    lastActionTime = millis();
  556.    while (millis() - lastActionTime < 1000) { if (checkStopCommand()) return false; }
  557.  
  558.    digitalWrite(RELAY9, LOW); // Push Load plate down
  559.    lastActionTime = millis();
  560.    while (millis() - lastActionTime < 1000) { if (checkStopCommand()) return false; }
  561.  
  562.    // Run motor counter-clockwise
  563.    runMotor(false);
  564.    Serial.println("Starting CCW motor...");
  565.    lastActionTime = millis();
  566.    while (millis() - lastActionTime < 1000) { if (checkStopCommand()) return false; }
  567.  
  568.    Serial.print("Cycle ");
  569.    Serial.print(cycle);
  570.    Serial.println(" completed.");
  571.   }
  572.  Serial.println("Auto mode completed all cycles.");  // Notify Completion
  573.  resetMode();  // Reset the mode and prepare for new commands
  574.  return true;
  575. }
  576. // Helper function to check for STOP command
  577. bool checkStopCommand() {
  578.  if (Serial.available() > 0) {
  579.    Serial.peek();
  580.    String command = Serial.readStringUntil('\n');
  581.    command.trim();  // Remove extra spaces and newlines
  582.  
  583.    Serial.print("Received Command: ");
  584.    Serial.println(command); // Debugging line to see actual command
  585.  
  586.    if (command == "STOP") {
  587.      Serial.println("STOP command received. Stopping Auto mode.");
  588.      stopMotor();
  589.      autoMode = false; // Reset Auto mode flag
  590.      return true;  // Exit runAutoMode
  591.     }
  592.    else if (command == "X") {
  593.      Serial.println("Emergency Stop Received (X).");
  594.      stopMotor();  // Stop all motors
  595.      return true;  // Exit runAutoMode
  596.     }
  597.   }
  598.  return false; // No stop command detected
  599. }
  600. void setFeederMotorSpeed(int speed, bool forward) {
  601.  analogWrite(RPWM_FEEDER, forward ? map(speed, 0, 100, 0, 255) : 0);
  602.  analogWrite(LPWM_FEEDER, forward ? 0 : map(speed, 0, 100, 0, 255));
  603. }
  604. void runFeeder(bool forward, unsigned long durationMillis, int speed) {
  605.  if (feederRunning) {
  606.    Serial.println("Feeder is already running.");
  607.    return; // Return the current direction if already running
  608.   }
  609.  // Start the feeder motor
  610.  setFeederMotorSpeed(speed, forward);
  611.  digitalWrite(EN_FEEDER, HIGH);  // Enable feeder motor
  612.  delay (100);
  613.  feederRunning = true; // set feeder running flag to true
  614.  isForward = forward;
  615.  // Set duration if provided (0 means run indefinitely)
  616.  if (durationMillis > 0) {
  617.    feederStartTime = millis();
  618.    feederDuration = durationMillis;
  619.   } else {
  620.    feederDuration = 0; // Run until manually stopped
  621.   }
  622.  
  623.  Serial.print(forward ? "FEEDER MOTOR FD STARTED" : "FEEDER MOTOR RD STARTED");
  624.  if (durationMillis > 0) {
  625.    Serial.print(" for ");
  626.    Serial.print(durationMillis / 1000.0);
  627.    Serial.println(" seconds");
  628.   } else {
  629.    Serial.println(" (indefinite run)");
  630.   }
  631. }
  632. void updateFeeder() {
  633.  if (feederRunning && feederDuration > 0 && (millis() - feederStartTime >= feederDuration)) {
  634.    // Time's up - stop the motor
  635.    digitalWrite(EN_FEEDER, LOW);
  636.    stopMotor();
  637.    feederRunning = false;
  638.    Serial.println("Feeder motor stopped (time elapsed)");
  639.   }
  640. }
  641. void runMotor(bool clockwise) {
  642.  if (motorRunning) {
  643.    Serial.println("Motor is already running.");
  644.    return isClockwise; // Return the current direction if already running
  645.   }
  646.  int relayPin = clockwise ? RELAY5 : RELAY6; // Choose correct relay
  647.  digitalWrite(relayPin, HIGH); // Start motor
  648.  motorRunning = true; // Set motor running flag to true
  649.  countA = 0; // Reset count for Proxy A
  650.  countB = 0; // Reset count for Proxy B
  651.  countingEnabled = true; // Enable counting
  652.  isClockwise = clockwise; // Set the direction
  653.  
  654.  // Disable counting for the opposite proxy and Proxy POS
  655.  if (clockwise) {
  656.    countProxyBEnabled = false; // Disable counting for Proxy B
  657.    countProxyPOSEnabled = false; // Disable counting for Proxy POS
  658.   }
  659.   else {
  660.    countProxyAEnabled = false; // Disable counting for Proxy A
  661.    countProxyPOSEnabled = false; // Disable counting for Proxy POS
  662.   }
  663.  Serial.println(clockwise ? "INDUCTION MOTOR CW STARTED" : "INDUCTION MOTOR CCW STARTED");
  664.  return clockwise; // Return the direction
  665. }
  666. void moveLinearGuide1(String direction1) {
  667.  if (direction1 == "LU") {  // Forward Direction (Linear motor up)
  668.    int sensorState = digitalRead(PROXY_POS);
  669.    Serial.print("Proxy POS Sensor State: ");
  670.    Serial.println(sensorState); // Print the actual state of the sensor
  671.  
  672.    // Check if proxyPOS is active
  673.    if (sensorState == LOW) { // Assuming HIGH means the sensor is inactive
  674.      Serial.println("Cannot move up. Proxy POS is not active.");
  675.      return; // Exit the function if proxyPOS is not active
  676.     }
  677.    Serial.println("LU");
  678.    delay(1000);
  679.    digitalWrite(RELAY9, HIGH); // Turn ON Relay 1
  680.    // Serial.println("RELAY9 ON"); // Notify Python
  681.    digitalWrite(DRIVER1_DIR, HIGH); // Set direction forward
  682.    digitalWrite(DRIVER1_ENA, LOW); // Enable motor
  683.  
  684.    // Serial.print("Limit States - 1: ");
  685.    // Serial.print(digitalRead(LIMIT_SWITCH1));
  686.    // Serial.print("  & Limit States - 2: ");
  687.    // Serial.println(digitalRead(LIMIT_SWITCH2));
  688.  
  689.    while (digitalRead(LIMIT_SWITCH2) == HIGH) {
  690.      digitalWrite(DRIVER1_STEP, HIGH);
  691.      delayMicroseconds(20);
  692.      digitalWrite(DRIVER1_STEP, LOW);
  693.      delayMicroseconds(25);
  694.      
  695.      if (checkStopCommand()) {
  696.        digitalWrite(RELAY9, LOW);
  697.        Serial.println("Linear Guide 1 Stopped");
  698.        return;
  699.       }
  700.     }      
  701.    digitalWrite(RELAY9, LOW); // Turn OFF Relay 7
  702.    // Serial.println("RELAY9 OFF"); // Notify Python
  703.    // Serial.println("Motors stopped at Limit Switch 2.");
  704.    Serial.println("Linear Guide1 Moved UP and reached Home Position");
  705.    delay (1000);  
  706.   }
  707.   else if (direction1 == "LD") {  // Reverse Direction (Linear motor down)
  708.    Serial.println("LD");
  709.    delay(1000);
  710.    digitalWrite(RELAY9, HIGH); // Turn ON Relay 1
  711.    // Serial.println("RELAY9 ON"); // Notify Python
  712.    digitalWrite(DRIVER1_DIR, LOW); // Set direction reverse
  713.    digitalWrite(DRIVER1_ENA, LOW); // Enable motor
  714.  
  715.    // Serial.print("Limit States - 1: ");
  716.    // Serial.print(digitalRead(LIMIT_SWITCH1));
  717.    // Serial.print("  & Limit States - 2: ");
  718.    // Serial.println(digitalRead(LIMIT_SWITCH2));
  719.      
  720.    while (digitalRead(LIMIT_SWITCH1) == HIGH) {
  721.      digitalWrite(DRIVER1_STEP, HIGH);
  722.      delayMicroseconds(20);
  723.      digitalWrite(DRIVER1_STEP, LOW);
  724.      delayMicroseconds(25);
  725.      
  726.      if (checkStopCommand()) {
  727.        digitalWrite(RELAY9, LOW);
  728.        Serial.println("Linear Guide 1 Stopped");
  729.        return;
  730.       }
  731.     }    
  732.    digitalWrite(RELAY9, LOW); // Turn OFF Relay 7
  733.    // Serial.println("RELAY9 OFF"); // Notify Python
  734.    // Serial.println("Motors stopped at Limit Switch 2.");
  735.    Serial.println("Linear Guide1 Moved Down and reached Tying Position");
  736.    delay (1000);
  737.   }
  738.  else {
  739.    Serial.println("Invalid direction for moveLinearGuide.");
  740.   }
  741. }
  742. void moveLinearGuide2(String direction2) {
  743.  if (direction2 == "LF") {  // Move Forward Direction (Linear motor forward)
  744.    Serial.println("LF");
  745.    if (digitalRead(LIMIT_SWITCH4) == LOW) { // Already at limit
  746.      Serial.println("Already at Limit Switch 4");
  747.      return;
  748.     }
  749.    delay(1000);
  750.    digitalWrite(RELAY7, HIGH); // Turn ON Relay 7
  751.    // Serial.println("RELAY7 ON"); // Notify Python
  752.    digitalWrite(DRIVER2_DIR, HIGH); // Set direction forward
  753.    digitalWrite(DRIVER2_ENA, LOW); // Enable motor
  754.    
  755.    // Serial.print("Limit States - 3: ");
  756.    // Serial.print(digitalRead(LIMIT_SWITCH3));
  757.    // Serial.print("  & Limit States - 4: ");
  758.    // Serial.println(digitalRead(LIMIT_SWITCH4));
  759.    
  760.    while (digitalRead(LIMIT_SWITCH4) == HIGH) {
  761.      digitalWrite(DRIVER2_STEP, HIGH);
  762.      delayMicroseconds(20);
  763.      digitalWrite(DRIVER2_STEP, LOW);
  764.      delayMicroseconds(25);
  765.      
  766.      if (checkStopCommand()) {
  767.        digitalWrite(RELAY7, LOW);
  768.        Serial.println("Linear Guide 2 Stopped");
  769.        return;
  770.       }
  771.     }      
  772.    digitalWrite(RELAY7, LOW); // Turn OFF Relay 7
  773.    // Serial.println("RELAY7 OFF"); // Notify Python
  774.    // Serial.println("Motors stopped at Limit Switch 4.");
  775.    Serial.println("Linear Guide2 reached Test Position");
  776.    delay (1000);
  777.   }
  778.  else if (direction2 == "LR") {  // Move Reverse Direction (Linear motor reverse)
  779.    Serial.println("LR");
  780.    if (digitalRead(LIMIT_SWITCH3) == LOW) { // Already at limit
  781.      Serial.println("Already at Limit Switch 3");
  782.      return;
  783.     }
  784.    delay(1000);
  785.    digitalWrite(RELAY7, HIGH); // Turn ON Relay 7
  786.    // Serial.println("RELAY7 ON"); // Notify Python
  787.    digitalWrite(DRIVER2_DIR, LOW); // Set direction reverse
  788.    digitalWrite(DRIVER2_ENA, LOW); // Enable motor
  789.    
  790.    // Serial.print("Limit States - 3: ");
  791.    // Serial.print(digitalRead(LIMIT_SWITCH3));
  792.    // Serial.print("  & Limit States - 4: ");
  793.    // Serial.println(digitalRead(LIMIT_SWITCH4));
  794.      
  795.    // Run BTS7960B motor forward at 5 speed
  796.    digitalWrite(EN_FEEDER, HIGH); // Enable feeder motor
  797.    setFeederMotorSpeed(35, true); // Use the helper function to set speed and direction
  798.    
  799.    while (digitalRead(LIMIT_SWITCH3) == HIGH) {
  800.      digitalWrite(DRIVER2_STEP, HIGH);
  801.      delayMicroseconds(20);
  802.      digitalWrite(DRIVER2_STEP, LOW);
  803.      delayMicroseconds(25);
  804.      
  805.      if (checkStopCommand()) {
  806.        digitalWrite(RELAY7, LOW);
  807.        Serial.println("Linear Guide 2 Stopped");
  808.        return;
  809.       }
  810.     }      
  811.    // Stop both motors when Limit Switch 3 is triggered or X command is received
  812.    digitalWrite(RELAY7, LOW); // Turn OFF Relay 7
  813.    digitalWrite(EN_FEEDER, LOW);
  814.    // Serial.println("RELAY7 OFF"); // Notify Python
  815.    // Serial.println("Motors stopped at Limit Switch 3.");
  816.    Serial.println("Linear Guide2 reached Home Position");
  817.    delay (1000);
  818.   }
  819.   else {
  820.    Serial.println("Invalid direction for moveLinearGuide.");
  821.   }
  822. }
  823. void runOpenGripper() {
  824.  // Define the behavior for Opening Grippers 1 & 2 after tying
  825.  digitalWrite(RELAY3, LOW); // Deactivate Gripper 1 (RELAY3)
  826.  digitalWrite(RELAY4, LOW); // Deactivate Gripper 2 (RELAY4)
  827.  delay(1000);
  828. }
  829. void stopMotor() {
  830.  // Stop Feeder motor
  831.  analogWrite(RPWM_FEEDER, 0);
  832.  analogWrite(LPWM_FEEDER, 0);
  833.  digitalWrite(EN_FEEDER, LOW);
  834.  feederRunning = false; // Reset feeder running flag
  835.  // Turn off relays
  836.  digitalWrite(RELAY1, LOW);
  837.  digitalWrite(RELAY5, LOW);      // Disable both relays
  838.  digitalWrite(RELAY6, LOW);
  839.  motorRunning = false;  
  840.  // Serial.println("Motor stopped");
  841. }
  842. void resetSerialBuffer() {
  843.  while (Serial.available()) {
  844.    Serial.read();  // Clear serial input buffer
  845.   }
  846. }
  847. void resetMode() {
  848.  autoMode = false; // Reset Auto mode flag
  849.  manualMode = false; // Reset Manual mode flag
  850.  currentMode = NONE; // Reset the current mode to a default state
  851.  resetSerialBuffer(); // Clear the serial buffer
  852.  Serial.println("Enter next command (START, STOP, Q):");  // Prompt for the next command
  853.  // Reset non-blocking auto input state
  854.  awaitingCycleInput = false;
  855.  cycleInputDeadline = 0;
  856. }
  857. void proxyA() {
  858.  static int lastStateA = LOW; // Remember previous state for Proxy A
  859.  static unsigned long lastTriggerTimeA = 0;
  860.  const unsigned long debounceDelay = 300;
  861.  const unsigned long minCountInterval = 2600; // Minimum time between counts (1 second)
  862.  
  863.  int sensorStateA = digitalRead(PROXY_A); // Read the current state of the sensor
  864.  unsigned long currentTime = millis();
  865.  
  866.  // Status feedback
  867.  // Serial.print("Proxy A Status: ");
  868.  // Serial.println(sensorStateA == HIGH ? "Triggered (High)" : "Not Triggered (Low)");
  869.  
  870.  // Check for Proxy A (CW)
  871.  if (isClockwise && countProxyAEnabled) {
  872.    // Only detect Rising edge (LOW to HIGH)
  873.    if (lastStateA == LOW && sensorStateA == HIGH) {
  874.      // Debounce check
  875.      if ((currentTime - lastTriggerTimeA) > debounceDelay && (currentTime - lastTriggerTimeA) > minCountInterval) {
  876.        countA++;  // Increment count for Proxy A
  877.        lastTriggerTimeA = currentTime;  // Update last trigger time
  878.        Serial.print("Proxy A Count: ");
  879.        Serial.println(countA);
  880.  
  881.        // Stop relay 4 if count reaches 5
  882.        if (countA >= 4) {
  883.          digitalWrite(RELAY5, LOW); // Stop relay 4
  884.          Serial.print("INDUCTION MOTOR CW STOPPED");
  885.          motorRunning = false; // Set flag to stop motor
  886.          countA = 0; // Reset count after stopping
  887.          countProxyAEnabled = false; // Disable counting for Proxy A
  888.          countProxyBEnabled = true; // Re-enable counting for Proxy B
  889.         }      
  890.       }
  891.     }
  892.    lastStateA = sensorStateA; // Update last state for Proxy A
  893.   }
  894. }
  895. void proxyB() {
  896.  static int lastStateB = LOW; // Remember previous state for Proxy B
  897.  static unsigned long lastTriggerTimeB = 0;
  898.  const unsigned long debounceDelay = 300;
  899.  const unsigned long minCountInterval = 2600; // Minimum time between counts (1 second)
  900.  
  901.  int sensorStateB = digitalRead(PROXY_B);  // Read the current state of the sensor
  902.  unsigned long currentTime = millis();
  903.  
  904.  // Status feedback
  905.  // Serial.print("Proxy B Status: ");
  906.  // Serial.println(sensorStateB == HIGH ? "Triggered (High)" : "Not Triggered (Low)");
  907.  
  908.  // Check for Proxy B (CCW)
  909.  if (!isClockwise && countProxyBEnabled) {
  910.    // Only detect Rising edge (LOW to HIGH)
  911.    if (lastStateB == LOW && sensorStateB == HIGH) {
  912.      // Debounce check
  913.      if ((currentTime - lastTriggerTimeB) > debounceDelay && (currentTime - lastTriggerTimeB) > minCountInterval) {
  914.        countB++;  // Increment count for Proxy B
  915.        lastTriggerTimeB = currentTime;
  916.        Serial.print("Proxy B Count: ");
  917.        Serial.println(countB);
  918.  
  919.         // Stop relay 5 if count reaches 5
  920.         if (countB >= 4) {
  921.          digitalWrite(RELAY6, LOW); // Stop relay 5
  922.          Serial.print("INDUCTION MOTOR CCW STOPPED");
  923.          motorRunning = false; // Set flag to stop motor
  924.          countB = 0; // Reset count after stopping
  925.          countProxyBEnabled = false; // Disable counting for Proxy B
  926.          countProxyAEnabled = true; // Re-enable counting for Proxy A
  927.         }
  928.       }
  929.     }
  930.    lastStateB = sensorStateB; // Update last state for Proxy B
  931.   }
  932. }
  933.  
  934. // End of USER CODE merged content
  935.  
  936. /* END CODE */
  937.  
Advertisement
Add Comment
Please, Sign In to add comment