pleasedontcode

Motor Controller rev_03

Oct 14th, 2025
268
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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: Motor Controller
  13.     - Source Code NOT compiled for: Arduino Uno
  14.     - Source Code created on: 2025-10-14 15:24:20
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* Help to update the code to use smoothly, control */
  21.     /* the motor via VFD. additionally add enough time */
  22.     /* delay. include Forward running, Reverse running, */
  23.     /* Emergency stop, quick Start speed adjustment */
  24.     /* control. */
  25. /****** END SYSTEM REQUIREMENTS *****/
  26.  
  27.  
  28. /* START CODE */
  29.  
  30. /****** DEFINITION OF LIBRARIES *****/
  31.  
  32. /****** GLOBAL VARIABLES *****/
  33. // Motor control pins
  34. const int stopPin = 2;
  35. const int fwdPin = 3;
  36. const int revPin = 4;
  37. const int speedPin = 9;  // PWM pin for speed control
  38.  
  39. // Safety Variables
  40. bool isRunning = false;
  41. bool emergencyStopActive = false;
  42. String currentDirection = "STOP";
  43. int currentSpeed = 0;
  44. unsigned long lastCommandTime = 0;
  45. const unsigned long SAFETY_TIMEOUT = 30000; // 30 seconds auto-stop
  46.  
  47. /****** FUNCTION PROTOTYPES *****/
  48. void setup(void);
  49. void loop(void);
  50.  
  51. void processCommand(char cmd);
  52. void runForward();
  53. void runReverse();
  54. void safeStop();
  55. void emergencyStop();
  56. void resetEmergencyStop();
  57. void setSpeed(int speedValue);
  58. void safeShutdown();
  59. void printStatus();
  60.  
  61. // Implementations
  62.  
  63. void setup(void) {
  64.   Serial.begin(9600);
  65.   // Pin initializations
  66.   pinMode(stopPin, OUTPUT);
  67.   pinMode(fwdPin, OUTPUT);
  68.   pinMode(revPin, OUTPUT);
  69.   pinMode(speedPin, OUTPUT);
  70.  
  71.   // Ensure known safe state
  72.   digitalWrite(stopPin, LOW);
  73.   digitalWrite(fwdPin, LOW);
  74.   digitalWrite(revPin, LOW);
  75.   analogWrite(speedPin, 0);
  76.  
  77.   isRunning = false;
  78.   emergencyStopActive = false;
  79.   currentDirection = "STOP";
  80.   currentSpeed = 0;
  81.   lastCommandTime = millis();
  82.  
  83.   Serial.println("Motor Controller Ready. Commands: F-forward, R-reverse, S-stop, E-emergency, X-reset emergency, +/ - adjust speed");
  84.   printStatus();
  85. }
  86.  
  87. void loop(void) {
  88.   // Handle serial input
  89.   if (Serial.available()) {
  90.     String line = Serial.readStringUntil('\n');
  91.     line.trim();
  92.     if (line.length() > 0) {
  93.       char cmd = line.charAt(0);
  94.       processCommand(cmd);
  95.     }
  96.   }
  97.  
  98.   // Safety timeout
  99.   unsigned long now = millis();
  100.   if (!emergencyStopActive && isRunning && (now - lastCommandTime > SAFETY_TIMEOUT)) {
  101.     safeStop();
  102.     Serial.println("Safety timeout: auto-stop engaged.");
  103.   }
  104.   // Small yield to avoid busy-waiting
  105.   // delay(10);
  106. }
  107.  
  108. void processCommand(char cmd) {
  109.   switch (cmd) {
  110.     case 'F':
  111.     case 'f':
  112.       runForward();
  113.       break;
  114.     case 'R':
  115.     case 'r':
  116.       runReverse();
  117.       break;
  118.     case 'S':
  119.     case 's':
  120.       safeStop();
  121.       break;
  122.     case 'E':
  123.     case 'e':
  124.       emergencyStop();
  125.       break;
  126.     case 'X':
  127.     case 'x':
  128.       resetEmergencyStop();
  129.       break;
  130.     case '+':
  131.       setSpeed(currentSpeed + 15);
  132.       break;
  133.     case '-':
  134.       setSpeed(currentSpeed - 15);
  135.       break;
  136.     default:
  137.       Serial.println("Unknown command. Use F, R, S, E, X, +, -");
  138.       break;
  139.   }
  140. }
  141.  
  142. void runForward() {
  143.   if (emergencyStopActive) return;
  144.   digitalWrite(fwdPin, HIGH);
  145.   digitalWrite(revPin, LOW);
  146.   currentDirection = "FORWARD";
  147.   // Quick start: ensure a reasonable speed
  148.   if (currentSpeed < 150) {
  149.     setSpeed(150);
  150.   }
  151.   lastCommandTime = millis();
  152.   isRunning = true;
  153.   Serial.println("Forward run started");
  154.   delay(50); // allow VFD to respond
  155. }
  156.  
  157. void runReverse() {
  158.   if (emergencyStopActive) return;
  159.   digitalWrite(fwdPin, LOW);
  160.   digitalWrite(revPin, HIGH);
  161.   currentDirection = "REVERSE";
  162.   if (currentSpeed < 150) {
  163.     setSpeed(150);
  164.   }
  165.   lastCommandTime = millis();
  166.   isRunning = true;
  167.   Serial.println("Reverse run started");
  168.   delay(50);
  169. }
  170.  
  171. void safeStop() {
  172.   // Ramp down to 0 speed before stopping
  173.   setSpeed(0);
  174.   digitalWrite(fwdPin, LOW);
  175.   digitalWrite(revPin, LOW);
  176.   currentDirection = "STOP";
  177.   isRunning = false;
  178.   lastCommandTime = millis();
  179.   Serial.println("Safe stop executed");
  180.   delay(50);
  181. }
  182.  
  183. void emergencyStop() {
  184.   emergencyStopActive = true;
  185.   setSpeed(0);
  186.   digitalWrite(fwdPin, LOW);
  187.   digitalWrite(revPin, LOW);
  188.   currentDirection = "EMERGENCY_STOP";
  189.   isRunning = false;
  190.   lastCommandTime = millis();
  191.   Serial.println("EMERGENCY STOP activated");
  192. }
  193.  
  194. void resetEmergencyStop() {
  195.   if (!emergencyStopActive) return;
  196.   emergencyStopActive = false;
  197.   currentDirection = "STOP";
  198.   currentSpeed = 0;
  199.   analogWrite(speedPin, 0);
  200.   digitalWrite(fwdPin, LOW);
  201.   digitalWrite(revPin, LOW);
  202.   lastCommandTime = millis();
  203.   Serial.println("Emergency stop reset");
  204. }
  205.  
  206. void setSpeed(int speedValue) {
  207.   speedValue = constrain(speedValue, 0, 255);
  208.   // If emergency stop active, ignore speed changes
  209.   if (emergencyStopActive) return;
  210.   if (currentSpeed == speedValue) return;
  211.   int step = (speedValue > currentSpeed) ? 5 : -5;
  212.   while (currentSpeed != speedValue) {
  213.     currentSpeed += step;
  214.     if ((step > 0 && currentSpeed > speedValue) || (step < 0 && currentSpeed < speedValue)) currentSpeed = speedValue;
  215.     analogWrite(speedPin, currentSpeed);
  216.     lastCommandTime = millis();
  217.     delay(20);
  218.   }
  219.   if (currentSpeed == 0) isRunning = false;
  220.   else isRunning = true;
  221.   Serial.print("Speed set to ");
  222.   Serial.println(currentSpeed);
  223. }
  224.  
  225. void safeShutdown() {
  226.   safeStop();
  227. }
  228.  
  229. void printStatus() {
  230.   Serial.print("Dir: "); Serial.print(currentDirection);
  231.   Serial.print(" | Speed: "); Serial.print(currentSpeed);
  232.   Serial.print(" | Running: "); Serial.println(isRunning ? "YES" : "NO");
  233. }
  234.  
  235. /* END CODE */
  236.  
Advertisement
Add Comment
Please, Sign In to add comment