Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /********* Pleasedontcode.com **********
- Pleasedontcode thanks you for automatic code generation! Enjoy your code!
- - Terms and Conditions:
- You have a non-exclusive, revocable, worldwide, royalty-free license
- for personal and commercial use. Attribution is optional; modifications
- are allowed, but you're responsible for code maintenance. We're not
- liable for any loss or damage. For full terms,
- please visit pleasedontcode.com/termsandconditions.
- - Project: Motor Controller
- - Source Code NOT compiled for: Arduino Uno
- - Source Code created on: 2025-10-14 15:24:20
- ********* Pleasedontcode.com **********/
- /****** SYSTEM REQUIREMENTS *****/
- /****** SYSTEM REQUIREMENT 1 *****/
- /* Help to update the code to use smoothly, control */
- /* the motor via VFD. additionally add enough time */
- /* delay. include Forward running, Reverse running, */
- /* Emergency stop, quick Start speed adjustment */
- /* control. */
- /****** END SYSTEM REQUIREMENTS *****/
- /* START CODE */
- /****** DEFINITION OF LIBRARIES *****/
- /****** GLOBAL VARIABLES *****/
- // Motor control pins
- const int stopPin = 2;
- const int fwdPin = 3;
- const int revPin = 4;
- const int speedPin = 9; // PWM pin for speed control
- // Safety Variables
- bool isRunning = false;
- bool emergencyStopActive = false;
- String currentDirection = "STOP";
- int currentSpeed = 0;
- unsigned long lastCommandTime = 0;
- const unsigned long SAFETY_TIMEOUT = 30000; // 30 seconds auto-stop
- /****** FUNCTION PROTOTYPES *****/
- void setup(void);
- void loop(void);
- void processCommand(char cmd);
- void runForward();
- void runReverse();
- void safeStop();
- void emergencyStop();
- void resetEmergencyStop();
- void setSpeed(int speedValue);
- void safeShutdown();
- void printStatus();
- // Implementations
- void setup(void) {
- Serial.begin(9600);
- // Pin initializations
- pinMode(stopPin, OUTPUT);
- pinMode(fwdPin, OUTPUT);
- pinMode(revPin, OUTPUT);
- pinMode(speedPin, OUTPUT);
- // Ensure known safe state
- digitalWrite(stopPin, LOW);
- digitalWrite(fwdPin, LOW);
- digitalWrite(revPin, LOW);
- analogWrite(speedPin, 0);
- isRunning = false;
- emergencyStopActive = false;
- currentDirection = "STOP";
- currentSpeed = 0;
- lastCommandTime = millis();
- Serial.println("Motor Controller Ready. Commands: F-forward, R-reverse, S-stop, E-emergency, X-reset emergency, +/ - adjust speed");
- printStatus();
- }
- void loop(void) {
- // Handle serial input
- if (Serial.available()) {
- String line = Serial.readStringUntil('\n');
- line.trim();
- if (line.length() > 0) {
- char cmd = line.charAt(0);
- processCommand(cmd);
- }
- }
- // Safety timeout
- unsigned long now = millis();
- if (!emergencyStopActive && isRunning && (now - lastCommandTime > SAFETY_TIMEOUT)) {
- safeStop();
- Serial.println("Safety timeout: auto-stop engaged.");
- }
- // Small yield to avoid busy-waiting
- // delay(10);
- }
- void processCommand(char cmd) {
- switch (cmd) {
- case 'F':
- case 'f':
- runForward();
- break;
- case 'R':
- case 'r':
- runReverse();
- break;
- case 'S':
- case 's':
- safeStop();
- break;
- case 'E':
- case 'e':
- emergencyStop();
- break;
- case 'X':
- case 'x':
- resetEmergencyStop();
- break;
- case '+':
- setSpeed(currentSpeed + 15);
- break;
- case '-':
- setSpeed(currentSpeed - 15);
- break;
- default:
- Serial.println("Unknown command. Use F, R, S, E, X, +, -");
- break;
- }
- }
- void runForward() {
- if (emergencyStopActive) return;
- digitalWrite(fwdPin, HIGH);
- digitalWrite(revPin, LOW);
- currentDirection = "FORWARD";
- // Quick start: ensure a reasonable speed
- if (currentSpeed < 150) {
- setSpeed(150);
- }
- lastCommandTime = millis();
- isRunning = true;
- Serial.println("Forward run started");
- delay(50); // allow VFD to respond
- }
- void runReverse() {
- if (emergencyStopActive) return;
- digitalWrite(fwdPin, LOW);
- digitalWrite(revPin, HIGH);
- currentDirection = "REVERSE";
- if (currentSpeed < 150) {
- setSpeed(150);
- }
- lastCommandTime = millis();
- isRunning = true;
- Serial.println("Reverse run started");
- delay(50);
- }
- void safeStop() {
- // Ramp down to 0 speed before stopping
- setSpeed(0);
- digitalWrite(fwdPin, LOW);
- digitalWrite(revPin, LOW);
- currentDirection = "STOP";
- isRunning = false;
- lastCommandTime = millis();
- Serial.println("Safe stop executed");
- delay(50);
- }
- void emergencyStop() {
- emergencyStopActive = true;
- setSpeed(0);
- digitalWrite(fwdPin, LOW);
- digitalWrite(revPin, LOW);
- currentDirection = "EMERGENCY_STOP";
- isRunning = false;
- lastCommandTime = millis();
- Serial.println("EMERGENCY STOP activated");
- }
- void resetEmergencyStop() {
- if (!emergencyStopActive) return;
- emergencyStopActive = false;
- currentDirection = "STOP";
- currentSpeed = 0;
- analogWrite(speedPin, 0);
- digitalWrite(fwdPin, LOW);
- digitalWrite(revPin, LOW);
- lastCommandTime = millis();
- Serial.println("Emergency stop reset");
- }
- void setSpeed(int speedValue) {
- speedValue = constrain(speedValue, 0, 255);
- // If emergency stop active, ignore speed changes
- if (emergencyStopActive) return;
- if (currentSpeed == speedValue) return;
- int step = (speedValue > currentSpeed) ? 5 : -5;
- while (currentSpeed != speedValue) {
- currentSpeed += step;
- if ((step > 0 && currentSpeed > speedValue) || (step < 0 && currentSpeed < speedValue)) currentSpeed = speedValue;
- analogWrite(speedPin, currentSpeed);
- lastCommandTime = millis();
- delay(20);
- }
- if (currentSpeed == 0) isRunning = false;
- else isRunning = true;
- Serial.print("Speed set to ");
- Serial.println(currentSpeed);
- }
- void safeShutdown() {
- safeStop();
- }
- void printStatus() {
- Serial.print("Dir: "); Serial.print(currentDirection);
- Serial.print(" | Speed: "); Serial.print(currentSpeed);
- Serial.print(" | Running: "); Serial.println(isRunning ? "YES" : "NO");
- }
- /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment