/********* 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 Switch" - Source Code compiled for: Arduino Uno - Source Code created on: 2024-03-31 10:11:27 ********* Pleasedontcode.com **********/ /****** SYSTEM REQUIREMENTS *****/ /****** SYSTEM REQUIREMENT 1 *****/ /* Control 2 DC Motors using L293D Bridge for forward */ /* and backward direction, pushbutton for switching */ /* on and off */ /****** END SYSTEM REQUIREMENTS *****/ /****** DEFINITION OF LIBRARIES *****/ #include /****** FUNCTION PROTOTYPES *****/ void setup(); void loop(); /***** DEFINITION OF DIGITAL INPUT PINS *****/ const uint8_t SwitchPushbuttonPin = 2; /***** DEFINITION OF MOTOR CONTROL PINS *****/ const uint8_t Motor1EnablePin = 4; const uint8_t Motor1DirectionPin1 = 5; const uint8_t Motor1DirectionPin2 = 6; const uint8_t Motor2EnablePin = 7; const uint8_t Motor2DirectionPin1 = 8; const uint8_t Motor2DirectionPin2 = 9; /***** DEFINITION OF MOTOR CLASS INSTANCES *****/ L293 motor1(Motor1EnablePin, Motor1DirectionPin1, Motor1DirectionPin2); L293 motor2(Motor2EnablePin, Motor2DirectionPin1, Motor2DirectionPin2); /****** Flag to indicate motor state *****/ bool motorsOn = false; void setup() { pinMode(SwitchPushbuttonPin, INPUT_PULLUP); motor1.setPWMOffset(10); // Optional speed offset adjustment for Motor 1 motor2.setPWMOffset(-10); // Optional speed offset adjustment for Motor 2 } void loop() { bool switchPushed = digitalRead(SwitchPushbuttonPin) == LOW; if (switchPushed && !motorsOn) { // Turn on the motors and make them go forward motor1.forward(); motor2.forward(); motorsOn = true; } else if (!switchPushed && motorsOn) { // Turn off the motors and stop them motor1.forceStop(1000); // Perform electrical braking with a 1 second handling time for Motor 1 motor2.forceStop(1000); // Perform electrical braking with a 1 second handling time for Motor 2 motorsOn = false; } }